Dynamic Memory management in c

memory management algorithms and implementation in c/c++ and memory management in c interview questions
Dr.MasonHanks Profile Pic
Dr.MasonHanks,Germany,Teacher
Published Date:23-07-2017
Your Website URL(Optional)
Comment
CIS 190: C/C++ Programming Lecture 3 Memory Management in C 1 Any Questions? 2 Outline • (from last class) Testing • Memory allocation • Memory errors • Errors • Debugging • Homeworks 3 Testing • unit testing – literal tests to make sure code works as intended – e.g., TwoPlusTwoEqualFour(...) for an Addition() function • edge case testing (or corner case, etc.) – ensure that code performs correctly with all (or at least many) possible input values – e.g., prevent program from accepting invalid input 4 Simple Testing Example / get month from user in integer form / printf(“Please enter month: “); scanf(“%d”, &month); 5 Simple Testing Example / get month from user in integer form / printf(“Please enter month: “); scanf(“%d”, &month); while (month JAN_INT month DEC_INT) scanf(“%d”, &month); 6 Simple Testing Example / get month from user in integer form / printf(“Please enter month: “); scanf(“%d”, &month); while (month JAN_INT month DEC_INT) printf(“\n%d is an invalid month”, month); printf(“please enter between %d and %d:”, JAN_INT, DEC_INT); scanf(“%d”, &month); 7 / print string up to number given by length (or full string, whichever is reached first) / void PrintToLength(char str, int length) int i; for (i = 0; i length; i++) printf(“%c”, stri); 8 Common Edge Cases • C-style string – empty string – pointer to NULL – without the \0 terminator • Integer – zero – negative/positive – below/above the min/max 9 Outline • (from last class) Testing • Memory allocation • Memory errors • Errors • Debugging • Homeworks 10 Memory • each process gets its own memory chunk, or address space 0x000000 Stack Function calls, locals Dynamically 4 GB Heap allocated address memory space “data segment” Global/static vars “code segment” Code 0xFFFFFFF 11 Memory • each process gets its own memory chunk, or address space 0x000000 Stack 4 GB Heap address space Global/static vars Code 0xFFFFFFF 12 Stack Allocation • memory allocated by the program as it runs – local variables – function calls Stack • fixed at compile time 13 Heap Allocation • dynamic memory allocation – memory allocated at run-time Heap • two options for allocating memory: –malloc() –calloc() • both require include stdlib.h to work 14 malloc() void malloc ( size to be allocated ) char letters; letters = (char) malloc(userVariable sizeof(char)); • malloc returns a pointer to a contiguous block memory of the size requested 15 calloc() void calloc ( number of elements, size of type ) float grades; grades = (float) calloc(userVariable, sizeof(float)); • calloc works very similarly to malloc, but it initializes all the allocated bits to zero − takes longer than malloc, so only use if needed 16 Casting Allocated Memory • both calloc() and malloc() return a pointer of type void, so you must cast the memory to match the given type letters = (char) malloc(userVariable sizeof(char)); grades = (float) calloc(userVariable, sizeof(float)); 17 Casting Allocated Memory • both calloc() and malloc() return a pointer of type void, so you must cast the memory to match the given type letters = (char) malloc(userVariable sizeof(char)); grades = (float) calloc(userVariable, sizeof(float)); 18 Handling Allocated Memory • IMPORTANT: before using allocated memory make sure it’s actually been allocated • if memory wasn’t correctly allocated, the address that is returned will be null − this means there isn’t a contiguous block of memory large enough to handle request 19 Exiting in Case of NULL • if the address returned is null, your program should exit − exit() takes an integer value − non-zero values are used as error codes if (grades == NULL) printf(“Memory not allocated, exiting.\n”); exit(-1); 20