Welcome, Guest. Please login or register.

Author Topic: char *buf2=new char[size]; problem  (Read 3834 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Doppie1200

  • Sr. Member
  • ****
  • Join Date: May 2004
  • Posts: 497
    • Show all replies
Re: char *buf2=new char[size]; problem
« on: November 11, 2004, 10:06:12 AM »
Looks like this would not work even if it would be C++

int size=0;
char *buf2=new char[size]; // ????

this would do nothing since size if 0.

If you want to create an array use a size greater then 0 or end up with zero elements.

Arrays will not grow on their own

memcpy(buf2+size,buf1,sizeof(buf1));
size++

This 'buf2+size' will run out of your allocated space soon and cause all kinds of undetermained behaviour.

If you really want arrays to grow dynamically you should create a new one that is larger than the previous. Copy the previous in the newly created one and then delete the previous to prevent memory leak. I don't know if this will affect memory fragmentation though.

Good luck.




Regards,
Erno

(O\\\\_|_/O) <- this is supposed to look like the front of my beetle
(entire front not possible in signature)