Programcode&constants1int main(void){2char *string="hello";Stack3printf("\"hellol"=%p\n","hello");4printf("string pointer = %pln", string);5string[4] = 0';6printf("Go to %sln",string);7return ;Heap8子Question #1. What are the printouts from Line3&4?Data Segment&BSS"hello"=0x8048520String pointer=0x8048520CodeQuestion#2. What is the printout from Line 6?ConstantSegmentation fault11
Program code & constants • Question #1. What are the printouts from Line 3 & 4? • Question #2. What is the printout from Line 6? 11 1 int main(void) { 2 char *string = "hello"; 3 printf("\"hello\" = %p\n", "hello"); 4 printf("String pointer = %p\n", string); 5 string[4] = '\0'; 6 printf("Go to %s\n", string); 7 return 0; 8 } Data Segment & BSS Heap Code + Constant Stack Segmentation fault "hello" = 0x8048520 String pointer = 0x8048520
Program code & constants1int main(void)2char *string ="hello";Stack3printf(""hello"=%p\n","hello");4printf("string pointer = %pln", string);5string[4]=e';6printf("Go to %sln"string)7return o;Heap8子Constants are stored in code segment. Accessing of constants are done usingData Segment&BSSaddresses (or pointers)CodeCodes and constants are both read-onlyConstant12
Program code & constants 12 • Constants are stored in code segment. – Accessing of constants are done using addresses (or pointers). • Codes and constants are both read-only. Data Segment & BSS Heap Code + Constant Stack 1 int main(void) { 2 char *string = "hello"; 3 printf("\"hello\" = %p\n", "hello"); 4 printf("String pointer = %p\n", string); 5 string[4] = '\0'; 6 printf("Go to %s\n", string); 7 return 0; 8 }
User-space memory managementAddress space;Code & constants;Data segment;Stack;Heap;Segmentation fault:13
13 User-space memory management - Address space; - Code & constants; - Data segment; - Stack; - Heap; - Segmentation fault;
DataSegment&BsS-propertiesint global int = 10;int main(void){Stackint local int = 1o;static int static int = 1o;printf("local_intaddr =%p\n",&local_int );&static_int);printf("staticintaddr=%pln",printf("global_int addr =%pln",&global_int);Heapreturn o;子They are stored next$./globalvs_staticto each other.local_intaddr = xbf8bb8acData Segmentstatic int addr = 0x804a018&BSSThis implies that theyglobal_int addr =0x804a014areinthesame$segment!CodeConstantNote:Astatic variable istreated as thesame as a global variable!14
Data Segment & BSS – properties 14 int global_int = 10; int main(void) { int local_int = 10; static int static_int = 10; printf("local_int addr = %p\n", &local_int ); printf("static_int addr = %p\n", &static_int ); printf("global_int addr = %p\n", &global_int ); return 0; } $ ./global_vs_static local_int addr = 0xbf8bb8ac static_int addr = 0x804a018 global_int addr = 0x804a014 $_ They are stored next to each other. This implies that they are in the same segment! Data Segment & BSS Heap Code + Constant Stack Note: A static variable is treated as the same as a global variable!
DataSegment&BsS-properties·Data- Containing initialized global and staticStackvariables.HeapBSS (Block Started by Symbol)- Containing uninitialized global andData Segmentstatic variables.&BSSCodeConstant15
Data Segment & BSS – properties 15 • Data – Containing initialized global and static variables. • BSS (Block Started by Symbol) – Containing uninitialized global and static variables. Data Segment & BSS Heap Code + Constant Stack