Thread: Stack order issue

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    Stack order issue

    Using the following code:
    Code:
    {
    	int a,b;
    	int Loop;
    	int c;
    I find the following facts (in release mode):
    &a: 0012FED4
    &b: 0012FED8
    &Loop: 0012FED0
    &c: 0012FEDC
    I find it odd the way the variables are positioned in memory. Shouldn't the address of 'c' be 4 less than the address of 'Loop'? Ie, shouldn't 'c' have been pushed onto the stack after 'Loop'?

    EDIT: Also, would I be correct in saying that the order they were pushed onto the stack was 'c','b','a','Loop'?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Do you use them at all, or just declare them? Perhaps they also look at the frequency in which you use a variable before determining which one get's pushed on first.

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Yeah I guess the compiler does its own thing.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I find the following facts (in release mode):
    The only time you can compare pointers is when you have two pointers to different elements of the same array, or different elements of the same block of allocated memory.

    So taking any pair of local variables and trying to figure out a 'before' or 'after' relationship is simply a curiosity exercise. Ordering and padding are both very specific to the compiler.

    By the time the compiler optimiser has finished, it may no longer exist as a variable, only as a register.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  2. Need Help with Stack
    By trongsi in forum C++ Programming
    Replies: 9
    Last Post: 05-23-2006, 04:14 PM
  3. Stacks
    By Cmuppet in forum C Programming
    Replies: 19
    Last Post: 10-13-2004, 02:32 PM
  4. stack implementation problem-help needed
    By sanju in forum C Programming
    Replies: 1
    Last Post: 12-10-2002, 07:29 AM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM