A tutorial on the concept and operation method of heap memory (heap) in c ++

Source: Internet
Author: User

What is heap memory?

We know that the size of the array defined in c/c ++ must be defined in advance. They are usually allocated in static memory or stack memory, however, in actual work, we sometimes need to dynamically allocate the size of the array, here in the c library malloc. the malloc () function in the h header file solves the problem for you (bc or alloc in the old standard. h), the original function is void * malloc (size_t size). In the dynamically opened memory, we need to use free () after use () function to release the dynamically opened memory space!

Let's take a complete example!


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# Include <iostream>
# Include <malloc. h>

Using namespace std;
Main ()
{
Int arraysize; // number of elements
Int * array; // pointer variable used to dynamically open up an array

Cin> arraysize;
Array = (int *) malloc (arraysize * sizeof (int); // use malloc to open up memory space in the heap memory, the size is the number of elements multiplied by the length of the data type.

For (int I = 0; I <arraysize; I ++)
{
Array [I] = I;
}

For (int I = 0; I <arraysize; I ++)
{
Cout <array [I] <",";
}
Cout <endl;
Free (array); // use free to release heap memory
Cin. get ();
Cin. get ();
}


Note the following points:

array=(int*)malloc(arraysize * sizeof(int));



The original form of the malloc () function is void * malloc (size_t size). Because the dynamically allocated space computers do not know what to do, there is no type, but when you want to use it on a dynamic integer array, You need to explicitly convert it to int!

Next we will introduce the unique methods for c ++ to open up and release heap memory space, including the new modifier and delete modifier.

The operations of the new and delete modifiers do not require the support of header files, which is unique to c ++. The new operation is simpler than the malloc operation, and the number of opened types can be directly stated, delete [] must be used if it is an array.


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# Include <iostream>

Using namespace std;
Main ()
{
Int arraysize; // number of elements
Int * array;

Cin> arraysize;

Array = new int [arraysize]; // opens the heap memory

For (int I = 0; I <arraysize; I ++)
{
Array [I] = I;
}

For (int I = 0; I <arraysize; I ++)
{
Cout <array [I] <",";
}
Cout <endl;
Delete [] array; // release heap memory
Cin. get ();
Cin. get ();
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.