The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #8 (permalink)  
Old 11-14-2007
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,886
It's the stack

I believe variables are usually allocated onto the stack in reverse order. So first a 4-byte allocation for the pointer to k, then maybe a word padding (for optimization with 64-bit platforms), then ... in the first example, a 16-byte boundry for j. Then another 4- or 8-byte boundary for i. But when I say reverse-order, I mean they are "pushed" "down" the stack. But they are read "up" the stack. So that j[0] is very close to i.

This might help explain going on, as you can imagine uninitialized parts of the stack having different values depending on where in the stack they are located. Try adding a function that has an "interesting" stack and calling this new function just before your function is called. You might experience different results each time.