0

i am new to c++ and trying to learn the language from the ground up. today i was learning about pointers and if i understand the code below correctly,

i am allocating 8 bytes of memory with a pointer named 'buffer' and using memset function, i am putting 0 into each byte and i want the whole memory to be filled with 0 so the full length of this is 8 so, i set it to be 0.

which if i run in debug mode, should return at this memory address of buffer

00 00 00 00 00 00 00 00

but instead, i get

cd cd cd cd cd cd cd cd
#include <iostream>

int main()
{
    char* buffer = new char[8];
    memset(buffer, 0, 8);

    delete[] buffer;
    std::cin.get();
}

Again, I come from javascript/python background and memory, heap, stack are all new to me. please help me fix this...

masafood
  • 325
  • 2
  • 11
  • 1
    Where exactly do you set your breakpoint? Before `memset` its uninitialized and after `delete` its undefined behavior. – tkausl Feb 03 '23 at 09:32
  • 2
    Do you perhaps read after `delete[]`? Try printing the bytes from the code itself. – HolyBlackCat Feb 03 '23 at 09:36
  • 2
    CD means uninitialised heap memory in visual studio https://en.m.wikipedia.org/wiki/Magic_number_(programming) – Alan Birtles Feb 03 '23 at 09:39
  • thank you both! it works now and now i know what cd is! – masafood Feb 03 '23 at 09:57
  • 1
    you could still clarify the question and then someone could write an answer, even you could write the answer if you like. Comments are only for clarification (which was necessary) but not actually for answers – 463035818_is_not_an_ai Feb 03 '23 at 11:22
  • [When and why will a compiler initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete?](https://stackoverflow.com/q/370195/995714) – phuclv Feb 03 '23 at 12:54

0 Answers0