Home  >  Article  >  Backend Development  >  The difference between malloc and new in c++

The difference between malloc and new in c++

下次还敢
下次还敢Original
2024-05-01 12:06:17665browse

The main difference between malloc and new in C is that the memory allocated by malloc needs to be released manually, while the memory allocated by new is released automatically. Malloc returns NULL if allocation fails, and throws an exception if new allocation fails. New automatically initializes the memory allocated, and the memory allocated by malloc needs to be initialized manually. new supports advanced features, such as array allocation, custom allocator, etc., malloc does not support it.

The difference between malloc and new in c++

The difference between malloc and new in C

malloc and new are both commonly used memory allocation functions in C , but there are some key differences between them.

1. Syntax

<code class="cpp">void *malloc(size_t size);
void *new(size_t size, std::nothrow_t = nullptr);</code>

2. Return value

  • malloc: If If the allocation is successful, a pointer to the allocated memory is returned, if the allocation fails, NULL is returned.
  • new: If the allocation is successful, a pointer to the allocated memory will be returned. If it fails, a std::bad_alloc exception will be thrown.

3. Memory management

  • ##malloc: The allocated memory is managed by the C runtime library and needs to be manually Use free to release.
  • new: The allocated memory is managed by the C runtime library and can be automatically released using delete.

4. Initialization

    ##malloc:
  • The allocated memory is not initialized and needs to be initialized manually.
  • new:
  • The allocated memory is automatically initialized using the constructor.
5. Exception handling

    malloc:
  • If allocation fails, an exception will not be thrown, but NULL will be returned.
  • new:
  • If allocation fails, a std::bad_alloc exception will be thrown, which can be used for exception handling.
6. Advanced features

    new:
  • Supports various advanced features, such as array allocation and custom allocator , placement new, etc.
  • malloc:
  • These advanced features are not supported.
Summary

In general, malloc and new are both memory allocation functions, but new is safer and easier to use, and can automatically initialize and handle exceptions . For simple memory allocation scenarios, malloc can be used, but for complex scenarios that require more features, new is recommended.

The above is the detailed content of The difference between malloc and new in c++. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to use setfill in c++Next article:How to use setfill in c++