Dynamic Memory Allocation

Views:
 
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

Dynamic Memory Allocation : 

Dynamic Memory Allocation Dept. of IT, RGUKT, Basara

Intro : 

Intro C language requires the number of elements in an array to be specified at compile time. Often leads to wastage or memory space or program failure. Dynamic Memory Allocation Memory space required can be specified at the time of execution. C supports allocating and freeing memory dynamically using library routines.

Memory Allocation Process in C : 

Memory Allocation Process in C Local variables Free memory Global variables Instructions Permanent storage area Stack Heap

Memory Allocation Process in C : 

Memory Allocation Process in C The program instructions and the global variables are stored in a region known as permanent storage area. The local variables are stored in another area called stack. The memory space between these two areas is available for dynamic allocation during execution of the program. This free region is called the heap. The size of the heap keeps changing

Memory Layout : 

Memory Layout How is memory organized? Text=code, constant data

Memory Layout : 

Memory Layout How is memory organized? Text=code, constant data Data = initialized global and static variables

Memory Layout : 

Memory Layout How is memory organized? Text=code, constant data Data = initialized global and static variables BSS = (Block Started by Symbol)

Memory Layout : 

Memory Layout How is memory organized? Text=code, constant data Data = initialized global and static variables BSS = (Block Started by Symbol) Heap = dynamic memory

Memory Layout : 

Memory Layout How is memory organized? Text=code, constant data Data = initialized global and static variables BSS = (Block Started by Symbol) Heap = dynamic memory Stack = local variables

Memory Layout : 

Memory Layout

Overview of memory management : 

Overview of memory management 11 Stack-allocated memory When a function is called, memory is allocated for all of its parameters and local variables. Each active function call has memory on the stack (with the current function call on top) When a function call terminates, the memory is deallocated (“freed up”) Ex: main() calls f(), f() calls g() g() recursively calls g() main() f() g() g()

Memory Allocation : 

Memory Allocation How is memory allocated? Global and static variables = program startup

Memory Allocation : 

Memory Allocation How is memory allocated? Global and static variables = program startup Local variables = function call

Memory Allocation : 

Memory Allocation How is memory allocated? Global and static variables = program startup Local variables = function call Dynamic memory = malloc()

Memory Allocation : 

Memory Allocation

Allocating new heap memory : 

Allocating new heap memory CS 3090: Safety Critical Programming in C 16 void *malloc(size_t size); Allocate a block of size bytes, return a pointer to the block (NULL if unable to allocate block) void *calloc(size_t num_elements, size_t element_size); Allocate a block of num_elements * element_size bytes, initialize every byte to zero, return pointer to the block (NULL if unable to allocate block)

Allocating new heap memory : 

Allocating new heap memory void *realloc(void *ptr, size_t new_size); Given a previously allocated block starting at ptr, change the block size to new_size, return pointer to resized block If block size is increased, contents of old block may be copied to a completely different region In this case, the pointer returned will be different from the ptr argument, and ptr will no longer point to a valid memory region If ptr is NULL, realloc is identical to malloc Note: may need to cast return value of malloc/calloc/realloc: char *p = (char *) malloc(BUFFER_SIZE);

Memory deallocation : 

Memory deallocation How is memory deallocated? Global and static variables = program finish

Memory deallocation : 

Memory deallocation How is memory deallocated? Global and static variables = program finish Local variables = function return

Memory deallocation : 

Memory deallocation How is memory deallocated? Global and static variables = program finish Local variables = function return Dynamic memory = free()

Memory deallocation : 

Memory deallocation How is memory deallocated? Global and static variables = program finish Local variables = function return Dynamic memory = free() All memory is deallocated at program termination

Memory deallocation : 

Memory deallocation How is memory deallocated? Global and static variables = program finish Local variables = function return Dynamic memory = free() All memory is deallocated at program termination It is good style to free allocated memory anyway

Memory deallocation : 

Memory deallocation

Deallocating heap memory : 

Deallocating heap memory void free(void *pointer); Given a pointer to previously allocated memory, put the region back in the heap of unallocated memory Note: easy to forget to free memory when no longer needed... especially if you’re used to a language with “garbage collection” like Java This is the source of the notorious “memory leak” problem Difficult to trace – the program will run fine for some time, until suddenly there is no more memory!

Checking for successful allocation : 

Checking for successful allocation Call to malloc might fail to allocate memory, if there’s not enough available Easy to forget this check, annoying to have to do it every time malloc is called... Click here to Visualize

Dynamic Memory : 

Dynamic Memory Code Execution: … start with dog(); Memory: char *cat(char *x, int i) { static int r = 4; x[3] = 'h'; char *result = (char *)malloc(20); sprintf(result, "%s x %d", x, i); return result; } void dog() { char *s = "my cat"; int z = 12; cat(s, z);}

Dynamic Memory : 

Dynamic Memory Code Execution: Memory: char *cat(char *x, int i) { static int r = 4; x[3] = 'h'; char *result = (char *)malloc(20); sprintf(result, "%s x %d", x, i); return result; } void dog() { char *s = "my cat"; int z = 12; cat(s, z);}

