C/C ++ dynamic application Array

Source: Internet
Author: User

The new and delete operators are used to dynamically allocate and revoke memory.

New usage:

1. Open a single variable address space

1) New int; // open up a bucket for storing arrays and return an address pointing to the bucket. int * A = new INT: assign an int type address to integer pointer.

2) int * A = new int (5) serves the same purpose as above, but at the same time, the integer is assigned to 5

2. Open up array space

One-dimensional: int * A = new int [100]; opens up an integer array space of 100

Two-dimensional: int ** A = new int [5] [6]

3D and above: so on.

General Usage: New Type [initial value]

Delete usage:

1. int * A = new int;

Delete A; // release the space of a single int

2. int * A = new int [5];

Delete [] A; // release the int array space

To access the struct space opened by new, the variable name cannot be used directly, but can only be accessed through the pointer of the value assignment.

New and delete can be used to dynamically open up and cancel the address space. when programming, if you use a variable (usually an array temporarily stored), you need to use it again next time, but want to save the effort of re-initialization, you can open up a space each time you start using it and undo it after it is used up.

 

#include <iostream>using namespace std;int main(){    char *p=new char[10];    scanf ("%s",p);    printf ("%s",p);    delete []p;    while (1);    return 0;}

This is a two-dimensional array application method.

# Define row 100 # define Col 200 # define t char (INT, float ,....) // general data type T ** ptemp; * ptemp = new T [row]; for (INT I = 0; I <Col; I ++) ptemp [I] = new T [col}; // Delete For (INT I = 0; I <Col; I ++) Delete [] ptemp [I]; Delete [] [] ptemp;

 

1. Memory Allocation Function malloc

Call format: (type specifier *) malloc (size) function: allocate a continuous area with a length of "size" bytes in the dynamic storage area of the memory. The Return Value of the function is the first address of the region. The "type specifier" indicates the Data Type Used for the region. (Type Description *) indicates to forcibly convert the returned value to this type pointer. "Size" is an unsigned number. For example, Pc = (char *) malloc (100); indicates allocating 100 bytes of memory and forcibly converting it to a character array type, the Return Value of the function is a pointer to the character array, which is assigned to the pointer variable PC.

2. Memory Allocation Function calloc

Calloc is also used to allocate memory space. Call format: (type specifier *) calloc (n, size) function: allocate n consecutive areas with the length of "size" bytes in the memory dynamic storage area. The Return Value of the function is the first address of the region. (Type Description *) is used to force type conversion. The difference between the calloc function and the malloc function is that n areas can be allocated at a time. For example, PS = (struet Stu *) calloc (2, sizeof (struct Stu); the sizeof (struct Stu) is the length of the stu structure. Therefore, this statement indicates that two consecutive regions are allocated according to the length of Stu, Which is forcibly converted to the stu type and the first address is assigned to the pointer variable ps.

3. Release the memory space function free

Call method: Free (void * PTR); function: Release the memory space pointed to by PTR. PTR is a pointer variable of any type and points to the first address of the released area. The released zone should be the Region allocated by the malloc or calloc function.

 

# Include <stdio. h> # include <malloc. h> # include <stdlib. h> void main () {int * array = 0, num, I; printf ("Please input the number of element:"); scanf ("% d ", & num); // apply for the memory block used by the dynamic array = (int *) malloc (sizeof (INT) * num); If (array = 0) // memory application failed, prompting to exit {printf ("out of memory, press any key to quit... \ n "); exit (0); // terminate the program running, return to the operating system} // prompt input num data printf (" Please input % d elements: ", num ); for (I = 0; I <num; I ++) scanf ("% d", & array ); // output the input num data printf ("% d elements are: \ n", num); for (I = 0; I <num; I ++) printf ("% d,", array); printf ("\ B \ n"); // remove the separator comma-free (array) after the last digit ); // release the memory block applied by the malloc function}

 

 

 

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.