0

im new to c++ and im trying to understand how the "new" keyword works in c++ and when to use it , so i made this little program to test and see how the heap and stack works

#include <iostream>
#include <string>

class Player
{
    private:
    const std::string name;

    public:
    Player() : name("unknown") {}
    Player(const std::string i_name) : name(i_name) {}
    const std::string GetName() const {return name;}
};

int main()
{
    Player* e;
    {
        Player player("Redouane");
        e = &player;
        std::cout<<player.GetName()<<std::endl;
    }
    std::cout<<(*e).GetName()<<std::endl;
}

based on what i've learned , the memory space in the stack should be freed by the end of the scope , which means the second output must be "unknown" . BUT , in my case it's not , it gives me the same output

Redouane 
Redouane
  • 1
    when to use it: (almost) never. [Why should C++ programmers minimize use of 'new'?](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) – 463035818_is_not_a_number Mar 31 '21 at 15:26
  • No, that's not what the space being freed means. It doesn't mean it must output "unknown". What happens if you access a freed variable in C++? – user253751 Mar 31 '21 at 15:26
  • 2
    unfortunately lots teaching is based on the false premise that you first need to learn the things "the wrong" way. By the time they show you the "right" way you already got used to allocate memory via `new` and pointers everywhere :/ – 463035818_is_not_a_number Mar 31 '21 at 15:28
  • 1
    `e` points to an object that is no longer exists. So `*e` is undefined behavior. It is an error to assume the pointer will refer to a default constructed instance, it is just a dangling pointer and can't be used. The second `"Redouane"` output is basically a coincidence. – François Andrieux Mar 31 '21 at 15:28
  • This program has UB, youre accessing a pointer to a deleted object. – Borgleader Mar 31 '21 at 15:29
  • "Wrong" way, I've never seen. Hard way, I've seen tons. It helps to have an idea of what's going on under the hood so that when the easy way fails, you can troubleshoot or still do what needs to be done. – sweenish Mar 31 '21 at 15:30
  • Technical note that you'll rarely see in the wild. C++ is designed so that you can do automatic storage without a stack and dynamic memory without a heap. It lays out the required behaviour and allows you to use hamster wheels if you can make them behave correctly. A more practical reason to use automatic and dynamic instead of stack and heap is automatic describes the behaviour of the storage: the lifetime is automatically managed. It is destroyed automatically. With dynamic, the lifetime is dynamic. Stack and heap don't really tell you anything about what happens. – user4581301 Mar 31 '21 at 15:43
  • thx for all the answers , as @FrançoisAndrieux it is a coincidence ... i tried replacing "Redouane" with a longer name ... and it works just fine ... the object gets destroyed and when trying to display the value of *e it returns some random stuff ... apparently when i give a short name (tried "Redo") somehow *e returns the same name – Shkoroski Apr 01 '21 at 13:22
  • @Shkoroski Maybe your platform implements Short String Optimization. So short strings gets stored directly within the `std::string`. And maybe your generated assembly never ends up reusing the memory your `Player` used to occupy. That could lead to an executable which happens to print the same string as was assigned to `player`. – François Andrieux Apr 01 '21 at 13:36

0 Answers0