Dynamic Memory : 

Dynamic Memory Code Execution: Memory: char *cat(char *x, int i) { static int r = 4; x[3] = 'h'; char *result = (char *)malloc(20); sprintf(result, "%s x %d", x, i); return result; } void dog() { char *s = "my cat"; int z = 12; cat(s, z);} *s “my cat”

Dynamic Memory : 

Dynamic Memory Code Execution: Memory: char *cat(char *x, int i) { static int r = 4; x[3] = 'h'; char *result = (char *)malloc(20); sprintf(result, "%s x %d", x, i); return result; } void dog() { char *s = "my cat"; int z = 12; cat(s, z);} *s “my cat” z = 12

Dynamic Memory : 

Dynamic Memory Code Execution: Memory: char *cat(char *x, int i) { static int r = 4; x[3] = 'h'; char *result = (char *)malloc(20); sprintf(result, "%s x %d", x, i); return result; } void dog() { char *s = "my cat"; int z = 12; cat(s, z);} *s “my cat” z = 12 *x i = 12 cat() Stack Frame dog() Stack Frame

Dynamic Memory : 

Dynamic Memory Code Execution: Memory: char *cat(char *x, int i) { static int r = 4; x[3] = 'h'; char *result = (char *)malloc(20); sprintf(result, "%s x %d", x, i); return result; } void dog() { char *s = "my cat"; int z = 12; cat(s, z);} *s “my cat” z = 12 *x i = 12 r = 4 cat() Stack Frame dog() Stack Frame Static Memory

Dynamic Memory : 

Dynamic Memory Code Execution: Memory: char *cat(char *x, int i) { static int r = 4; x[3] = 'h'; char *result = (char *)malloc(20); sprintf(result, "%s x %d", x, i); return result; } void dog() { char *s = "my cat"; int z = 12; cat(s, z);} *s “my hat” z = 12 *x i = 12 r = 4 cat() Stack Frame dog() Stack Frame Static Memory

Dynamic Memory : 

Dynamic Memory Code Execution: Memory: char *cat(char *x, int i) { static int r = 4; x[3] = 'h'; char *result = (char *)malloc(20); sprintf(result, "%s x %d", x, i); return result; } void dog() { char *s = "my cat"; int z = 12; cat(s, z);} *s “my hat” z = 12 *x i = 12 r = 4 cat() Stack Frame dog() Stack Frame Static Memory *result 20 bytes Heap Memory

Dynamic Memory : 

Dynamic Memory Code Execution: Memory: char *cat(char *x, int i) { static int r = 4; x[3] = 'h'; char *result = (char *)malloc(20); printf(result, "%s x %d", x, i); return result; } void dog() { char *s = "my cat"; int z = 12; cat(s, z);} *s “my hat” z = 12 *x i = 12 r = 4 cat() Stack Frame dog() Stack Frame Static Memory *result “my hat x 12” Heap Memory

Dynamic Memory : 

Dynamic Memory Code Execution: Memory: char *cat(char *x, int i) { static int r = 4; x[3] = 'h'; char *result = (char *)malloc(20); sprintf(result, "%s x %d", x, i); return result; } void dog() { char *s = "my cat"; int z = 12; cat(s, z);} *s “my hat” z = 12 r = 4 dog() Stack Frame Static Memory “my hat x 12” Heap Memory

Dynamic Memory : 

Dynamic Memory Code Execution: Memory: char *cat(char *x, int i) { static int r = 4; x[3] = 'h'; char *result = (char *)malloc(20); sprintf(result, "%s x %d", x, i); return result; } void dog() { char *s = "my cat"; int z = 12; cat(s, z);}

Slide 37: 

int main (void) { char word[MAXWORD]; char * w[N]; /* an array of pointers */ int i, n; /* n: no of words to sort */ for (i=0; scanf(“%s”, word) == 1); ++i) { w[i] = calloc (strlen(word)+1, sizeof(char)); if (w[i] == NULL) exit(0); strcpy (w[i], word) ; } n = i; sortwords (w, n) ; wrt_words (w, n); return 0; }

Slide 38: 

w 0 1 2 3 17 Input : A is for apple or alphabet pie which all get a slice of come taste it and try A \0 i s \0 f o r \0 a p p l e \0 t r y \0

Slide 39: 

39 void sort_words (char *w[], int n) { int i, j; for (i=0; i<n; ++i) for (j=i+1; j<n; ++j) if (strcmp(w[i], w[j]) > 0) swap (&w[i], &w[j]) ; } void swap (char **p, char **q) { char *tmp ; tmp = *p; *p = *q; *q = tmp; }

Slide 40: 

w w[i] f o r \0 a p p l e \0 Before swapping w[j]

Slide 41: 

w w[i] f o r \0 a p p l e \0 After swapping w[j]

Memory errors : 

Memory errors 42 Using memory that you have not initialized Using memory that you do not own Using more memory than you have allocated Using faulty heap memory management

authorStream Live Help