Find the answer to your Linux question:
Results 1 to 2 of 2
char *s = "Hello"; // 1 where does this "Hello" will store in memory region, in data segment or stack.. As *s is a pointer of auto variable and we ...
  1. #1
    Just Joined!
    Join Date
    May 2009
    Posts
    21

    regarding variable storage

    char *s = "Hello"; // 1
    where does this "Hello" will store in memory region, in data segment or stack..
    As *s is a pointer of auto variable and we are not allocating memory for that. But we directly assigning value.
    Can anyone clarify this.

    AND

    what about volatile key word. where it will sit in the memory.

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    If you try compiling your program with -S switch it will produce an assembler file so you can see where and what the compiler is doing with your variables. Check out the code examples below:

    test.c C code example
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    __volatile__ int myint = 1234;
    
    void myfunc(void)
    {
    	char *s = "hello, world!\n";
    	fprintf(stdout, "%s\n", s);
    }
    
    int main(int argc, char**argv)
    {
    	myfunc();
    	fprintf(stdout, "myint->%d\n", myint);
    	exit(EXIT_SUCCESS);
    }
    Assembler file generated by gcc -S test.c(I only copied the section that mattered)
    test.s
    Code:
    	.file	"test.c"
    .globl myint
    	.data
    	.align 4
    	.type	myint, @object
    	.size	myint, 4
    myint:
    	.long	1234
    	.section	.rodata
    .LC0:
    	.string	"hello, world!\n"
    .LC1:
    	.string	"%s\n"
    	.text
    You can see that char *s is in the rodata(read only data) section of the code. As for the second question, the volatile keyword informs the compiler that the variable may change unexpectedly so don't perform any optimization on it....Gerard4143
    Make mine Arch Linux

Posting Permissions

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