We help IT Professionals succeed at work.

Question about dynamic allocation with operator new.....

chenwei
chenwei asked
on
Medium Priority
244 Views
Last Modified: 2006-11-17
My program lokks like:

****************************
...
...
void main()
{
...
...
while(1)
{
  MyFunc();
}
...
}

void MyFunc()
{
int i;
char  **szFileName;
...
i = 0;
szFileName = new char*;
szFileName[i] = new char[MAX_PATH];
i++;
...
...

}

****************************
The program runs. My question is: I haven't used the "delete" to free the memory since I thought everytime the function MyFunc is called, it will generate a new szFileName but with the same name and the index will be repeated from 0, so there wouldn't be overflow problem. Am I right?

Comment
Watch Question

Hi,

In C++, new is used to allocate memory at run time from heap.

Hence, whenever the MyFunc() is called, memory of 4 bytes will be allocated.

The variable szFileName will point to new location from heap.

Please note that the concept of " it will generate a new szFileName but with the same name " is wrong.

The variables are only placeholders of memory location.

The new(), malloc() etc allocates memory and the variables points to these memory locations.

For e.g :-

char  **szFileName;
szFileName = new char*;

// Suppose szFilename is at 1000 memory location.The initial value it holds will be junk or null.
// After new, it will pont to the memory location allocated at run time, say 2000.

Hence, one needs to use delete operator to free the memory locations we have asked the compiler to do for us at run time.


> The program runs. My question is: I haven't used
> the "delete" to free the memory since I thought everytime
> the function MyFunc is called, it will generate a new
> szFileName but with the same name and the index
> will be repeated from 0, so there wouldn't be overflow
> problem. Am I right?

no... every time you call new, the system will allocate a new block of memory... this block will not be 'released' until either you call delete or the program ends.  So in your case, every time you call MyFunc, memory is leaked.

In your case, the amount of memory leaked is 4 bytes (for the first new) + MAX_PATH bytes (for the second) on every call.

To put it right add you must call delete[] on the array you allocate and delete on the single pointer szFileName.


Author

Commented:
What's the difference if I use delete szFileName or use delete []szFileName?

Commented:
In practice, there is no difference, at least for char[].  

The theory is something else, though.  The standard says that calling delete on something that you new[]'ed (or delete[] on a new) is undefined behaviour, which means that the program could do almost anything at that point.

Generally, what happens is that the individual items of the array are not destructed properly if you new[] then delete.  If the objects are more interesting than char, then not calling their destructor could be a leak.

Commented:
As a general rule:
DON'T EVER MIX UP THE DELETE AND NEW!!!!!!

(Simple to follow, simple to remember.  And it may save your bacon some day.)

Commented:
All I know so far, syntax : delete [] used to deallocate array type object, whereas syntax : delete for other than array type.

Author

Commented:
Thanks for the info from all sites!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.