Use new in C + + to dynamically generate an array space and use Delete to release storage space

Source: Internet
Author: User

1 //dynamically allocating storage space using new2 3#include <iostream>4 usingstd::cout;5 6 intMain ()7 {8     //1th Way9     int*a=New int;Ten*a=1; Onecout<<"the result of dynamically allocating storage space using the first method is: \ n" A<<"*a="<<*a<<Std::endl; -Delete A;//releasing dynamic storage space -     //2nd Way the     int*b=New int(2); -cout<<"the result of dynamically allocating storage space using the first method is: \ n" -<<"*b="<<*b<<Std::endl; -Delete B;//releasing dynamic storage space +     //3rd Way -     int*C; +C=New int(3); Acout<<"the result of dynamically allocating storage space using the first method is: \ n" at<<"*c="<<*c<<Std::endl; -Delete C;//releasing dynamic storage space -  -     //Creating arrays Dynamically -     float*d=New float[3]; -d[0]=3; ind[1]=6; -d[2]=8; tocout<<"d[0]="<<d[0]<<Std::endl; +D=d+1; The fundamental difference between an array name and a pointer -cout<<"d[0]="<<d[0]<<Std::endl; thed=d-1; *cout<<"d[0]="<<d[0]<<Std::endl; $delete [] D;//releasing dynamic storage spacePanax Notoginseng     return 0; -}

Use new in C + + to dynamically generate an array space and use Delete to release storage space

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.