Memory allocation in c

Memory allocation in c

It is a function which is used to allocate a block of memory dynamically. It reserves memory space of specified size and returns the null pointer pointing to the memory location. The pointer returned is usually of type void. … The malloc function returns a pointer to the allocated memory of byte_size.

The start of a string, as mentioned earlier, is indicated by a pointer to a variable of type char. You might recall how to declare such a pointer:
char *message;
This statement declares a pointer to a variable of type char named message. It doesn’t point to anything now, but what if you changed the pointer declaration to read:
char *message = “Great Caesar\’s Ghost!”;
When this statement executes, the string Great Caesar’s Ghost! (with a terminating null character) is stored somewhere in memory, and the pointer message is initialized to point to the first character of the string. Don’t worry where in memory the string is stored; it’s handled automatically by the compiler.

The malloc() function

The malloc() function is one of C’s memory allocation functions. When you call malloc(), you pass it the number of bytes of memory needed. malloc() finds and reserves a block of memory of the required size and returns the address of the first byte in the block. You don’t need to worry about where the memory is found; it’s handled automatically.
The malloc() function returns an address, and its return type is a pointer to type void. Why void? A pointer to type void is compatible with all data types. Because the memory allocated by malloc() can be used to store any of C’s data types, the void return type is appropriate.

#include <stdlib.h> 
void *malloc(size_t size);

malloc() allocates a block of memory that is the number of bytes stated in size. By allocating memory as needed with malloc() instead of all at once when a program starts, you can use a computer’s memory more efficiently. When using malloc(), you need to include the STDLIB.H header file. Some compilers have other header files that can be included; for portability, however, it’s best to include stdlib.h.

malloc() returns a pointer to the allocated block of memory. If malloc() was unable to allocate the required amount of memory, it returns null. Whenever you try to allocate memory, you should always check the return value, even if the amount of memory to be allocated is small.

Example 1

#include <stdlib.h>
#include <stdio.h> 
int main( void ) { 
/* allocate memory for a 100-character string */ 
       char *str; str = (char *) malloc(100);
       if (str == NULL) {
            printf( “Not enough memory to allocate buffer\n”); exit(1); 
            printf( “String was allocated!\n” );
        return 0;

Example 2

/* allocate memory for an array of 50 integers */ 
int *numbers;
numbers = (int *) malloc(50 * sizeof(int)); 

Example 3

/* allocate memory for an array of 10 float values */
float *numbers;
numbers = (float *) malloc(10 * sizeof(float)); 

Why do we need to allocate memory in C?

“free” method is used to dynamically de-allocate the memory. The memory allocated using functions malloc() and calloc() are not de-allocated on their own. Hence the free() method is used, whenever the dynamic memory allocation takes place. It helps to reduce wastage of memory by freeing it.

What are the types of memory allocation?

There are two types of memory allocation. 1) Static memory allocation — allocated by the compiler. Exact size and type of memory must be known at compile time. 2) Dynamic memory allocation — memory allocated during run time.