data member allocation (local vs heap)


U

u carcamagnu

Hello everybody,
is there any best practice when allocating data member locally rather
then on the heap? (I'm not sure if 'locally' is correct)

I mean which is preferable between
struct S1{
int v[10];
};

and
struct S2{
int* p;
S2(){ p=new int[10]; }
~S2(){ delete [] p; }
};

Apart from the first being 'easier', lacking of ctor e dtor, the only
arguments I thought about are:

a. if 'v' is not shared among different objects, using S1 gives me
'for free' a working copy ctor and assignment operator, while S2 needs
an explicit copy of elements of the vector

b. if S1 has many 'large' data members you risk to run out the stack
if you are using many automatic instances of S1, I mean
int main(){
S1 s1;
S1 s2;
[...]
return 0;
}
so the question becomes: in there a data members size limit to prefer
S2?

Thank you all.
 
Ad

Advertisements

B

Bo Persson

u said:
Hello everybody,
is there any best practice when allocating data member locally
rather then on the heap? (I'm not sure if 'locally' is correct)

I mean which is preferable between
struct S1{
int v[10];
};

and
struct S2{
int* p;
S2(){ p=new int[10]; }
~S2(){ delete [] p; }
};

Apart from the first being 'easier', lacking of ctor e dtor, the
only arguments I thought about are:

a. if 'v' is not shared among different objects, using S1 gives me
'for free' a working copy ctor and assignment operator, while S2
needs an explicit copy of elements of the vector

b. if S1 has many 'large' data members you risk to run out the stack
if you are using many automatic instances of S1, I mean
int main(){
S1 s1;
S1 s2;
[...]
return 0;
}
so the question becomes: in there a data members size limit to
prefer S2?

You might use S2 if the size is very large, where 'large' depends on
your system. The more common use for S2 is if the size isn't known in
advance, because then S1 doesn't work.


Bo Persson
 
F

Francesco S. Carta

Il 22/06/2010 09:37, u carcamagnu ha scritto:
Hello everybody,
is there any best practice when allocating data member locally rather
then on the heap? (I'm not sure if 'locally' is correct)

I mean which is preferable between
struct S1{
int v[10];
};

and
struct S2{
int* p;
S2(){ p=new int[10]; }
~S2(){ delete [] p; }
};

Apart from the first being 'easier', lacking of ctor e dtor, the only
arguments I thought about are:

a. if 'v' is not shared among different objects, using S1 gives me
'for free' a working copy ctor and assignment operator, while S2 needs
an explicit copy of elements of the vector

b. if S1 has many 'large' data members you risk to run out the stack
if you are using many automatic instances of S1, I mean
int main(){
S1 s1;
S1 s2;
[...]
return 0;
}
so the question becomes: in there a data members size limit to prefer
S2?

Thank you all.

Not exactly the response you were looking for - you already had it from
Bo - but I must recall that using a standard container such as
std::vector should be preferred, except in case you really need to use a
raw C-style array.
 
R

red floyd

Not exactly the response you were looking for - you already had it from
Bo - but I must recall that using a standard container such as
std::vector should be preferred, except in case you really need to use a
raw C-style array.

Even then, you should prefer the vector, because it's easy to convert
a vector to a raw C-style pointer:

std::vector<T> v;
// initialize v
T* p = &v[0];
 
Ad

Advertisements

F

Francesco S. Carta

Not exactly the response you were looking for - you already had it from
Bo - but I must recall that using a standard container such as
std::vector should be preferred, except in case you really need to use a
raw C-style array.

Even then, you should prefer the vector, because it's easy to convert
a vector to a raw C-style pointer:

std::vector<T> v;
// initialize v
T* p =&v[0];


If one needs a raw C-style array, needs a raw C style array, not just a
raw C-style pointer - given, I always suggest to use higher level
facilities... so then yes, I would do as you suggest if I could get
along with just a pointer.

I know there is something (much) that I don't know, other's requirements
fall in that "unknown" range. So then: one should use a vector unless
one really _is_ in need of an array ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top