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 ...
- 08-01-2009 #1Just 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.
- 08-01-2009 #2
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
Assembler file generated by gcc -S test.c(I only copied the section that mattered)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); }
test.s
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....Gerard4143Code:.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
Make mine Arch Linux


Reply With Quote