2

I'm new to C++ programming and I am having some hard time understanding some concepts.

Take this code as a example:

// Example program
#include <iostream>

class nber
{
    int* value;
    
  public:  
  nber(int n)
  {
      value = &n;
  }
  int getNber()
  {
      return *value;
  }
};

int main()
{
  nber var(111);
  std::cout << "The number is:" << var.getNber() << "\n";
}

As you can see, the nber constructor receives an integer n and passes its address to the "value" pointer. What I expected is to have some kind of unwanted behavior, since the scope of the received integer (n) ends as soon as the constructor end, but the output is:

The number is:111

So the scope didn't end? If it really didn't end, when is the memory used to store the variable n going to be released? Thanks.

Float07
  • 314
  • 2
  • 12
  • 2
    *Values* go out of scope at their matching closing brace. You have a reference to a temporary, so this is undefined behavior. *Pointers* may be *dangling* in that they reference memory that has gone out of scope. It's up to us the programmers to make sure that doesn't happen. C++ will let you shoot yourself in the foot. – AndyG Jul 01 '20 at 12:20
  • Welcome to undefined behavior land, where anything can happen. – NathanOliver Jul 01 '20 at 12:20
  • 2
    You do have unwanted behaviour. The behaviour of the program is undefined. Your compiler is being most unkind to you in this case by making the program appear to work. – Bathsheba Jul 01 '20 at 12:20
  • So the best solution would be to use a "malloc" in this case or a "new" in case of an object? – Float07 Jul 01 '20 at 12:25
  • 1
    Typically you want to pass and store and return value when possible. Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Jul 01 '20 at 12:28
  • Will take a look. Thanks everyone for the help :) – Float07 Jul 01 '20 at 12:29
  • 1
    using `new` is rarely the best solution and `malloc` almost never – 463035818_is_not_an_ai Jul 01 '20 at 12:29
  • Try adding some other function calls after `nber var(111);`. Like maybe `nber var(111); cout << "hello world!\n"; cout << var.getNber();` – user253751 Jul 01 '20 at 12:32
  • See https://en.cppreference.com/w/cpp/language/ub – Jesper Juhl Jul 01 '20 at 13:58

1 Answers1

3

The scope did end. What you're seeing is Undefined Behavior - anything can happen. The number could be "purple", as far as the rules say. Or your hard disk could be erased. The latter is a bit rare, though.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • I would go and look at eric's answer he has a popular answer about undefined behavior: It basically uses a hotel room analogy, if you have an unsafe hotel, and you did not return back their key and decided to come back later to get your items, the item may be there, or the hotel may be demolished or the book might be taken, since it is undefined anything can happen.... – Yunfei Chen Jul 01 '20 at 19:29