Download CS240 Programming in C

Transcript
Malloc and Dynamic Memory

malloc() allows us to request memory from the OS while the
program is running.

Instead of pre-allocating memory (eg. Maximum matrix) when the
program is compiled, we can allocate it at runtime using malloc()
p=(T*) malloc( sizeof(T) );

This allocates memory of sizeof(T) bytes and assigns it to ‘p’
(after casting)

p=calloc(1,sizeof(T)) allocates memory for an object of type T
like in malloc

The only difference is that calloc() initializes memory to 0.
calloc() calls malloc() internally and then initializes memory to 0
