Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy doesn't the C program stack grow downward?
    text
    copied!<p>Running the following program on gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 and Intel(R) Core(TM)2 Duo CPU, I want to verify the c program stack grows downward, I write following code:</p> <pre><code> #include &lt;stdio.h&gt; #include &lt;malloc.h&gt; #include &lt;string.h&gt; static int a = 1; static int b; int c = 2; int d; void foo(void) { int *p1; int *p2; int *p3; int *p4; printf("&amp;p1\t%p\n", &amp;p1); printf("&amp;p2\t%p\n", &amp;p2); printf("&amp;p3\t%p\n", &amp;p3); printf("&amp;p4\t%p\n", &amp;p4); } int main() { static int e = 3; static int f; int g = 4; int h; char *str1 = "abc"; char *str2 = "abc"; char *str3; char *str4; printf("&amp;\"abc\"\t%p\n", &amp;"abc"); printf("&amp;str1\t%p\n", &amp;str1); printf("&amp;str2\t%p\n", &amp;str2); printf("str1\t%p\n", str1); printf("str2\t%p\n", str2); printf("&amp;str3\t%p\n", &amp;str3); printf("str3\t%p\n", str3); str4 = (char *)malloc(strlen("abc")*sizeof(char)); printf("&amp;str4\t%p\n", &amp;str4); printf("str4\t%p\n", str4); printf("&amp;g\t%p\n", &amp;g); printf("&amp;h\t%p\n", &amp;h); foo(); return 0; } </code></pre> <p>I get this result:</p> <pre><code> &amp;"abc" 0x8048680 &amp;str1 0xbff1be20 &amp;str2 0xbff1be24 str1 0x8048680 str2 0x8048680 &amp;str3 0xbff1be28 str3 0x8048599 &amp;str4 0xbff1be2c str4 0x950f008 &amp;g 0xbff1be18 &amp;h 0xbff1be1c &amp;p1 0xbff1bde0 &amp;p2 0xbff1bde4 &amp;p3 0xbff1bde8 &amp;p4 0xbff1bdec </code></pre> <p>I find the addresses of str1, str2, str3, str4 grows upward, the addresses of p1, p2, p3, p4 grows upward too, not downward, why?</p>
 

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