Heap memory remains allocated when a function returns. Stack memory becomes invalid when the function returns. malloc(…) allocates memory on the heap. - malloc(…) takes one argument: the # of bytes to allocate. - malloc(…) returns the address of the first byte in that allocation block. - "allocation block" means a block of memory allocated by one call to malloc(…). - malloc(…) a void* which means "address of anything". sizeof(v) gives you the number of bytes necessary to allocate a variable of the same type as v. USE THIS WAY ONLY. sizeof(int) gives you the number of bytes necessary to allocate an int. DO NOT USE THIS (except for very rare demonstration code, such as in lectures when talking about C itself). free(…) deallocates (="frees") a single heap allocation block. - free(…) takes one argument: the address of the allocation block to deallocate (="free"). - You MUST call free once for every call to malloc(…). - Do not attempt to access that memory after it has been freed. Heap memory remains allocated until it is explicitly deallocated (="freed"). WARNING: Do not use a typecast with malloc(…). __________________________________________________________________________ PITFALLS ______ BAD: int* a_n = (int*) malloc(sizeof(*a_n)); // BAD!!! ‾‾‾‾‾‾ Argument to malloc(…) must be expressed in terms of sizeof(…). DO NOT USE TYPECASTS unless you are 100% sure why you need one. This will be very rare in this class. ∙ printf("%p", (void*)a_n); // typecast is necessary here because of how printf(…) works. TYPECASTS WILL CAUSE YOU HELL (except for those rare circumstances). int* a_n = malloc(sizeof(a_n)); // BAD!!!!! int* a_n = malloc(sizeof(*a_n)); // DON'T forget the ASTERISK free(*a_n); // BAD free(a_n); // ADDRESS