Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>if I have an array allocated on the stack a pointer to the first element will also be lower > in value than a pointer to the second element ? </p> </blockquote> <p>It is not important "how" you allocate the array, you can increase or decrease the stack pointer, but as result you have address space reserved for the array.</p> <p>You can work with them in normal way, since the lowest adress is reserved for element 0.</p> <blockquote> <p>so my question is what is the correct memory layout for a process in Linux ? </p> </blockquote> <p>You can check it yourself. Insert somewhere into you program something like <code>std::cin.get()</code> to pause your program.</p> <p>Then run in a separate shell:</p> <pre><code>ps aux | grep your_program_name cat /proc/&lt;pid show by grep&gt;/maps </code></pre> <p>This prints the memory mappings of your process, where you can see where the heap, the stack and other things are placed in memory.</p> <p>About the stack: let's assume that you have ordinary machine with Linux and Intel or AMD 64 bit CPU. Then write the following code:</p> <pre><code>extern void f(int); void g(int param) { f(param); } </code></pre> <p>compile it and disassemble:</p> <pre><code>g++ -ggdb -c test_my_stack.cc &amp;&amp; objdump -S test_my_stack.o </code></pre> <p>you can see (unimportant details removed): </p> <pre><code> void g(int param) { 0: 55 push %rbp 1: 48 89 e5 mov %rsp,%rbp 4: 48 83 ec 10 sub $0x10,%rsp 8: 89 7d fc mov %edi,-0x4(%rbp) f(param); b: 8b 45 fc mov -0x4(%rbp),%eax </code></pre> <p>as you can see in <code>sub $0x10,%rsp</code> we reserved space in the stack by decreasing (moving down) the stack pointer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload