Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic Memory Allocation

Similar presentations


Presentation on theme: "Dynamic Memory Allocation"— Presentation transcript:

1 Dynamic Memory Allocation
In this diagram of a program’s memory, the heap is a pool of available memory that can be allocated dynamically while your program is running. Whereas a function's local (i.e. stack) variables disappear when the function returns, memory allocated from the heap will persist until it is explicitly given back to the operating system.

2 A call to malloc() prototype: void* malloc(size in bytes); example:
The C library provides a function called malloc() (short for memory allocation) that allocates a block of memory from the heap and returns a pointer to it. Whenever your program needs fresh storage at run time that must outlast the current function, it can call malloc(), passing it the number of bytes needed. Since the sizes of data types vary from system to system, C provides the sizeof() operator for determining the size of a particular type. In this example, sizeof(int) * 10 evaluates to 40 bytes on the appliance, so we are asking the OS for a pointer to 40 bytes of memory. **** Note that sizeof() is considered an operator not a function because it takes any kind of data type and returns the appropriate size, whereas a function must specify what type of argument it takes. **** prototype: void* malloc(size in bytes); example: int* ptr = malloc(sizeof(int) * 10);

3 Check for NULL! int* ptr = malloc(sizeof(int) * 10); if (ptr == NULL)
Normally malloc() returns a pointer to a block of newly allocated memory from the heap. However, if there is an error, or the heap has been exhausted, malloc() returns NULL. For this reason, ALWAYS CHECK THE POINTER RETURNED BY MALLOC!! If the returned pointer is NULL, you must handle this case by signaling an error. Programs that don't perform this check can dereference NULL, which results in a segmentation fault. int* ptr = malloc(sizeof(int) * 10); if (ptr == NULL) { printf("Error -- out of memory.\n"); return 1; }

4 A call to free() prototype: void free(pointer to heap memory);
When a program has finished using heap memory, it should release the storage by calling free() on the pointer that was originally produced by malloc(). All memory that is malloc'd must later be free'd (but only once), and only memory that was produced by malloc() should be free'd. prototype: void free(pointer to heap memory); example: free(ptr);

5 #include <stdio.h> #include <cs50.h> int main(void) {
int* ptr = malloc(sizeof(int)); if (ptr == NULL) printf("Error -- out of memory.\n"); return 1; } *ptr = GetInt(); printf("You entered %d.\n", *ptr); free(ptr); Let's walk through this program: First, we ask the OS for a pointer to 4 bytes of memory in the heap. Next, we check to make sure malloc() did not return NULL. We then get an integer from the user, store it in the memory that malloc() gave us, and print the integer. Finally, we free the malloc'd memory.


Download ppt "Dynamic Memory Allocation"

Similar presentations


Ads by Google