Announcement

Collapse
No announcement yet.

address of stack var changes at every run?

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    address of stack var changes at every run?

    this behaviour does not comply with my understanding of "process address space".
    take the following c code:
    Code:
    #include <stdio.h>
    
    int global_var;
    
    int main() {
      int stack_var;
      printf("address of global var...%p\n", &global_var);
      printf("address of stack var....%p\n", &stack_var);
    }
    compile and run it a few times.
    across different runs, i would expect global_var and stack_var to have the same addresses.
    global_var actually does.
    stack_var does not.
    on gnu/linux, the address of stack_var changes at every run.

    can someone please explain me why?
    what is it that makes gnu/linux behave the way it does?

    cheers
    gnu/linux is not windoze

    #2
    Re: address of stack var changes at every run?

    Hi.

    The address of stack_var could change for a number of reasons. Remember that main() is not actually parameter-less. If we assume you are not passing in command line arguments that are changing, keep in mind that the runtime passes in your complete environment. This could potentially be very dynamic between program executions.

    The fundamental reason it can, and probably will change is that there is quite a bit of "stuff" being added to the stack before the code that reserves stack_var's space is ever executed.

    You can find out what is changing by running your program in a debugger, such as gdb, and viewing the stack contents.

    Hope this helps.

    Comment


      #3
      Re: address of stack var changes at every run?

      hi.
      thanks for your reply.

      i run the application from the very same shell (i.e. the very same env) every time.
      i tested on windoze, mac-os-x, solaris and gnu/linux.
      gnu/linux is the only platform on which the address of stack_var changes.
      and not only it changes, it does so at every run.

      given your explanation, i should see this behaviour on all the platforms.
      but on the other platforms the addresses are always the same.
      which is what i'd have expected to see on gnu/linux, as well.

      so gnu/linux does something different from the others...
      something special...

      what you're saying is that gnu/linux puts different stuff at the beginning of the stack each time.
      is it?
      gnu/linux is not windoze

      Comment


        #4
        Re: address of stack var changes at every run?

        This is not an explanation of what is going on, but rather an explanation of why linux doesn't behave like the other OS's. Linux is an AT&T Unix variant. Windoze, OS-X, and Solaris are all BSD Unix variants.

        In fact, if you look at Windoze object code you can see a text message at the top of many files starting "Copyright Regents of the University of Callifornia", or at least you could on Win3 -- XP (I don't know about Vista). OS-X is an acknowledged BSD system and SunOS (which preceded Solaris) was written by Bill Joy, who was a key contributor to BSD. BSD forked from AT&T Unix before microprocessors were invented.

        Comment


          #5
          Re: address of stack var changes at every run?

          Originally posted by askrieger
          In fact, if you look at Windoze object code you can see a text message at the top of many files starting "Copyright Regents of the University of California", or at least you could on Win3 -- XP (I don't know about Vista).
          That's interesting! Of course, I new about the M$ - IBM 'relationship', but I didn't know about this.
          Windows no longer obstructs my view.
          Using Kubuntu Linux since March 23, 2007.
          "It is a capital mistake to theorize before one has data." - Sherlock Holmes

          Comment


            #6
            Re: address of stack var changes at every run?

            Originally posted by askrieger
            ...
            Linux is an AT&T Unix variant. Windoze, OS-X, and Solaris are all BSD Unix variants.
            ...
            alright.
            interesting.
            i'll have to find the docs where the two models are explained in detail.
            thanks mate.
            gnu/linux is not windoze

            Comment

            Working...
            X