Created
March 6, 2015 03:45
-
-
Save savanovich/2197331fff25e6f1363f to your computer and use it in GitHub Desktop.
C Principals: Local string literal vs local value on stack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int* foo(void); | |
int* foo2(void); | |
char* bar(void); | |
char* bar2(void); | |
int main(int argc, char* argv[]) | |
{ | |
int *m = foo(); | |
int *m2 = foo2(); | |
printf("Printing local value of function foo = %d\n", *m); | |
printf("Printing local value of function foo2 = %d\n", *m2); | |
char *m3 = bar(); | |
char *m4 = bar2(); | |
printf("Printing local value of function bar = %s\n", m3); | |
printf("Printing local value of function bar2 = %s\n", m4); | |
return 0; | |
} | |
int* foo(void) | |
{ | |
int i = 10; /* ON STACK!!! */ | |
return &i; \ | |
} | |
int* foo2(void) | |
{ | |
int i = 12345; /* ON STACK!!! */ | |
return &i; \ | |
} | |
char* bar(void) | |
{ | |
char* str = "STRING"; /* string literal from RO section */ | |
/* .section .rodata */ | |
/* .LC4: */ | |
/* .string "STRING" */ | |
return str; | |
} | |
char* bar2(void) | |
{ | |
char* str = "foobar2"; /* string literal from RO section */ | |
return str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment