Lecture 08 - Dealing With Dynamic Memory
Lecture 08 - Dealing With Dynamic Memory
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
SYNOPSIS
#include <stdlib.h>
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.
void* p;
if ((p=malloc(n)) == NULL)
return 1;
else
{ /* memory is allocated */}
int* A = malloc(n*sizeof(int));
if (A != NULL)
for (i=0;i<n;i++)
A[i] = 0;
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
pointer arithmetic to obtain the content of the address.
free(A);
will cause the program to give back the block to the heap
(or free memory). The argument to free is any address that
was returned by a prior call to malloc. If free is applied
to a location that has been freed before, a double free
memory error may occur. We note that malloc returns a block
of void* and therefore can be assigned to any type.
double* A = (double*)malloc(n);
int* B = (int*)malloc(n);
char* C = (char*)malloc(n);
calloc(n, sizeof(int))
is equivalent to
Exercise:
Write realloc function using malloc and free
int A[n],i=0;
for (i=0;i<n;i++)
A[i] = random();
int* B = malloc(2*n);
B = A;
int* A = malloc(4*n);
int *B = A;
free(B);
% man valgrind
NAME
valgrind - a suite of tools for debugging and profiling programs
SYNOPSIS
valgrind [valgrind options] your-program [your-program options]
DESCRIPTION
valgrind is a flexible program for debugging and profiling Linux exe-
cutables. It consists of a core, which provides a synthetic CPU in
software, and a series of "tools", each of which is a debugging or pro-
filing tool. The architecture is modular, so that new tools can be cre-
ated easily and without disturbing the existing structure.
This manual page covers only basic usage and options. Please see the
HTML documentation for more comprehensive information.
INVOCATION
valgrind is typically invoked as follows:
This runs program (with arguments args) under valgrind using the mem-
check tool. memcheck performs a range of memory-checking functions,
including detecting accesses to uninitialized memory, misuse of allo-
cated memory (double frees, access after free, etc.) and detecting mem-
ory leaks.
and more……….
int x;
int* A = malloc(100);
Revisiting * and **
char name[30]=”guna\0”;
int num = 23;
int* intptr = #
char* charptr = name;
int* ptr;
assignint(&ptr);
*ptr = 10;
Example Programs
This code can be found on course democode folders.
#include <stdlib.h>
int* A = NULL;
if ((A = malloc(sizeof(int)*n)) != NULL)
{
for (i=0;i<n;i++)
*(A+i) = 0; // you can replace *(A+i) by A[i] if you wish
}
else
{ printf("malloc failed: Exiting Program!\n\n");
exit( EXIT_FAILURE );
}
#include <stdlib.h>
#define MAX_WORD_LENGTH 50
char* getword() {
char* s = malloc(MAX_WORD_LENGTH*sizeof(char));
if (s == NULL)
{
printf("malloc failed: Exiting Program!\n\n");
exit( EXIT_FAILURE );
}
printf("Enter a word (<%d chars): ",MAX_WORD_LENGTH);
scanf(“%s”,s);
return s;
}
Further Readings
[1] http://www.cs.cmu.edu/~thoffman/S09-15123/Chapter-
3/Chapter-3.html#CHAP_3.2