Pointers and dynamically allocated memory (array of indefinite lengths)------New Standard C + + programming

Source: Internet
Author: User

Background:

The length of the array is defined so that it is fixed throughout the program. C + + does not allow arrays that define an indeterminate number of elements. For example:

int N;int A[n];  This definition is not allowed.

However, in practical programming, there are situations where the amount of data to be processed cannot be determined programmatically. If you always define an array as large as possible, it can result in wasted space. Moreover, this "as big as possible" in the end should be how big enough?

To solve this problem, C + + provides a "Dynamic allocation of memory" mechanism, so that the program can be run, according to actual needs, the operating system temporarily allocated a piece of memory space for data storage. This memory allocation is made in the run of the program, not at compile time, so it is called dynamic memory allocation. Dynamic memory allocation is implemented in C + + through the new operator.

How to use:

The first usage:

P=new T;

T is any type name and P is a pointer of type t*. Such a statement dynamically allocates a size of sizeof (T) bytes of memory space and assigns the starting address of that memory space to p. For example:

int *p;p=new int;*p=5;

The second line dynamically allocates a 4-byte memory space, and P points to the space. This space can be read and written by P.

Second usage:

Used to dynamically allocate an array of any size:

P=new T[n];

T is any type name, p is a pointer of type t*, n means "number of elements", can be any expression with a positive integer value, the expression can contain variables, function calls, etc. Such a statement dynamically allocates a memory space of N x sizeof (T) characters, and the starting address of this space is assigned to P. For example:

int* pn;int i=5;pn=new int[i*20];p n[0]=20;pn[100]=30;

The last line compiles without problems, but the runtime causes the array to be out of bounds. Because there are only 100 elements in the dynamically allocated array above, PN[100] is no longer dynamically allocated within this memory area.

Processing after the use is finished:

The memory space that is dynamically allocated by the program from the operating system should be freed after it is used and returned to the operating system so that the operating system allocates this memory space for use by other programs. C + + provides the delete operator for freeing dynamically allocated memory space. The basic usage of the delete operator is as follows:

Delete pointer;

The pointer must point to a dynamically allocated memory space, or the runtime is likely to get an error. For example:

int *p=new int;*p=5;delete P;delete p;        This sentence causes a program error

The first DELETE statement above has correctly freed the dynamically allocated 4 bytes of memory space. The second DELETE statement causes the program to fail because the space pointed to by P is freed and P is no longer a pointer to the dynamically allocated memory space.

If you are allocating memory space with the second usage of new, that is, an array is allocated dynamically, then when you release the array, you should use the delete operator in the following form:

delete[] pointer;

For example:

int *p=new int [];p [0]=1;delete []p;

Similarly, the pointer p required to be released must be a pointer to a dynamically allocated memory space, or else an error will occur.

Note:

1, if the allocation of space is too large, the operating system can not find enough memory to meet, then the dynamic memory allocation will fail. The program throws an exception at this point.

2, if an array is dynamically allocated, but is freed with the "delete pointer", without "[]", there is no problem at compile time, the runtime also generally does not have an error, but actually causes the dynamically allocated array is not completely released.

3. Dynamically allocate memory space with the new operator, be sure to release it with the delete operator, ensuring that each subsequent execution path will release it.

4. A pointer is released, and the value of the pointer is not changed to NULL.

New standard C + + programming

Forwarding please indicate the source http://www.cnblogs.com/goudanli/p/7657712.html

Pointers and dynamically allocated memory (array of indefinite lengths)------New Standard C + + programming

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.