In my class we are writing our own copy of C's malloc() function. To test my code (which can currently allocate space fine) I was using:
char* ptr = my_malloc(6*sizeof(char));
memcpy(ptr, "Hello
", 6*sizeof(char));
printf("%s", ptr);
The output would typically be this:
Hello
Unprintable character
Some debugging figured that my code wasn't causing this per se, as ptr's memory is as follows:
[24 bytes of meta info][Number of requested bytes][Padding]
So I figured that printf was reaching into the padding, which is just garbage. So I ran a test of:
printf("%s", "test
d");
and got:
test
d
Which makes me wonder, when DOES printf("%s", char*) stop printing chars?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…