Thread: globals

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490

    globals

    every function starts and ends with some appropriate stack saving commands (at least in the standard calls). the function receives variables and declares local variables in the stack.

    my question is, where do global variables go? is space for it saved in the stack at the beginning? or does it go somewhere else? what about const char* strings?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Global variables are kept in a different segment than the stack. This segment is usually, but not always, referred to as the Data segment. An a.out file is usually described like so (The lowest item being the lowest memory address):
    Code:
    Magic number
    Stack Segment - Data local to functions.
    BSS Segment size - Uninitialized data.
    Data Segment - Global and static variables
    Text Segment - Machine instructions
    -Prelude
    My best code is written with the delete key.

  3. #3
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    Yes, in fact, let's alter this slightly and relable it-- it'll make more sense (We'll ignore 'Magic Number' that's not germane to our context):

    This is your heap zone.

    STACK
    --------
    This is your stack. Highest location in heap, grows down towards Data) It "grows" as functions are called because they leave their variables "on the stack". When the function ends, these variables are removed (stackframe space is reclaimed) and the stack _shrinks_. If the space between the Data segment and the stack isn't large enough to handle this changing "growth", the stack can grow into the data and code segments, corrupting the values there.

    RAW
    ------
    Unused RAM between Stack and Data. Allocations occur here, otherwise the are taken from system ram.

    DATA
    -------
    Global and static variables, constants, text strings.

    CODE (more accurate than 'text')
    -------
    Machine instructions. This is where your code lives, preceded by a short stub and jumptable section. The start of this is the lowest address in your heap zone.
    It is not the spoon that bends, it is you who bends around the spoon.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    >>what about const char* strings?

    I can only speak for windows and what I have seen from peeking into my exes, but most compilers/linkers would put global const char*s into a read only section. Microsoft's Link.exe program (used with MSVC++) labels this section ".idata" by default, and when it is loaded it is mapped into a piece of memory marked readonly.....So at runtime your code can access the contents of this memory, but cant alter them (your compiler will most likely have kicked out any such nonsence before linking anyway)...

    Funny though....if you declare a const int globally, you may find your compiler simple absorbing the value of the int into the opcode itself....

    ala
    Code:
    const int foo = 0xFFFFFFFF;//global
    
    //.....other code
    
    int bar = foo;//disassembly - mov dword ptr [ebp-4],0FFFFFFFFh
    So effectively, foo didnt exist in memory!

    I tried this with declaring foo in the main function and it simply created it as a local variable.....pushed it on the stack....and relied on the checking at compile time to make sure that no piece of code tried to alter that location.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wondering if this would be a dangerous use of globals.
    By suzakugaiden in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 10:51 PM
  2. Globals initialization
    By New++ in forum C++ Programming
    Replies: 3
    Last Post: 12-30-2004, 01:11 PM
  3. avoiding globals
    By joed in forum C Programming
    Replies: 10
    Last Post: 09-05-2004, 10:15 PM
  4. On globals of different modules.
    By BrownB in forum C Programming
    Replies: 1
    Last Post: 07-16-2004, 11:55 AM
  5. globals and splitting up your source
    By cppdude in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2002, 12:57 PM