dynamic storage allocation
play

Dynamic Storage Allocation C s data structures, including arrays, - PDF document

3/27/14 Dynamic Storage Allocation C s data structures, including arrays, are normally fixed in size, i.e., static. Memory Management o Static data structures must have their sizes decided at time of compilation Fortunately, C


  1. 3/27/14 ¡ Dynamic Storage Allocation • C ’ s data structures, including arrays, are normally fixed in size, i.e., static. Memory Management o Static data structures must have their sizes decided at time of compilation • Fortunately, C supports dynamic storage allocation: the ability to allocate storage during program Based on slides from K. N. King and Dianna Xu execution. o used most often for strings, arrays, and structures. Bryn Mawr College o can be linked together to form lists, trees, and other CS246 Programming Paradigm data structures. o done by calling a memory allocation function. The Heap Memory • The pool of memory from which dynamic memory • What is stored in memory? is allocated is separate, and is known as the heap. o Code 0 o Constants • There are library routines to allocate and free o Global and static variables memory from the heap. o Local variables • Heap memory is only accessible through pointers. virtual o Dynamic memory (malloc) address • Mixing statically and dynamically allocated space global int SIZE; memory is not allowed. char* f(void) { local char *c; const SIZE = 10; c = malloc(SIZE); dynamic return c; } 0xffffffff Memory Layout Function Call Mechanism • How is memory organized? • Activation record (of a function call), also known as a stack frame o Code – Text 0 o Constants – Data Text • A block of memory that contains: o Global and static variables – BSS Data o Parameters passed to the function o Dynamic memory (malloc) – Heap BSS o Local variables declared in the function o Local variables – Stack Heap o Return address – pointer to the instruction to be global int SIZE; executed after the function call char* f(void) { local char *c; const SIZE = 10; c = malloc(SIZE); dynamic Stack return c; } 0xffffffff 1 ¡

  2. 3/27/14 ¡ Call Stack A Typical Stack Frame • A call stack is a region of memory that manages • int foo(int arg1, int arg2); activation records • Two local vars • The call stack is initialized with the activation • Stack grows upwards ESP--> record of main Callee saved registers (as needed) • ESP and EBP : temporary storage • Activation record of a function is registers used to local var1 EBP-8 local var2 o Pushed onto the stack at the function call point to the top EBP-4 Caller ’ s EBP Return address o Popped off the stack on return from the call of the stack arg1 EBP+8 and the base arg2 • The reason why local variables are only present EBP+12 Caller saved registers (as needed) during the function call Stack Memory Allocation Functions Null Pointers • The <stdlib.h> header declares three memory • If a memory allocation function can ’ t locate a memory allocation functions: block of the requested size, it returns a null pointer. malloc —Allocates a block of memory but doesn ’ t initialize it. p = malloc(10000); o void *malloc (size_t size); if (p == NULL) { //same as: if (!p) o Allocates a block of size bytes from the heap /* allocation failed; take appropriate action */ } o Returns a pointer to the block allocated o size_t is an unsigned integer type used for very large integers. • A null pointer is a special value that can be o type void * is a “ generic ” pointer. distinguished from all valid pointers. • After we ’ ve stored the function ’ s return value in a calloc —Allocates a block of memory and clears it. pointer variable, we must test to see if it ’ s a null realloc —Resizes a previously allocated block of memory. pointer. • void free (void *ptr); Dynamic Memory Layout Dynamic Memory Layout char *p1 = malloc(3); char *p1 = malloc(3); char *p2 = malloc(4); char *p2 = malloc(4); char *p3 = malloc(1); char *p3 = malloc(1); p1 0 Text 0 Text free(p2); free(p2); char *p4 = malloc(6); char *p4 = malloc(6); Data Data free(p3); free(p3); char *p5 = malloc(2); char *p5 = malloc(2); BSS BSS free(p1); free(p1); free(p4); free(p4); free(p5); Heap free(p5); Heap Stack Stack 0xffffffff 0xffffffff 2 ¡

  3. 3/27/14 ¡ Dynamic Memory Layout Dynamic Memory Layout char *p1 = malloc(3); char *p1 = malloc(3); char *p2 = malloc(4); char *p2 = malloc(4); char *p3 = malloc(1); char *p3 = malloc(1); p1 p1 0 Text 0 Text free(p2); free(p2); char *p4 = malloc(6); char *p4 = malloc(6); p2 p2 Data Data free(p3); free(p3); char *p5 = malloc(2); char *p5 = malloc(2); BSS p3 BSS free(p1); free(p1); free(p4); free(p4); Heap Heap free(p5); free(p5); Stack Stack 0xffffffff 0xffffffff Dynamic Memory Layout Dynamic Memory Layout char *p1 = malloc(3); char *p1 = malloc(3); char *p2 = malloc(4); char *p2 = malloc(4); char *p3 = malloc(1); char *p3 = malloc(1); p1 p1 0 Text 0 Text free(p2); free(p2); char *p4 = malloc(6); char *p4 = malloc(6); p2 p2 Data Data free(p3); free(p3); char *p5 = malloc(2); char *p5 = malloc(2); BSS BSS p3 p3 free(p1); free(p1); p4 free(p4); free(p4); free(p5); Heap free(p5); Heap Stack Stack 0xffffffff 0xffffffff Dynamic Memory Layout Dynamic Memory Layout char *p1 = malloc(3); char *p1 = malloc(3); char *p2 = malloc(4); char *p2 = malloc(4); char *p3 = malloc(1); char *p3 = malloc(1); p1 p1 0 Text 0 Text free(p2); free(p2); char *p4 = malloc(6); p2 char *p4 = malloc(6); p2,p5 Data Data free(p3); free(p3); char *p5 = malloc(2); char *p5 = malloc(2); BSS BSS p3 p3 free(p1); free(p1); p4 p4 free(p4); free(p4); free(p5); Heap free(p5); Heap Stack Stack 0xffffffff 0xffffffff 3 ¡

  4. 3/27/14 ¡ Dynamic Memory Layout Dynamic Memory Layout char *p1 = malloc(3); char *p1 = malloc(3); char *p2 = malloc(4); char *p2 = malloc(4); char *p3 = malloc(1); char *p3 = malloc(1); p1 p1 0 Text 0 Text free(p2); free(p2); char *p4 = malloc(6); char *p4 = malloc(6); p2,p5 p2,p5 Data Data free(p3); free(p3); char *p5 = malloc(2); char *p5 = malloc(2); p3 BSS p3 BSS free(p1); free(p1); p4 p4 free(p4); free(p4); Heap Heap free(p5); free(p5); Stack Stack 0xffffffff 0xffffffff Dynamic Memory Layout Example: String Allocation • Allocate memory for a string of n characters: char *p1 = malloc(3); char * p; char *p2 = malloc(4); p = malloc(n + 1);//adding 1 for ‘\0’ char *p3 = malloc(1); p1 0 Text By default void* will be casted to char* . free(p2); char *p4 = malloc(6); p2,p5 Data free(p3); char *concat(const char *s1, const char *s2){ char *p5 = malloc(2); BSS p3 char *result; free(p1); p4 free(p4); result = malloc(strlen(s1) + strlen(s2) + 1); free(p5); Heap if (result == NULL) { printf("Error: malloc failed in concat\n"); exit(EXIT_FAILURE); } strcpy(result, s1); strcat(result, s2); Stack return result; 0xffffffff } Example: int Array Allocation The calloc Function • Suppose a program needs an array of n integers, • The calloc function is an alternative to malloc . where n is computed during program execution. void *calloc(size_t nmemb, size_t size); o Allocates space for an array with nmemb elements, each of which is size bytes long. int *a; a = (int *) malloc(sizeof(int)*n); o Returns a null pointer if the requested space isn ’ t for (i = 0; i < n; i++) available. a[i] = 0; o Initializes allocated memory by setting all bits to 0. …… free(a); • a = calloc(n, sizeof(int)); //array of n integers. • struct point { int x, y; } *p; p = calloc(1, sizeof(struct point)); 4 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend