المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : The exact size of "string" problem [modified][Solved]



C++ Programming
11-24-2009, 03:00 AM
Hi,
I allocated a struct from a heap, the struct may be like this one:

struct abc{
int tmp1;
string tmp2;
int tmp3;
}*pabc;

pabc=(abc*)malloc(sizeof abc);

And when I initialize the string(pabc->tmp2="1234"), an "Illegal Pointer" exception occured.
It seems that the size of the string is changed, so I decided to allocate a bigger size.

pabc=(abc*)malloc(sizeof abc+4);

But the error still exists.
sizeof(string("1234")) seems to be 8 instead of 4.

Can anyone help me with this problem?

modified on Sunday, November 22, 2009 11:46 PM