1

In c++, I thought that all variables are stored in stack memory, starting from higher addresses and going towards 0x0. Why it's not working here ? I am getting 0x7fff67225e90 for the address of x and 0x7fff67225e94 for the address of z.

int main(){
    int x = 7; 
    std::cout<<"x in main(): "<<x<<std::endl;
    std::cout<<"&x in main(): "<<&x<<std::endl;
    //foo();
    int z = 8;
    std::cout<<"z in mai(): "<<z<<std::endl;
    std::cout<<"&z  in  main(): "<<&z<<std::endl;

    return 0;   
}   
  • 1
    "_I thought that all variables are stored in stack memory, starting from higher addresses and going towards 0x0._" Why do you think that? – Algirdas Preidžius Oct 11 '19 at 13:50
  • Here is the source I am getting the information from : https://www.coursera.org/learn/cs-fundamentals-1/lecture/Iccq3/2-1-stack-memory-and-pointers –  Oct 11 '19 at 13:52
  • 2
    *"I thought that all variables are stored in stack memory, starting from higher addresses and going towards 0x0"* Not all variables are on the stack. And the language makes no guarantee about how those that are on the stack are organised. It doesn't even formally promise that there is a stack, though it's the most rational way of implementing what it does require. – François Andrieux Oct 11 '19 at 13:52
  • The thing about the "order" of the stack is about different stack frames (and of course platform specific), not variables within the same frame – harold Oct 11 '19 at 13:56
  • @harold you mean that, there is no guarantee of order within the same frame but if we create another function to be called, its frame is definitely going to be at a lower level ? –  Oct 11 '19 at 13:58
  • @HilbertHotel As a C++ beginner, the layout of the stack should not matter to you at all. Even for advanced developers, it's not something you generally have to worry about. In fact, relying on it is usually a mistake. If your class or tutorial is putting a lot of importance on that at the start, I would tend to believe that whoever designed it doesn't recognize what's important and what's not important and may not be a great reference for learning the language. – François Andrieux Oct 11 '19 at 13:59
  • @HilbertHotel yes, that is, on a platform where it actually works that way and a call is actually made (not just "the source code looks like a call") – harold Oct 11 '19 at 13:59
  • @FrançoisAndrieux well, I think I am lost between courses and different approaches. Just before this one I was learning from another course then It turns out to be teaching C++ as a modern C. I don't like books, any course to recommend ? –  Oct 11 '19 at 14:03
  • @AlgirdasPreidžius I don't like books, do you have a course to recommend ? –  Oct 11 '19 at 14:04
  • @HilbertHotel It's been so long, even if I remembered good resources from when I was learning, they would be outdated by now. And the best resources I've used recently to stay up to date have been books. My advice would be to *make sure* whatever resource you choose to use is intended to teach **modern** C++. Look for the keywords C++11, C++14 and C++17 specifically. Anything made before 2011 is likely to be outdated and not that useful anymore. – François Andrieux Oct 11 '19 at 14:05
  • 1
    FWIW I don't think knowing how stack frames work is useless at all, knowing how it works is *completely different* from writing code that relies on it. Misunderstanding such basics plays a large role in why beginners tend to write odd code, for example a beginner may think that local variables are allocated *one-by-one* and as such each come with an allocation cost, and that leads to overuse of XOR-swap and variable-reuse and such. – harold Oct 11 '19 at 14:08
  • @harold any good online course/tutorials to learn from. I don't like book that much but I think there is no better alternative ... –  Oct 11 '19 at 14:12
  • @HilbertHotel "_I don't like books, do you have a course to recommend?_" I don't like online courses. Didn't see a good one, yet. – Algirdas Preidžius Oct 11 '19 at 18:09

0 Answers0