Memory Management : scope and local pointer variable

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* secret_message()
{
  char message_buffer[100];
  char* text = "Hey man!";
  int n = 0;
  while (text[n] != '\0')
    n++;
  for (int i = 0; i <= n ; i++)
    message_buffer[i] = text[i];
  return message_buffer;
}


Answer.
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.

Is it correct?

Please let me know.
Thanks,
You are correct.
Topic archived. No new replies allowed.