0

I'm having trouble deleting these two pointers:

int *p = new int;
    char * string1 = new char[20];
    char * string2 = new char[25];
    strcpy(string1, "Sheldon");

    string2 = string1;
    delete p;
    delete[] string2; //this works without the next line
    delete[] string1; //when I add this, it fails at string2 = string1

I'm using the memory leak detection with

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

When I run he program without "delete[] string1," it gives me "{66} normal block at 0x0075E300, 25 bytes long." So "delete[] string2" is deleting string1. Which doesn't make sense to me, but I'm guessing it has to do with the assignment. I tried looking up stuff about it, but nothing has worked.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
abyssmu
  • 165
  • 1
  • 12

4 Answers4

4

The code fails because you're deleting the same pointer twice, which results in undefined behavior (see here). After the statement string2 = string1; the pointers string1 and string2 hold the same address, and you can no longer access the address stored instring2 before the assginment, which also results in a memory leak.

If you wanted to copy string1 to string2, you shold use strncpy(string2, string1, 20) (see documentation) in which case the pointers themselves remain unchanged and the deallocation code you provided is valid.

Community
  • 1
  • 1
blazs
  • 4,705
  • 24
  • 38
0

You will have to delete the memory assigned to string2 before being assigned string1.

AFter asssignment, string2 and string1 are one and same. Check below code for reference:

int *p = new int;
char * string1 = new char[20];
char * string2 = new char[25];
strcpy(string1, "Sheldon");
delete[] string2; //this would prevent memory leak
string2 = string1;
delete p;

delete[] string1; //now you are releasing the memory held by string2 too.
0

As you already identified, the problem in your code is the assignment. After string2 = string1;, both string2 and string1 point to the same memory location. Therefore, when you call delete[] string2 you are free the memory pointed by string1. Then you call delete[] string1 and you get undefined behaviour as the memory to which the pointer points has already been freed. That is, you are free twice the same pointer.

Also, your code contains a memory leak because you are not de-allocating the initial memory reserved for string2. What you should do is :

    delete[] string2;  // release the memory for string2
    string2 = string1; // string1, string2 point to the same memory area
    delete[] string1; // release the memory for string1
Giuseppe Pes
  • 7,772
  • 3
  • 52
  • 90
0
char * string1 = new char[20];
char * string2 = new char[25];
strcpy(string1, "Sheldon");

string2 = string1;
delete p;
delete[] string2; //this works without the next line
delete[] string1; //when I add this, it fails at string2 = string1

Three problem:

  1. The original char array which the original variable string2 pointed to would never be deleted. (memory leak)
  2. By delete[] string2; the original char array which string1 pointed to would be deleted. (recycle unintended memory)
  3. By delete[] string1; after delete[] string2;, the memory that already deleted, would be deleted again.(fail)
lulyon
  • 6,707
  • 7
  • 32
  • 49