Lecture%2008%20-%20%20Dealing%20with%20Dynamic%20Memory -...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Copyright @ 2009 Ananda Gunawardena Lecture 08 Dynamic Memory Allocation In this lecture Dynamic allocation of memory malloc, calloc and realloc Memory Leaks and Valgrind Heap variables versus stack variables Revisiting * and ** Case for Dynamic Variables Examples Further Readings Exercises C does not have automatic garbage collection like Java does. Therefore a C programmer must manage all dynamic memory used during the program execution. The <stdlib.h> provides four functions that can be used to manage dynamic memory. NAME calloc, malloc, free, realloc - Allocate and free dynamic memory SYNOPSIS #include <stdlib.h> void *calloc(size_t nmemb, size_t size); void *malloc(size_t size); void free(void *ptr); void *realloc(void *ptr, size_t size); DESCRIPTION calloc() allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allo- cated memory. The memory is set to zero. malloc() allocates size bytes and returns a pointer to the allocated memory. The memory is not cleared. free() frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behaviour occurs. If ptr is NULL, no operation is performed. realloc() changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged to the minimum of the old and new sizes; newly allocated memory will be uninitialized. If ptr is NULL, the call is equivalent to malloc(size); if size is equal to zero, the call is equivalent to free(ptr). Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc().
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The malloc function The malloc function allocates a memory block of size n bytes (size_t is equivalent to an unsigned integer) The malloc function returns a pointer or void* to that block of memory. That void* pointer can be used for any pointer type. malloc allocates a contiguous block of memory. If enough contiguous memory is not available, then malloc returns NULL. Therefore always check to make sure memory allocation was successful by using void* p; if ((p=malloc(n)) == NULL) return 1; else { /* memory is allocated */} Example: if we need an array of n ints, then we can do int* A = malloc(n*sizeof(int)); A holds the address of the first element of this block of 4n bytes, and A can be used as an array. For example, if (A != NULL) for (i=0;i<n;i++) A[i] = 0; will initialize all elements in the array to 0. We note that A[i] is the content at address (A+i). Therefore we can also write for (i=0;i<n;i++) *(A+i) = 0; Recall that A points to the first byte in the block and A+I points to the address of the ith element in the list. That is &A[i]. We can also see the operator [] is equivalent to doing
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/27/2009 for the course CS 123 taught by Professor Bajkzek during the Fall '08 term at Carnegie Mellon.

Page1 / 14

Lecture%2008%20-%20%20Dealing%20with%20Dynamic%20Memory -...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online