-1

Yesterday I fell upon an awkward question and would love for some help!

If I allocate a dynamic pointer in c in Visual Studio, then free it, I saw that the memory is then changed to "dd dd dd dd dd dd dd dd". I was taught when memory is freed, it is not set or changed by the operating system but only set as "unused memory" that the system can allocate again and overwrite.

Code:

int* x = (int*)malloc(sizeof(int) * 2);
*x = 1;
*(x + 1) = 2;
free(x);

would love for some help with understanding whats going on and why this is acting like it is, have provided some before/after pictures down bellow.

Thanks for the help!!

Roe
  • 61
  • 1
  • 3
  • Does this answer your question? [In Visual Studio C++, what are the memory allocation representations?](https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations) – Ken Y-N Oct 28 '20 at 06:57
  • The point is that you are not supposed to even know this as you are not allowed to access memory after you free it. There is no requirement to change the memory content or to keep it untouched. – Gerhardh Oct 28 '20 at 09:21

1 Answers1

3

The debugging version of Visual Studio's C library sets freed memory to 0xdd in order to make it easier to debug. It makes it easier to tell that memory has been freed when you are debugging a program.

If you compile a release version, this will not happen.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415