1

There's a PDF I'm reading which says that a pointer is invalid after it passes out of scope.

See slide #14 in the file below: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-087-practical-programming-in-c-january-iap-2010/lecture-notes/MIT6_087IAP10_lec05.pdf

Now I wrote almost the exact same code below in C++ with Dev-C++ compiler:

#include <iostream>
using namespace std;

char* get_message()
{
      char msg[]="Hello";
      return msg;
}

int main()
{
    char *ptr = get_message();
    cout<<ptr<<endl;
    system("PAUSE");
    return 0;
}

In the original file, the code uses the "puts" function to print out the char string. It prints out garbage. I was expecting garbage, but it prints out "Hello" just fine. Curiously, if I amend my code to:

char *ptr = get_message();
puts(ptr);
cout<<ptr<<endl;

It prints out garbage twice, suggesting the original "ptr" pointer has been modified by the "puts" function.

Can somebody please explain exactly what is happening? Why does cout prints out the string just fine (although there's a warning saying "address of local variable returned") even though the pointer is supposed to be invalid? Why does "puts" not work? Why does cout not work after puts? Why do comets always land in craters?

mrsinister
  • 97
  • 2
  • 7

1 Answers1

0

In your get_message() function, msg is local to the function. After the function returns there is no existence of msg. so, using the return value of the function inside the caller invokes undefined behavior.

FWIW, once you hit UB, there is absolutely no guarantee of any output either matching or deviating from your expected output.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • He knows. You didn't answer the question. – Lightness Races in Orbit Sep 26 '15 at 12:44
  • @LightnessRacesinOrbit Oh is it? How does not mentioning UB answers this question? – Sourav Ghosh Sep 26 '15 at 12:45
  • Because the question is "I know this is UB. Why can I still see 'Hello'?". So the answer is not "This is UB." Just parroting the old "This is UB" is not helpful or useful in any way. – Lightness Races in Orbit Sep 26 '15 at 12:45
  • 1
    @LightnessRacesinOrbit In that case, I'd doubt OP _knows_ anything about UB. The basic of UB is UB, including the apparently _expected behaviour._ – Sourav Ghosh Sep 26 '15 at 12:47
  • The OP is asking for an explanation. Don't hide behind "this is UB". Give an explanation (or don't, since this is a dupe). – Lightness Races in Orbit Sep 26 '15 at 12:57
  • @LightnessRacesinOrbit OP was asking for explanation since he expected some garbage output _always_, which is not the case. and an explanation will be dependent on many things. So, let's better go with the nice dupe you added. – Sourav Ghosh Sep 26 '15 at 13:00
  • @LightnessRacesinOrbit You overestimate my knowledge. I did not, in fact, know what UB was, but that didn't make things clearer for me. Your statement " 'expecting' anything was a mistake" made everything clear. :) – mrsinister Sep 26 '15 at 13:07
  • @SaadTariq: You may not have known the term, but your opening statement _"There's a PDF I'm reading which says that a pointer is invalid after it passes out of scope"_ sums up this answer. :) – Lightness Races in Orbit Sep 26 '15 at 13:54