CStrings and pointers: heap corruption when trying to delete a character array

I've exhausted myself coming up with this, but I haven't been able to find a clear answer or help myself figure out what's going wrong. As part of a homework assignment, I am trying to dynamically allocate memory for a character array (like CString) and assign a char pointer variable to point to it, then delete the allocated memory before the program ends. I just wrap my head around pointers, so I suspect that some of my ideas about what is going on behind the scenes are wrong and I end up with Visual Studio 2010 giving me heap break points.

Here is a simplified version of what I am trying to do (my comments show what I think I am doing):

char *chPtr = new char[10]; // Create a char pointer type variable, and point it at the memory allocated for a char array of length 10
chPtr = "September";        // Assign 9 characters (+1 null terminator character) to the char array
cout << chPtr;              // Prints "September", then finds the null terminator and stops printing
delete [] chPtr;            // THIS is what causes my heap corruption -> But I don't know why. Shouldn't this delete the array of memory that chPtr is pointing at?

      

  • I tried using delete chPtr

    as well with the same heap corruption result.
  • I tried to manually assign a null terminator to the end of the array chPtr[9] = '\0';

    , but that also results in heap corruption. What for? I can access individual characters from the array when I do things like cout << chPtr[7];

    etc. Without any problem ...

What's really confusing is that this works without error:

char *chPtr = new char[10]; // As above, create a char pointer type and point it at the memory allocated for a char array of length 10
delete chPtr;               // This doesn't give any error!

      

It seems that initializing the array by assigning a value is somehow breaking me.

Can anyone help me?

+3


source to share


5 answers


this is the source of your problem:

char *chPtr = new char[10]; // Create a char pointer type variable, and point it at the memory allocated for a char array of length 10
chPtr = "September";  

      

first you allocate heap memory on chPtr

, then you allocate non-heap memory (more precisely, data segment memory) by assigning a non-heap char array with the value "september" on it.



you are trying to delete non-heap memory then get the error you got.

use strcpy(chPtr,"September")

or even better: lose C and use std::string

.

0


source


chPtr = "September";

changes where it points chPtr

. Instead of pointing to where your array of 10 was allocated char

, it now points to the static September character array. Then you try to remove that, which fails.

As a test, you can try the following:

char* p = nullptr;
std::cout << static_cast<void*>(p) << "\n";

p = new char[10];
std::cout << static_cast<void*>(p) << "\n";

p = "September";
std::cout << static_cast<void*>(p) << "\n";

      

Possible way out:

00000000
0068C988
00D18B34

      



You can see that the address at which p

the change points changes with each assignment. The block allocated in free storage ( 0068C988

) is lost on the third assignment.

If you want to assign symbols, do something like

std::strcpy(chPtr, "September");

      

This comes with all the caveats about memory, array and pointer management. What do you really want to do:

std::string s = "September";
std::cout << s;

      

+1


source


You are trying to delete the block of memory where your "September" is and is definitely not allocated on the heap, if you need to initialize the pre-assigned buffers use the strcpy (..) functions.

0


source


The appointment chPtr = "September";

does not do what you think it does. It assigns an array of static symbols to the address to the"September"

variable chPtr

. When you try to call operator delete[]

on it, things go wrong: you are trying to delete a statically allocated character array.

If you want to copy characters, you will need to use a string copy function like std :: strcpy .

Of course, the correct way to use C-style strings in C ++ is not to use C-style strings. Use one of the standard C ++ templates std :: basic_string .

0


source


chPtr = "September"

assigns nothing to the memory it points to chPtr

, instead assigns chPtr

to point to another memory that is holding "September". You cannot delete this memory.

0


source







All Articles