Basic memo: New \ Delete, malloc \ free, and memset

Source: Internet
Author: User
1. New, delete, malloc, and free1. New, delete, malloc, and free relationships

Delete calls the destructor of the object, and the free corresponding to new only releases the memory. New calls the constructor. Malloc and free are standard library functions in C ++/C, and new/delete are operators in C ++. They can be used to apply for dynamic memory and release memory. For non-Internal data objects, maloc/free alone cannot meet the requirements of dynamic objects. The constructor must be automatically executed when the object is created, and the Destructor must be automatically executed before the object is extinct. Since malloc/free is a library function rather than an operator and is not controlled by the compiler, it is impossible to impose the tasks of executing constructor and destructor on malloc/free. Therefore, the C ++ language requires a new operator that can complete dynamic memory allocation and initialization, and a delete operator that can clean up and release memory. Note that new/delete is not a database function.

2. Differences between Delete and delete []

Delete calls the Destructor only once, while Delete [] calls the destructor of each member. In more efficient C ++, there is a more detailed explanation: "When the delete operator is used for an array, it calls the Destructor for each array element, and then calls operatordelete to release the memory ." Supports Delete and new, and supports Delete [] and new []

Memtest * mtest1 = new memtest [10];

Memtest * mtest2 = new memtest;

Int * pint1 = new int [10];

Int * pint2 = new int;

Delete [] pint1; //-1-

Delete [] pint2; //-2-

Delete [] mtest1; //-3-

Delete [] mtest2; //-4-

An error is reported at-4.

This indicates that for built-in simple data types, the delete and delete [] functions are the same. For custom complex data types, delete and delete [] cannot be mutually used. Delete [] delete an array. Delete deletes a pointer. In short, delete the memory allocated with new using delete Delete the memory allocated with new [] Using Delete [] delete Delete [] will call the destructor of the array element. Internal data types do not have destructor, so there is no problem. If you do not use parentheses when using Delete, delete will think that it points to a single object. Otherwise, it will think that it points to an array.

3. csdn Forum:New can be regarded as two actions: 1. Allocate memory (equivalent to malloc) 2. Initiate Constructor. 4. Baidu replied:1. malloc and free are standard library functions of C ++/C, and new/delete are operators of C ++. They can be used to apply for dynamic memory and release memory. 2. For non-Internal data objects, the use of maloc/free alone cannot meet the requirements of dynamic objects. The constructor must be automatically executed when the object is created, and the Destructor must be automatically executed before the object is extinct. Since malloc/free is a library function rather than an operator and is not controlled by the compiler, it is impossible to impose the tasks of executing constructor and destructor on malloc/free. 3. Therefore, the C ++ language requires a new operator that can complete dynamic memory allocation and initialization, and a delete operator that can clean up and release memory. Note that new/delete is not a database function. 4. c ++ programs often call C functions, while C Programs can only use malloc/free to manage dynamic memory. New is an operator, and what "+ ","-", "= "... there is a similar position: malloc, free is the c function, new, delete is the c ++ operator, in addition, new is the force type, malloc is not, there are also many different types of conversions, of course. New constructors can call the Class Object Function to initialize malloc only to allocate space when declared. Initialization is required elsewhere, and delete not only releases space, the Destructor will be called before release, and malloc needs to specify the size of the allocated space, while new is automatically calculated.

5. Experiment

The new keyword must be released by the developer. If the application is not released, it can exist until the end of the application. In C ++, delete is used to release memory.

Syntax for allocating a single memory:

Type identifier * pointer name = new type identifier (Initial Value); // For example: int * P = new int (10 );

Memory Allocation syntax for Arrays:

Type identifier * pointer name = new type identifier [array length]; // For example: int * P = new int [10];

For a single memory release Syntax:

Delete pointer name;

For arrays, the release syntax is:

Delete [] pointer name; // note [] Before pointer name

# Include <iostream> using namespace STD; int main () {int * P = new int; cout <"input array length:" <Endl; CIN> * P; cout <"enter an array element:" <Endl; int * q = new int [* p]; // For arrays, use the new data type [array length]; for (INT I = 0; I <* P; I ++) CIN> q [I];/* Q [I] instead of * Q [I]. equivalent to: int A [10] = {1, 2, 4, 5, 6, 7, 8, 9, 0}; int * q; // int * q =; so Q [I] = A [I]; */cout <"the array element is:" <Endl; For (INT I = 0; I <* P; I ++) cout <q [I] <'\ T'; cout <Endl; Delete P; // delete a single memory Delete [] q; // note here [] before the pointer name system ("pause ");}

Result:

Ii. malloc and memset

In C, malloc and memset are two common memory-related functions. First, let's take a look at the function prototype of the two functions.

1. malloc FunctionThe malloc function is used to allocate memory space of specified bytes from the stack. Void * malloc (size_t N );

The parameter n indicates the number of memory bytes to be allocated. If the execution is successful, the first address of the memory space obtained in the function range; if the execution fails, the return value is null. Because the type of the return value of the function is void pointer, you can convert the void pointer type and assign it to any type of pointer, in this way, you can operate on the memory space obtained from the stack by operating on this type of pointer. Note that the memory space allocated by the malloc function is not initialized. Sometimes, you need to initialize the memory space before using it, and memset will work in handy.2. memset Function
Void * memset (void * P, int C, size_t N );

The pointer P is the first address of the operated memory space, C is the value assigned to each byte, and N is the length of the byte of the operated memory space, that is, the number of bytes in which the memory is assigned to C. When using memset, pay attention to the value assignment in bytes. The value range is 0x00 ~ 0xff. If you want to assign values to an array of the double or Int type, pay special attention to this: example1: Char A [4]; memset (A, '\ 0', 4 );
Example2: int A [4]; memset (A, 1, 4); // replace it with memset (A, * sizeof (INT )) the first example is correct. memset assigns a null value to each char element in the character array. In the second example, it is clear that the result is not to assign each element in the int array to 1. Because the memset function assigns values in bytes, the four bytes of an int element in the array will be assigned a value of 1 (or the character corresponding to ASCII code 1 ), in fact, it represents an integer of 0x01010101. In addition, when using malloc to allocate memory space for a two-dimensional array, pay special attention to the possibility of errors when initializing with memset.
Int m = 2;
Int n = 3;
Int I;

// Two-digit array a [m] [N]
Int **;

A = (INT **) malloc (M * sizeof (int *));

For (I = 0; I <m; ++ I)
{
A [I] = (int *) malloc (N * sizeof (INT ));
}

Memset (A, 0, sizeof (INT) * m * n); When memset is used for all arrays above two dimensions, if the multi-dimensional array is allocated by calling the malloc function multiple times, the memory space of the array may be discontinuous. It makes no sense to use the memset function for continuous uniform assignment. The declared multi-dimensional array, such as the memory space of a [2] [3], is obviously continuous. In this case, it is no problem to use the memset function for initialization.

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.