L
Lionel
Hi all,
Just wondering exactly how memory is allocated using the new operator?
More specifically I am interested in how the memory is calculated for
globable variables? I recently stumbled into a problem which corrected
my way of thinking.
Now, people probably won't be able to answer much to that question, so I
will give an example. This is example uses the qt library qstring.h, but
I assume this would be similar to the C++ string.h and don't think this
should be OT.
e.g. I have a class header:
class AClass {
public:
AClass( QString id );
private:
QString identifier;
int aInt;
};
And implementation:
AClass::AClass( QString id ) {
identifier = id;
aInt = 0;
}
Now, I thought the memory would be allocated on the stack for identifier
and aInt, however it appears that QString isn't a type, and so aInt gets
memory but there is no guarantee for identifier. This means the memory
address of identifer can potentially get used elsewhere if the function
where identifier was created returns. Not a desirable effect.
So as it turns out I use the * and new operator and it quite obviously
is fine.
So, how does C++ handle allocation in such situations?
Thanks and sorry for the length of the post. Hopefully this is
understandable.
Lionel.
Just wondering exactly how memory is allocated using the new operator?
More specifically I am interested in how the memory is calculated for
globable variables? I recently stumbled into a problem which corrected
my way of thinking.
Now, people probably won't be able to answer much to that question, so I
will give an example. This is example uses the qt library qstring.h, but
I assume this would be similar to the C++ string.h and don't think this
should be OT.
e.g. I have a class header:
class AClass {
public:
AClass( QString id );
private:
QString identifier;
int aInt;
};
And implementation:
AClass::AClass( QString id ) {
identifier = id;
aInt = 0;
}
Now, I thought the memory would be allocated on the stack for identifier
and aInt, however it appears that QString isn't a type, and so aInt gets
memory but there is no guarantee for identifier. This means the memory
address of identifer can potentially get used elsewhere if the function
where identifier was created returns. Not a desirable effect.
So as it turns out I use the * and new operator and it quite obviously
is fine.
So, how does C++ handle allocation in such situations?
Thanks and sorry for the length of the post. Hopefully this is
understandable.
Lionel.