Thread: C++ storage location for various variables

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    44

    C++ storage location for various variables

    hi,
    I had a basic question about memory storage. I have seen many threads and was somewhat confused.
    where would the following be stored
    1) Automatic variables ( local variables within function, and function parameters)
    2) Register variables
    3) static variables with no linkage( within function or block)
    4) static variable with external linkage ( global variable available in multiple files)
    5) static variable with internal linkage( global variable for the current file)
    6)volatile variable
    7) variables with "new" (dynamic memory)

    Also, consider the following code taken from C++Primer.
    Code:
    #include <iostream>
    using namespace std;
    
    double warming = 0.3; // external variable
    
    void update(double dt); 
    void local();
    
    int main() 
    {
     cout << “Global warming is “ << warming << “ degrees.\n”;
     update(0.1); // call function to change warming
     cout << “Global warming is “ << warming << “ degrees.\n”;
     local(); // call function with local warming
     cout << “Global warming is “ << warming << “ degrees.\n”;
     return 0;
    }
    void update(double dt) // modifies global variable
    {
      extern double warming; // optional redeclaration
      warming += dt;
      cout << “Updating global warming to “ << warming;
      cout << “ degrees.\n”;
    }
    void local() // uses local variable
    {
      double warming = 0.8; // new variable hides external one
      cout << “Local warming = “ << warming << “ degrees.\n”;
      // Access global variable with the // scope resolution operator
      cout << “But global warming = “ << ::warming;
      cout << “ degrees.\n”;
    }
    the program results is
    Code:
    1. Global warming is 0.3 degrees.
    2. Updating global warming to 0.4 degrees.
    3. Global warming is 0.4 degrees.
    4. Local warming = 0.8 degrees.
    5. But global warming = 0.4 degrees.
    6. Global warming is 0.4 degrees.
    My question is following
    a) if the statement
    Code:
       extern double warming;
    was not used, still variable "warming" inside the function update(dt) would be treated as global and its value updated to 0.4?
    b) when in function local(), variable "warming" is created again as local variable, what happens in memory? does the compiler create another variable with vale 0.8 with different name and "cout" prints it rather than the variable warming or does the compiler create 2 variables in memory with same name and know which one to print?

    Thanks
    sedy

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Well this looks like a homework problem to me.
    How about you post your guesses and we'll somewhat help fill the knowledge gaps.

    To start with though, the volatile and register keywords pretty much have no effect on where a variable is stored, except that I suppose a volatie variable probably cant be stored in a register. It does somewhat depend on the compiler too. Really for the register keyword, the answer is "wherever the compiler chooses".
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    All variables are placed wherever the compiler chooses to place them. Such decisions depend on the host environment (hardware, operating system, etc).

    The register keyword in C and C++ is just a hint, that the compiler is free to ignore. If the compiler takes the hint - and most modern compilers do not, because they can make such decisions better than a programmer can - then register variables might be placed in machine registers.

    The only things that, practically, programmers should worry about with any type of variable is

    1) Not modifying it before it exists.

    2) Not accessing its value before it is initialised.

    3) Not accessing or modifying it after it has ceased to exist (passed out of scope, been deleted, etc)

    4) How to declare it (i.e. code that needs to use a variable is told that it exists somewhere)

    5) How it is defined (a definition is a special type of declaration that ensures something exists, once and once only)

    6) How it is initialised (i.e. given a value that can be accessed without invoking undefined behaviour)

    7) How it is destroyed (so, as far as the program is concerned, it ceases to exist).

    These requirements rarely, if ever, require understanding of where a variable is actually placed within memory, machine registers, disk files, executable segments, etc. If you address each of these requirements, you will be in a position to sensibly answer your questions.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    44
    hi,
    it was not a homework problem,but the explanation was given in the book, which was no clear for many cases. so, I wanted to know what really happens.
    thanks
    sedy

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    44
    hi,
    what i meant was, from books and discussions, I know the following, which i wanted to confirm
    1) Automatic variables ( local variables within function, and function parameters)
    :stack
    2) Register variables
    : as you told, most of the time, processor register

    3) static variables with no linkage( within function or block)

    4) static variable with external linkage ( global variable available in multiple files)

    5) static variable with internal linkage( global variable for the current file)

    6)volatile variable

    7) variables with "new" (dynamic memory)
    :heap

    answers for parts 3,4,5,6 were not clear.
    any help now?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    As was said before, the compiler can and will put variables wherever it wants. There is no guarantee that an automatic variable will be on the stack, or that variables allocated with new will end up on the heap (although the last one is a lot more likely than the first one). So there's no way to say where any of the variables will be stored (and, for that matter, the whole point of "volatile" is generally that it isn't stored anywhere).

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    and, for that matter, the whole point of "volatile" is generally that it isn't stored anywhere
    No, that's not what volatile means. But I won't go into it any deeper, since a beginner never needs to use volatile. (For that matter, very few non-beginners need to, either.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by CornedBee View Post
    No, that's not what volatile means. But I won't go into it any deeper, since a beginner never needs to use volatile. (For that matter, very few non-beginners need to, either.)
    I suspect tabstop is referring to a couple of special cases in the standard. For example, a const volatile variable, if it is never used, is not required to have any memory allocated for it.

    More generally, however ......

    All volatile means is that the variable may be modified in ways that the compiler cannot detect.

    Practically, that often has an effect on ability of a compiler to optimise any code that accesses or modifies that variable (e.g. the compiler cannot assume the variable maintains the same value throughout the body of a function, even if that function does not actually change the value).

    The volatile keyword does not imply any specific change of where that variable may be placed in memory (a compiler may place a variable in a different location depending on whether it is volatile or not, but is not required to), except in the special case I referred to above.

    As you say the meaning is a little deeper, but I doubt we need to discuss the abstract machine today.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by grumpy View Post
    I suspect tabstop is referring to a couple of special cases in the standard. For example, a const volatile variable, if it is never used, is not required to have any memory allocated for it.
    But doesn't that apply to all unused variables, regardless of cv-qualifiers?

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Nope.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by grumpy View Post
    I suspect tabstop is referring to a couple of special cases in the standard. For example, a const volatile variable, if it is never used, is not required to have any memory allocated for it.
    That's an effect of const, not of volatile.
    Unless you can quote a passage that says otherwise?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I was going on memory in my previous comment, with room for error that implies. Not having the standards on the machine I'm using tonight, I can't confirm that for certain.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I was referring to Memory-mapped I/O - Wikipedia, the free encyclopedia.

    I suppose you can argue that it's still "memory"....

  14. #14
    Registered User
    Join Date
    May 2011
    Posts
    44
    thanks all

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    If a variable is not used, then the compiler is free to remove it completely, regardless of cv qualification. Simmilarly if a non-volatile variable is never read from. After all, such optimizations have no effect on the program function.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default Storage Class for Global Variables
    By zane.sims in forum C Programming
    Replies: 3
    Last Post: 09-18-2010, 11:04 AM
  2. Replies: 6
    Last Post: 12-02-2009, 08:47 AM
  3. Data Storage Question, and Dynamic variables?
    By Zeusbwr in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2004, 11:01 PM
  4. hex values/variables location
    By threahdead in forum C Programming
    Replies: 10
    Last Post: 02-21-2003, 12:57 PM
  5. Replies: 4
    Last Post: 10-12-2002, 03:12 AM

Tags for this Thread