$30 off During Our Annual Pro Sale. View Details »

Dynamic Memory Allocation in C

Avatar for Madhumita Kedlaya Madhumita Kedlaya
December 30, 2017
68

Dynamic Memory Allocation in C

Avatar for Madhumita Kedlaya

Madhumita Kedlaya

December 30, 2017
Tweet

Transcript

  1. C Memory Management void foo() { // an integer stored

    on the stack int a_stack_integer; // a pointer stored on the stack int *a_stack_pointer; // make a_stack_pointer "point" to integer data that's allocated on the heap a_stack_pointer = (int*)malloc(10 * sizeof(int)); }
  2. Stack and Heap • Stack is scratch space used by

    a function. It’s a LIFO structure (as stacks tend to be) and all local (or automatic) variables are stored here • When a function returns, it’s stack space is no longer maintained and can be reused by other functions on that thread. • Heap is used mainly for dynamic memory allocation and there are no rules for how much memory is used, when it’s allocated and when it’s freed • Usually used by declaring a pointer that holds the address of the heap space being used
  3. Dynamic Memory Allocation • Why use it? • Allows you

    to “dynamically” decide how much memory you want to allocate during runtime • Allows you to modify the amount of memory allocated • Why avoid using it? • Forgetting to free memory makes the memory unavailable for any one else • Management of heap memory is up to the programmer! (This is always a bad idea)
  4. What happens when a pointer is declared? Whenever a pointer

    is declared, all that happens is that C allocates space for the pointer. Using the pointer without allocating memory usually leads to a Segmentation Fault! Or worse The copy actually succeeds! void foo() { // A pointer is born char *p; // I want my pointer to point to a string strcpy(p, “Hello”); }
  5. Proper Memory Usage void foo() { // A pointer is

    born char *p; int len_string = 10; // Allocate memory before usage p = (char *)malloc(len_string * sizeof(char)); // Check if your allocation worked if ( p!=NULL ) { // All good! } // We’re done with this memory, remember to free! free(p); }
  6. Passing Data Around char *foo(char *); void main() { char

    *a = NULL; char *b = NULL; a = foo("Hi there"); b = foo("Goodbye"); printf("From main: %s %s\n", a, b); } char *foo(char *p) { // Stack allocate a char array char q[strlen(p)+1]; strcpy(q, p); printf("From q: the string is %s\n", q); return q; } Array q is Stack allocated inside function foo and will be blown away when foo returns The address of the stack allocated variable is passed back to main and used
  7. (Actually) Passing Data Around char *foo(char *); void main() {

    char *a = NULL; char *b = NULL; a = foo("Hi there"); b = foo("Goodbye"); printf("From main: %s %s\n", a, b); } char *foo(char *p) { char *q = (char *)malloc(strlen(p)+1); strcpy(q, p); printf("From foo: the string is %s\n", q); return q; } Array q is Heap allocated. Ptr q may be blown away but the string sticks around in the heap The variables and b now point to legitimate strings and should print the actual Values • The string allocated in foo sticks around even after the function returns until it is free’d (where should it be free’d?)
  8. Resizing arrays • C does provide a way to resize

    arrays • A thread safe function to either • Increase the size allocated initially or • Allocate a new block of memory with the new size and 
 free the existing allocated block of memory void *realloc(void *oldptr, size_t newsize)
  9. Garbage Collection Why it’s great: • Doesn’t leave memory management

    up to the programmer! • Reduces the chances of double free and memory leaks (when all the pointers to your block of memory are gone and now you can’t free it) • Dangling Pointers are avoided Why it’s not great: • GC can be non deterministic and doesn’t play well with Real Time constraints.
  10. Improving on Malloc • TLSF • jemalloc • dlmalloc (Doug

    Lea's malloc, been around forever) • ptmalloc2 (Doug Lea's malloc, extended to support per- thread arenas. Default allocator for glibc2.3?) • TCMalloc (Google's malloc, claims to be 6x faster than ptmalloc2) • nedmalloc (claims to be faster than tcmalloc) • Hoard, also claims to be very fast