Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've exhausted myself googling this, but I haven't been able to find a clear answer or help myself understand what is going wrong. As part of a homework assignment, I'm trying to dynamically allocate memory for a character array (ie. a CString) and assign a char pointer variable to point at it, then delete the allocated memory before I end the program. I'm just wrapping my head around pointers so I suspect that some of my understanding of what's happening behind the scenes is not correct, and I'm ending up with Visual Studio 2010 giving me heap corruption breakpoints.

Here's a simplified version of what I'm trying to do (my comments indicate what I think I'm 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've tried using delete chPtr as well, with the same heap corruption result.
  • I've tried manually assigning a null terminator to the end of the array chPtr[9] = '\0'; , but this also results in heap corruption. Why? I can access individual characters from within the array when I do things like cout << chPtr[7]; etc without any problems...

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 breaking things for me somehow.

Can anybody help me out?

like image 567
b1skit Avatar asked Mar 01 '26 21:03

b1skit


1 Answers

chPtr = "September"; changes where chPtr is pointing. Instead of pointing to wherever your array of 10 chars was allocated, it is now pointing to the static character array "September". You are then trying to delete that, which fails.

As a test, you can try this:

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";

A possible output would be:

00000000
0068C988
00D18B34

You can see that the address where p is pointing changes at every assignment. The block allocated on the free store (0068C988) is lost on the third assignment.

If you want to assign characters, do something like

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

This comes with all the caveats of memory management, arrays and pointers. What you really want to do is:

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

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!