Dynamic application of memory in C and C + +

Source: Internet
Author: User

The use of dynamic memory in C and C + + is different, in C language to use dynamic memory to include a header file that is #include <malloc.h> or #include < Stdlib.h> then uses the C language system function void * malloc (usigned size) to obtain dynamically allocated memory, the function parameter is the number of bytes of memory that needs to be requested, and returns the first address of the requested memory. The type of memory returned is void, so you need to force the type conversion as needed, such as int *array; array= (int *) malloc (sizeof (int) *10), so you can apply dynamically to a 10 The memory space of the INT type variable.

But in C + +, it seems that the statement used is simpler, including the header file is #include <iostream.h> application memory does not need to call the function as long as a keyword new.    e.g. int *array; array=new int[10]; Note that int *array=new int (10) is not a space to apply for 10 int, but an int-type memory space, and then assigns him a value of 10. Of course, if you use int *a; a=new int, then you get a memory space that can hold int data.

C and C + + Dynamic request memory is freed before the program exits,C uses the free () function, and C+ + uses the delete keyword.

Dynamic application of memory in C and C + +

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.