Hello all,

I recently was reading some code and i found something along the lines of this:

void GetText() {
    int* size = new int;

    // .......
    // lots of code here
    // .......

   size = new int;
   
   // crap here
}

^^ as you can see size is 'new'ed twice without being deleted. My question is, does this cause a memory leak? or does the first memory allocated get replaced by the second 'new'.

Thanks in advance :)

It creates a leak...

new

allocates a new place in memory at which the pointer then points.

delete

frees the memory at which the pointer points.

Hence, not deleting any pointer that has been given a part of the heap, will cause a memory leak. Maybe a very small one, but it does cause trouble.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.