The concept and operation method of heap memory (heap) in C + +

Source: Internet
Author: User
Tags function prototype modifier

What is the heap memory?

We know that the size of an array defined in C + + must be defined in advance, they are usually allocated in the static memory space or in the stack memory space, but in the actual work, we sometimes need dynamic array size, here in C library malloc.h header file in the malloc () The function solves the problem for you (BC or alloc.h in the old standard), its function prototype is void* malloc (size_t size), in the dynamically developed memory, we use the free () function to release the dynamically opened memory space after use.

Now let's look at a complete example:

Program Author: Junning
Site: www.cndev-lab.com
All manuscripts are copyrighted, if you want to reprint, please be sure that the famous source and author
#include <iostream>
#include <malloc.h>
using namespace Std;
Main ()
{
int arraysize; Number of elements
int *array; A pointer variable used to dynamically open an array
cin>>arraysize;
Array= (int*) malloc (arraysize * sizeof (int))//Use malloc to open up memory space in heap memory, its 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)//frees dynamically exploited heap memory space
Cin.get ();
Cin.get ();

Here's a special place to watch:

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

The malloc () function of the prototype itself is void* malloc (size_t size), because the dynamically allocated space computer is not known for what it is used for, so it is no type, but you have to use it in the dynamic shape of the array when you need to explicitly convert to int*.

Here's how C + + is unique in creating and freeing heap memory space, the new modifier and the delete modifier.

The new and delete modifiers do not require the support of the header file, which is unique to C + +, and the new operation is simpler than the malloc, which directly describes the number of types to be opened, and delete uses delete[if it is an array.

Program Author: Junning
Site: www.cndev-lab.com
All manuscripts are copyrighted, if you want to reprint, please be sure that the famous source and author
#include <iostream>
using namespace Std;
Main ()
{
int arraysize; Number of elements
int *array;
cin>>arraysize;
Array=new int[arraysize];//to open 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;//frees heap memory
Cin.get ();
Cin.get ();
}

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.