AddressspaceHow does a programmerlook at the memory space?Stack - Local variablesAn array of bytes?Heap -DynamicallyMemoryofaprocessisallocated memorydivided into segmentsData Segment & BsSThis way of arrangingGlobal and staticmemoryis calledvariablessegmentationCode + Constant6
Address space 6 Data Segment & BSS – Global and static variables Heap – Dynamically allocated memory Code + Constant Stack - Local variables How does a programmer look at the memory space? - An array of bytes? - Memory of a process is divided into segments - This way of arranging memory is called segmentation
Addressspaceintmain(void)(int *malloc ptr=malloc(4);What is theprocess address space?char *constant ptr -"hello";printf("Localvariable=%15pln,&malloc_ptr);printf("malloc()space%15p/n,malloc_ptr);Increasingprintf("Global variable=%15pn",&global int):printf("Code &constant-%15pn",constant ptr);addressStack - Local variablesreturn o;$/addrHeap -DynamicallyLocal variable=0xbfa8938callocated memorymalloc()space0x915c008-Global variable=0x804a0200x8048550Code &constant=Data Segment &BssGlobal and staticvariablesNoteTheaddresses are not necessarilytheCode + Constantsamein different processes7
Address space 7 Data Segment & BSS – Global and static variables Heap – Dynamically allocated memory Code + Constant Stack - Local variables Increasing address $ ./addr Local variable = 0xbfa8938c malloc() space = 0x915c008 Global variable = 0x804a020 Code & constant = 0x8048550 $ _ Note The addresses are not necessarily the same in different processes What is the process address space?
AddressspaceHowlargeistheaddress space?oxf=111111bit+16'bitsIncreasingoxffffffff0x100000000addressStack - Local variables2^324GHeap-Dynamicallyallocated memoryIna32-bitsystem,OneaddressmapstoonebyteThe maximum amount of memoryDataSegment &Bssin a process is 4GB.Global and staticNotevariablesThisis the so called logical addressspaceCode + ConstantEachprocesshasitsownaddressspace, and it can reside in any partof the physical memory8
8 Address space Data Segment & BSS – Global and static variables Heap – Dynamically allocated memory Code + Constant Stack - Local variables 0xffffffff = 0x100000000 - 1 1 ‘1’ bit + 16 ‘0’ bits = 2^32 - 1 = 4G - 1 0xf = 1111 In a 32-bit system, - One address maps to one byte. - The maximum amount of memory in a process is 4GB. Increasing address Note - This is the so called logical address space - Each process has its own address space, and it can reside in any part of the physical memory How large is the address space?
User-space memory managementAddress space; Code & constants;Data segment;Stack;Heap;Segmentation fault:9
9 User-space memory management - Address space; - Code & constants; - Data segment; - Stack; - Heap; - Segmentation fault;
Program code&constants·A program is an executable fileStackAprocessisnotboundedtooneprogram codeHeap- Remember exec*() family?Data SegmentThe program code requires&BSSmemory space because...- The CPU needs to fetch theCodeConstantinstructions from the memoryforexecution.(intel)instructionCore"i710
Program code & constants • A program is an executable file • A process is not bounded to one program code. – Remember exec*() family? • The program code requires memory space because. – The CPU needs to fetch the instructions from the memory for execution. 10 Data Segment & BSS Heap Code + Constant Stack instruction