Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, everybody, I have a little program as the following: /******************* this is to test satck-layout moscowliuyong(at)hotmail.com ********************/ #include <string.h> #include <stdio.h> int main() { char str[4]; char s[4]; int ...
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Posts
    1

    Question user stack layout Help

    Hi, everybody,

    I have a little program as the following:

    /*******************
    this is to test satck-layout
    moscowliuyong(at)hotmail.com
    ********************/

    #include <string.h>
    #include <stdio.h>

    int main()
    {

    char str[4];
    char s[4];
    int x=1;
    strcpy(s,"12345");
    strcpy(str,s);
    printf("x=%d, \t\tstr=%s, \t\ts=%s\n",x, str,s);
    printf("&x=%p,\tstr->%p,\ts->%p\n",&x,str,s);
    }


    I compiled and got different results in different system:
    on Solaris sytem, the result is not "correct", but understandable.
    on BSD system, the result is correct,but I'm confused bythe real stack layout.

    The results of the program is as the following:

    -bash-3.00$ cat solaris.output
    x=889192449, str=12345, s=5
    &x=ffbffce8, str->ffbffce0, s->ffbffce4

    -bash-3.00$ cat bsd.output
    x=1, str=12345, s=12345
    &x=0x7fffffffebcc, str->0x7fffffffebe0, s->0x7fffffffebd0

    My question is: where the string s is stored??

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    try compiling your program with -S and look at the assembled code it will show you where the char s[4] is stored. This said, char s[4] should be stored on the stack...Hope this helps

    gcc -S test.c

  3. #3
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Also try compiling the program with -g and check it in the gdb(gnu debugger)

    gcc -g -o test test.c

    here's a link to a gdb tutorial, just in case you never used gdb

    Peter's gdb Tutorial: Table Of Contents

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...