An array of dynamic requests for C + +

Source: Internet
Author: User

Operators for dynamic allocation and revocation of memory by the new and delete operators

New method of Use:

1. Open a single variable address space

1) New int; Opens an array of storage space and returns an address to that storage space. int *a = new int is the assignment of an int type address to an integer pointer a.

2) int *a = new int (5) acts as above, but at the same time the integer is assigned a value of 5

2. Open array Space

One-dimensional: int *a = new int[100]; Open an integer array space of size 100

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

Three-dimensional and above: and so on.

General use Method: new type [initial value]

Delete How to use:

1. int *a = new int;

Delete A; Free space for a single int

2.int *a = new Int[5];

delete [] A; Free int array space

To access the structure space opened by new, it cannot be directly through the variable name, but can only be interviewed by an assigned pointer.

Use new and delete to dynamically open, undo the address space. When you are in a program, if you run out of a variable (usually an array of temporary storage), you need to reuse it the next time, but you want to save another initialization, and you can open up a space each time you start using it, and then undo it when you're done.

#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 the method of applying a two-dimensional array

#define   ROW   #define   COL   #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. Allocating memory space functions malloc

Invocation form: (type specifier *) malloc (size) function: Allocates a contiguous region of length "size" bytes in the dynamic storage of memory. The return value of the function is the first address of the range. The type specifier indicates what data type to use for the region. The (type specifier *) indicates that the return value is cast to the type pointer. ' Size ' is an unsigned number. For example: pc= (char *) malloc (100); Represents allocating 100 bytes of memory space and casting it to a character array type, the return value of the function is a pointer to the array of characters, and the pointer is assigned to the pointer variable PC.

2. Allocating memory space functions Calloc

Calloc is also used to allocate memory space. Invocation form: (type specifier *) calloc (n,size) function: Allocates n contiguous regions of length "size" bytes in the memory dynamic store. The return value of the function is the first address of the range. (The type descriptor *) is used to force type conversions. The Calloc function differs from the malloc function only in the ability to allocate n blocks at a time. For example: ps= (Struet stu*) calloc (2,sizeof (struct stu)); The sizeof (struct Stu) is the structural length of the Stu. So the meaning of this statement is: Allocate 2 contiguous regions by the length of the Stu, cast to the Stu type, and assign its first address to the pointer variable PS.

3. Freeing the memory space function free

Call form: Free (VOID*PTR); Function: Releases the memory space pointed to by PTR, which is a random type pointer variable that points to the first address of the released area. The freed area should be the area 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 the memory block used by the dynamic array array      = (int *) malloc (sizeof (int) *num);      if (array = = 0)             //Memory request failed, prompt to exit      {          printf ("Out of memory,press any key to quit...\n");          Exit (0);             Terminate program execution, return operating system      }      //Prompt for NUM data       printf ("Please input%d elements:", num);       for (i = 0; i < num; i++)          scanf ("%d", &array);      Output the NUM data you just entered      printf ("%d elements is: \ n", num);      for (i = 0; i < num; i++)          printf ("%d,", array);      printf ("\b \ n");    Remove the last digit after the delimiter comma free      (array);        Frees the memory block requested by the malloc function}
   



An array of dynamic requests for C + +

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.