PDA

View Full Version : Dynamic memory allocation...


Koepi
17th September 2002, 11:43
Hello,

I once more tried to make a GUI more comfortable and switched from fixed buffer sizes to "dynamic" ones:

char *buffer = new char(m_setup.m_paketsize);
...
infile.read(buffer,m_setup.m_paketsize);
outfile.write(buffer,m_setup.m_paketsize);
...
delete[] buffer;

but of course, this code crashes on execution. (the read and write is in a loop, of course).

Has someone a hint how to solve this gracefully?

Thanks,

regards,
Koepi

mtrooper
17th September 2002, 12:03
This is the problem:
char *buffer = new char(m_setup.m_paketsize);
instead you need this:
char *buffer = new char[m_setup.m_paketsize];

Example:

char *buf=new char[len];
scanf("%s", buf);
printf("%s\n", buf);
delete [] buf;


Of course, I may be missing the point (dunno what kind of params .read .write methods take).

Koepi
17th September 2002, 12:33
Thanks a million, the brakets were indeed the problem!

:)

Best regards,
Koepi