Re: Detecting pointer on the heap
On Apr 2, 7:46 am, Goran <goran.pu...@gmail.com> wrote:
On Apr 2, 9:36 am, pfultz2 <pful...@yahoo.com> wrote:
For example, if I have two classes like this:
template<class T, int N>
class value_array
{
T data[N];
public:
int length()
{
return N;
}
T* pointer()
{
return data;
}
//Methods for operator overloading
};
template<class T>
class array
{
int len;
T* data;
public:
explicit array(int n)
{
len = n;
data = new T[N];
}
template<int N>
array(value_array<T, N>& a)
{
data = a.pointer();
len = N;
}
int length()
{
return N;
}
T* pointer()
{
return data;
}
void deallocate()
{
if (is_heap(data)) delete [] data;
}
//Methods for operator overloading
};
This is kind of a rough sketch of what im trying to accomplish.
Why not simply this:
class array
{
// ...
bool _owns;
explicit array(int n) : _owns(true)
{
len = n;
data = new T[N];
}
template<int N>
array(value_array<T, N>& a) : _owns(false)
{
data = a.pointer();
len = N;
}
~array()
{ // NB: you should probably forget your "deallocate" idea.
if (_owns) delete [] data;
}
}
But! But... I seriously question your design.
Why do think you should have modifiable data in multiple arrays like
this? Do you actually want to mix them this way? From a design
standpoint, it seems mighty dubious to me.
One reason I can think of why you would want that is that after
__serious__ performance measurement and a __serious__ work on
optimization (most notably, elimination of copying), you still need to
go faster.
If, OTOH, you were thinking "it will be faster if I do it this way"
for no apparent reason... Just forget the whole idea and go for
std::vector. Honestly. Also, did you consider using shared_ptr?
Goran.
--
[ Seehttp://www.gotw.ca/resources/clcm.htmfor info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
The problem with vector and shared_ptr, which would give the same
semantics respectively, is first they are not a POD type. Secondly, I
want to emulate the semantics that are in C, but have lenght available
also. So there are two ways to create an array in C, 1) Create one
dynamically, and use a pointer to it, or 2) Create one statically on
the stack. Now, using my design I can pass arrays by value or by
reference, like this:
void fun1(array<int> a)
{
//Process array
}
int main(int argc, char** argv)
{
value_array<int, 5> a;
fun1(a);
}
Now it works like this also, in C:
void fun1(int* a, int len)
{
//Process array
}
int main(int argc, char** argv)
{
int a[5];
fun1(a, 5);
}
But the length is carried along with it and plus I can add begin() and
end() methods to get the iterators(or rather pointers) for the array,
which is the standard methods to call on containers. This only seems
like the next logical step in C++, to use a class. The problem is when
I want to create a managed version of the array class, for
shared_array, its ok. Here is rough example:
template<class T>
class shared_array : public array<T>
{
private:
share_count sc; //Similar to whats used in boost shared_ptr
public:
template<int N>
shared_array(value_array<T, N> a)
{
data = a.pointer();
len = N;
sc = share_count(data, null_deleter());
}
explicit shared_array(int n)
{
data = new T[n];
len = n;
sc = share_count(data, array_deleter());
}
//Make the user decide for how to deallocate
template<class D>
shared_array(array<T> rhs, D deallocator)
{
data = a.pointer();
len = n;
sc = share_count(data, deallocator);
}
};
But sometimes shared_array is too much, so it would be better to have
unique_array:
template<class T>
class unique_array : public array<T>
{
template<int N>
unique_array(value_array<T, N> a)
{
data = a.pointer(); //We shouldnt delete beacause its on the
stack
len = N;
}
explicit unique_array(int n)
{
data = new T[n];//We should delte because its on the heap
len = n;
}
unique_array(array<T> rhs)
{
data = a.pointer(); //Maybe we delete or maybe not, this could
be from the heap or the stack
len = n;
}
//...create move constructors
~unique_array()
{
this->deallocate(); //How can I tell if this is on the stack or
in the heap?
}
};
However, we dont know if the array should be deallocated or not. We
could always carry a bool along with the array to know whether it is
from the stack or heap,but it has extra overhead. and i thought there
was an easy way to tell if the pointer is on the heap, or a Win32 or
POSIX command that could tell you if it was on the heap or not.
Thanks for the answers
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]