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)); }
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
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)
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”); }
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); }
*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
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?)
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)
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.
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