Skip to content
Related Articles

Related Articles

C++ malloc()

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 11 Oct, 2022
Improve Article
Save Article

The function malloc() in C++ is used to allocate the requested size of bytes and it returns a pointer to the first byte of allocated memory. A malloc() in C++ is a function that allocates memory at the runtime, hence, malloc() is a dynamic memory allocation technique. It returns a null pointer if fails.


pointer_name = (cast-type*) malloc(size);

Here, size is an unsigned integral value (cast to size_t) which represents the memory block in bytes

malloc() in C++ allocates a block of size bytes of memory, returning a pointer to the beginning of the block. The content of the newly allocated block of memory is not initialized, remaining with indeterminate values. Malloc function is present in <cstdlib> header file.

malloc() example

Syntax of malloc()

Return Types of malloc()

If the size is zero, the return value depends on the particular library implementation (it may or may not be a null pointer), but the returned pointer shall not be dereferenced.

  • void pointer is used to the uninitialized the initialized memory block that is allocated by the function
  • null pointer if the allocation fails

Working and Allocation of Memory Blocks Using malloc()

Example 1:


// C++ program to demonstrate working of malloc()
// cstdlib is used to use malloc function
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
    // size_t is an integer data type which can assign
    // greater than or equal to 0 integer values
    size_t s = 0; // s is SIZE
    // malloc declartion/initialization
    int* ptr = (int*)malloc(s);
    // return condition if the memory block is not
    // initialized
    if (ptr == NULL) {
        cout << "Null pointer has been returned";
    // condition printing the message if the memory is
    // initialized
    else {
        cout << "Memory has been allocated at address "
             << ptr << endl;
    return 0;


Memory has been allocated at address 0x8cae70

free() function in C++ is used to dynamically de-allocate the memory.

Example 2:


// C++ program to demonstrate working of malloc()
#include <iostream> 
using namespace std; 
int main() 
     // variable declaration
  int var_len = 10;
    // pointer variable declaration
  int *ptr;
  // allocating memory to the pointer variable using malloc()
  ptr = (int*) malloc(sizeof(int)*var_len);   
  for(int i=0;i<var_len;i++) 
      cout << "Enter a number : " << endl; 
      cin >> *(ptr+i); 
  cout << "Entered elements are : " << endl; 
   for(int i=0;i<var_len;i++) 
     cout << *(ptr+i) << endl; 
    return 0; 


malloc() initialization example

Output of malloc() initialization

Where Should Malloc be used?

1. Dynamic Memory allocation

Dynamic Memory Allocation helps us allocate a piece of memory as per the user’s demand. It returns a pointer to the start of that memory, which could be treated similarly to an array.

2. Heap memory

malloc() allocates the memory location on the heap and returns a pointer on the stack pointing to the starting address of the array type memory being allocated whereas the static array size put a hard upper limit on how much data the program could process at any one time, without being recompiled.

3. Better lifetime

Variables or Arrays created using malloc exist for a lifetime until they are cleared. This is of great importance for various data structures such as linked lists, binary heap, etc.

Difference between new and malloc()

new malloc
new is an operator malloc() is a function
new calls constructors malloc() does not call constructors
new returns the exact data type malloc() returns void*
new never returns a NULL (will throw on failure)  malloc() returns NULL
Reallocation of memory not handled by new  Reallocation of memory can be handled by malloc
new allocates memory and calls the constructor malloc only  allocates the memory

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!