Example of creating memory in heap

C++




int Geeks()
{
    // Nothing allocated yet excluding the
    // pointer itself, which is allocated
    // here on the stack.
    char* p;
 
    // Memory allocated on the stack.
    bool flag = true;
 
    if (flag) {
 
        // Create 1000 bytes on the stack
        char buffer[1000];
 
        // Create 1000 bytes on the heap
        p = new char[1000];
    }
 
    // buffer is deallocated here but pointer
    // p is not Here occurs a memory leak,
    // We have to call delete[] p;
}


Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        // java does not have direct access to memory
        // location ie. pointers so we use Object
        Object p;
 
        // Memory allocated on the stack.
        boolean flag = true;
 
        if (flag)
        {
            // Create 16 bytes on the stack memory
            char buffer;
 
            // Create 1000 bytes on the heap memory
            p = new Object[1000];
        }
    }
}
//This code is contributed by Akshay Tripathi(akshaytripathi19410)


Python




def Geeks():
    # Nothing allocated yet excluding the
    # pointer itself, which is allocated
    # here on the stack.
    p = None
 
    # Memory allocated on the stack.
    flag = True
 
    if flag:
        # Create 1000 bytes on the stack
        buffer = bytearray(1000)
 
        # Create 1000 bytes on the heap
        p = bytearray(1000)
 
    # buffer is deallocated here but pointer
    # p is not. Here occurs a memory leak,
    # We have to call del p
 
 # this code is contributed by bhardwajji


Javascript




<script>
// Define a function called Geeks
function Geeks() {
// Declare a pointer variable p
// No memory has been allocated yet, except for the pointer itself on the stack
let p;
 
// Declare a boolean variable flag and set it to true
let flag = true;
 
if (flag) {
     
// Declare a char array called buffer and allocate 1000 bytes on the stack
let buffer = new Array(1000);
 
// Allocate 1000 bytes on the heap and assign the pointer to p
p = new Array(1000);
}
 
    // buffer is deallocated here but pointer
    // p is not Here occurs a memory leak,
    // We have to call delete[] p;
}
// This code is contributed by Aman Kumar.
</script>


C#




using System;
 
class Program
{
    static void Main(string[] args)
    {
        // In C#, the equivalent of a Java Object is an object type
        object p;
 
        // Memory allocation on the stack
        bool flag = true;
 
        if (flag)
        {
            // Create 16 bytes on the stack memory
            char buffer;
 
            // Create 1000 bytes on the heap memory
            p = new object[1000];
        }
    }
}


What is a Memory Heap?

Similar Reads

What is Heap memory?

Heaps are memory areas allocated to each program. Memory allocated to heaps can be dynamically allocated, unlike memory allocated to stacks....

Advantages of heap memory:

Heap doesn’t have any limit on memory size. It allows you to access variables globally. Garbage collection runs on the heap memory to free the memory used by the object. The heap method is also used in the Priority Queue....

Disadvantages of heap memory:

It takes too much time to execute compared to the stack. It takes more time to compute. It can provide the maximum memory an OS can provide Memory management is more complicated in heap memory as it is used globally....

Problems that can be solved with heap memory:

The following are some important points about Garbage Collection....

Example of creating memory in heap:

C++ int Geeks() {     // Nothing allocated yet excluding the     // pointer itself, which is allocated     // here on the stack.     char* p;       // Memory allocated on the stack.     bool flag = true;       if (flag) {           // Create 1000 bytes on the stack         char buffer[1000];           // Create 1000 bytes on the heap         p = new char[1000];     }       // buffer is deallocated here but pointer     // p is not Here occurs a memory leak,     // We have to call delete[] p; } Java /*package whatever //do not write package name here */   import java.io.*;   class GFG {     public static void main(String[] args)     {         // java does not have direct access to memory         // location ie. pointers so we use Object         Object p;           // Memory allocated on the stack.         boolean flag = true;           if (flag)         {             // Create 16 bytes on the stack memory             char buffer;               // Create 1000 bytes on the heap memory             p = new Object[1000];         }     } } //This code is contributed by Akshay Tripathi(akshaytripathi19410) Python def Geeks():     # Nothing allocated yet excluding the     # pointer itself, which is allocated     # here on the stack.     p = None       # Memory allocated on the stack.     flag = True       if flag:         # Create 1000 bytes on the stack         buffer = bytearray(1000)           # Create 1000 bytes on the heap         p = bytearray(1000)       # buffer is deallocated here but pointer     # p is not. Here occurs a memory leak,     # We have to call del p    # this code is contributed by bhardwajji Javascript C# using System;   class Program {     static void Main(string[] args)     {         // In C#, the equivalent of a Java Object is an object type         object p;           // Memory allocation on the stack         bool flag = true;           if (flag)         {             // Create 16 bytes on the stack memory             char buffer;               // Create 1000 bytes on the heap memory             p = new object[1000];         }     } }...

Points to Remember:

...