Q. In terms of Memory Management, What is the error in the following code?
1 2 3 4 5 6 7 8 9 10 11
char* text = "Hey man!";
int n = 0;
while (text[n] != '\0')
for (int i = 0; i <= n ; i++)
message_buffer[i] = text[i];
I think message_buffer is local variable
that is automatically reclaimed after function ends.
This function returns a reference to an invalid memory location
, since message_buffer disappears right after return statement.