Thread: where are static values stored?

  1. #16
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by dwks View Post
    They're called "auto" variables, after the keyword of the same name. Besides, you can see the value of automatic variables inside different functions if you need to, in the call stack. If you tried to see variables outside the call stack, you wouldn't get very meaningful values.
    Code:
    C>type viewvar.c
    #include <stdio.h>
    
    void func() {
        puts("In func()");
    }
    
    int main() {
        int mainvar = 3;
        func();
        return 0;
    }
    
    C>gdb -q viewvar.exe
    (gdb) break viewvar.c:4
    Breakpoint 1 at 0x401296: file viewvar.c, line 4.
    (gdb) r
    Starting program: viewvar.exe
    
    Breakpoint 1, func () at viewvar.c:4
    4           puts("In func()");
    (gdb) bt
    #0  func () at viewvar.c:4
    #1  0x004012da in main () at viewvar.c:9
    (gdb) frame 1
    #1  0x004012da in main () at viewvar.c:9
    9           func();
    (gdb) l
    4           puts("In func()");
    5       }
    6
    7       int main() {
    8           int mainvar = 3;
    9           func();
    10          return 0;
    11      }
    (gdb) p mainvar
    $1 = 3
    (gdb) c
    Continuing.
    In func()
    
    Program exited normally.
    (gdb) q
    
    C>
    I am not famiililar with that debugger as I don't use one at all myself but it seems you
    that will only work on a few specific occasions and you cannot guarantee to be
    able to see the 'variable'.

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How do you know? I mean, you've never even touched a debugger, right? They're so filthy and complex and hate global variables that it gives you itches to use them, so you're better off with buggy code where you add a lot of printfs to track down your errors.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Elysia View Post
    It was comprehensible. First you mentioned "function," as in you were calling a function and it returned an int, and then you mentioned you never mentioned you were calling a function. That's what I gather.
    No no have taken it out of context.

    I mean how could you access this static variable any way?

    Code:
    int somevariable;
    
    somevariable=do_something;
    I make no mention of calling the function here, so I am not sure where that idea came from.

  4. #19
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Elysia View Post
    How do you know? I mean, you've never even touched a debugger, right? They're so filthy and complex and hate global variables that it gives you itches to use them, so you're better off with buggy code where you add a lot of printfs to track down your errors.
    I can manage fine without a debugger, they tend to be counter productive, I don't tend
    to get many bugs in my code so I have not have much experience using them unfortunately.

    Maybe someone could explain how the deal with automatic variables?

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Maybe someone could explain how the deal with automatic variables?
    Use them within an appropriate local scope.

    I suggest that we let cs32 give a reply before we continue.

    Elysia, kindly do not flame others.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by laserlight View Post
    Use them within an appropriate local scope.

    I suggest that we let cs32 give a reply before we continue.

    Elysia, kindly do not flame others.
    That suggests you can only see variables within scope, which is pretty much what
    you would expect. However I guess you could write a debugger which would record
    the values all variable even when out of scope if you wanted to. So it would depend
    on the particular debugger, however I doubt many take that approach, if any.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by esbo View Post
    However I guess you could write a debugger which would record
    the values all variable even when out of scope if you wanted to. So it would depend
    on the particular debugger, however I doubt many take that approach, if any.
    What would be the reason for that?
    Once they go out of scope, they disappear, so again, what is the point of recording those variables?
    Variables that do not go out of scope is an entirely different thing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Registered User
    Join Date
    Jan 2008
    Posts
    69
    Wow - a lot of activity in this thread. : )

    My question was a purely abstract one. I was interested in knowing exactly what went on behind the scenes when a static variable was used in the context that I presented. The inspiration for my question came from the implementation of the strtok function. All of the answers (except those from esbo, which confused me to no end) were greatly beneficial.

    Quote Originally Posted by brewbuck View Post
    The usual name used to refer to the region where global and static variables are stored is simply "the data segment." This is an area of memory distinct from both the stack and heap, where variables are loaded directly into memory from the program image.

    "Uninitialized" global and static variables, or those which are initialized explicitly to zero, are often placed into yet a different segment called "BSS" which is not contained in the program image. This region is automatically zeroed by the runtime during program startup.
    I've asked this question before, but I'll ask it again to see if I get different answers. How many different segments are there in a typical 32 bit program? It sounds like we have one for the stack/heap (what is this called, BTW? Is this the text segment?), one for global/static variables (data) and one for the compiled code. What else is there?

    Thanks for the great answers.

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't know if I would say the heap/stack is part of any segment.
    Both reside the in the virtual memory, which is part of the physical memory. At least the heap is.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cs32 View Post
    I've asked this question before, but I'll ask it again to see if I get different answers. How many different segments are there in a typical 32 bit program? It sounds like we have one for the stack/heap (what is this called, BTW? Is this the text segment?), one for global/static variables (data) and one for the compiled code. What else is there?
    First, realize that 'segments' are a concept within the executable image, not necessarily in the memory of a running program. Typically there is a text segment which contains the executable code, a data segment which contains initialized data, and a bss segment which contains uninitialized data. The stack and heap are not segments, but are created when the program starts up.

    Some linking formats, for instance ELF, allow you to define as many segments as you want, with whatever names you want. This is useful if you want to control the specific load addresses or VMAs of certain parts of the code. It is not something you would normally encounter. The ELF program header tells the loader how to load these segments into memory.

    Once the program is running, the OS doesn't care about segments anymore. It's all just data and code sitting in memory at that point.

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    First, realize that 'segments' are a concept within the executable image, not necessarily in the memory of a running program. Typically there is a text segment which contains the executable code, a data segment which contains initialized data, and a bss segment which contains uninitialized data. The stack and heap are not segments, but are created when the program starts up.

    Some linking formats, for instance ELF, allow you to define as many segments as you want, with whatever names you want. This is useful if you want to control the specific load addresses or VMAs of certain parts of the code. It is not something you would normally encounter. The ELF program header tells the loader how to load these segments into memory.

    Once the program is running, the OS doesn't care about segments anymore. It's all just data and code sitting in memory at that point.
    I agree with the above, however, the OS will "manage" three or four different sections of memory:
    1. Code - this is normally non-writable. Sometimes called Text.
    2. Data - May be non-executable. [1] Most variables are here.
    3. Read Only Data - non-writable, may be non-executable. For example string constants are here.
    4. Stack - Read-write, expands (towards address zero) as needed to a limit.

    Any segment within the executable file will fall into one of the three first sections.

    The heap is "read-write data", but (normally) not part of the executable, and expands according to the needs of the application. The OS may limit the amount of heap an application can use. The heap is managed as type 2 above, although with special OS calls, you can make heap segments executable for those apps that do "runtime code generation".

    [1] Modern processors have a "No Execute" protection bit in the page-tables that manage the memory.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #27
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by matsp View Post
    [1] Modern processors have a "No Execute" protection bit in the page-tables that manage the memory.
    Speaking of page tables and memory protection, some processors (e.g. x86) also have a memory protection mechanism called "segmentation" which uses segments instead of pages for access control. This shouldn't be confused with the kind of "segments" we are talking about here, although the two things are not entirely unrelated.

    Sometimes segments are referred to as "sections" instead.

  13. #28
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Elysia View Post
    What would be the reason for that?
    Once they go out of scope, they disappear, so again, what is the point of recording those variables?
    Variables that do not go out of scope is an entirely different thing.
    The might give an indication that a particular event has occured which might make it easier
    to debug the program.

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, they are not global variables.
    Variables that go out of scope are destroyed, so there's no no reason to keep their value around.
    You obviously haven't used a debugger very much, so you wouldn't know.

    Problems do not arise from a variable after it's destroyed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #30
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Elysia View Post
    No, they are not global variables.
    Variables that go out of scope are destroyed, so there's no no reason to keep their value around.
    You obviously haven't used a debugger very much, so you wouldn't know.

    Problems do not arise from a variable after it's destroyed.
    1. I never said they were global variables.

    2. Future states of a program may be dependant on previous states.

    3 I have used a debugger but I can manage perfectly well without them,
    futhermore a program may run in debug mode but crash when compilied normally.

    4 Knowing the historical values of variables can be an aid to debugging a program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do i do this? (static structure inside class)
    By 39ster in forum C++ Programming
    Replies: 4
    Last Post: 11-17-2008, 03:14 AM
  2. static object never inits
    By krappa in forum C++ Programming
    Replies: 10
    Last Post: 10-30-2008, 12:04 PM
  3. Static functions.... why?
    By patricio2626 in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2007, 08:06 PM
  4. Resizing a triangle. Why is my code not working?
    By gozu in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2007, 06:40 PM
  5. program to unpack packed data
    By vsk in forum C Programming
    Replies: 5
    Last Post: 11-14-2002, 09:17 PM