C++学习之new()和malloc()函数

1. Introduction

Memory management is an important concept in programming and different languages provide different methods for memory allocation and deallocation. In C++, two commonly used functions for dynamic memory allocation are new() and malloc().

2. new() Function

2.1 Usage

The new() function is used to dynamically allocate memory in C++. It is an operator that calls the constructor to initialize the allocated memory and returns a pointer to the newly allocated memory. The syntax for new() is as follows:

pointer_variable = new data_type;

For example, to allocate memory for an integer variable:

int *num;

num = new int;

2.2 Advantages

One of the advantages of using new() over malloc() is that new() automatically calls the constructor to initialize the memory, while malloc() only allocates the memory without initializing it. This can prevent programming errors caused by uninitialized variables.

2.3 Memory Allocation Failure

If the new() function fails to allocate the requested memory, it throws a bad_alloc exception. It is important to catch this exception in the program and handle it appropriately, such as by freeing any previously allocated memory and exiting gracefully.

2.4 Memory Deallocation

To deallocate the memory allocated by the new() function, use the delete operator. The syntax for delete is as follows:

delete pointer_variable;

For example, to deallocate the memory allocated for the integer variable:

delete num;

3. malloc() Function

3.1 Usage

The malloc() function is used to dynamically allocate memory in C. It is a library function that returns a void pointer to the newly allocated memory. The syntax for malloc() is as follows:

void *malloc(size_t size);

For example, to allocate memory for an integer variable:

int *num;

num = (int*) malloc(sizeof(int));

3.2 Memory Allocation Failure

If the malloc() function fails to allocate the requested memory, it returns a NULL pointer. It is important to check for this condition in the program and handle it appropriately.

3.3 Memory Deallocation

To deallocate the memory allocated by the malloc() function, use the free function. The syntax for free is as follows:

free(pointer_variable);

For example, to deallocate the memory allocated for the integer variable:

free(num);

4. Comparison

In general, new() is preferred over malloc() in C++ due to the advantages discussed above. However, malloc() may be preferred in certain situations, such as when working with legacy C code that uses malloc().

Another difference between new() and malloc() is that the former is an operator while the latter is a function. This means that new() can be overloaded like other operators in C++, while malloc() cannot.

5. Conclusion

Dynamic memory allocation is an important concept in programming and C++ provides two commonly used functions for it: new() and malloc(). Although they are similar in their basic usage, new() has advantages such as automatic initialization and overloadability that make it preferred in C++. However, in certain situations such as working with legacy C code, malloc() may still be preferred.

后端开发标签