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.
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.