Dynamic Memory Allocation in C
Dynamic Memory Allocation in C
The functions malloc() and calloc() are library functions that allocate memory
dynamically. Dynamic means the memory is allocated during runtime (execution of
the program) from the heap segment.
malloc() allocates a memory block of given size (in bytes) and returns a
pointer to the beginning of the block. malloc() doesn’t initialize the allocated
memory. It doesn't initialize memory at execution time, so it has garbage value initially. It
returns NULL if memory is not sufficient.
calloc() allocates the memory and also initializes every byte in the allocated
memory to 0. It initially initialize all bytes to zero. It returns NULL if memory is not
sufficient.
1. malloc() function creates a single block of memory calloc() function assigns multiple blocks of memory to
of a specific size. a single variable.
4. malloc() has high time efficiency. calloc() has low time efficiency.
5. The memory block allocated by malloc() has a The memory block allocated by calloc() is initialized by
garbage value. zero.
Math Functions :
Square Root
To find the square root of a number, use the sqrt() function:
Example : printf("%f", sqrt(16));
Round a Number
The ceil() function rounds a number upwards to its nearest integer, and the floor() method
rounds a number downwards to its nearest integer, and returns the result:
Example printf("%f", ceil(1.4)); return 2
printf("%f", floor(1.4)); return 1
Power
The pow() function returns the value of x to the power of y (xy):
Example : printf("%f", pow(4, 3));