---
title: "Why should C++ programmers minimize use of \"new\"?"  
description: "Why should C++ programmers minimize use of \"new\"?"  
author: "Steilla Mitchel"  
published: 2023-07-18  
updated: 2023-07-19  
canonical: https://www.mindstick.com/forum/159145/why-should-c-plus-plus-programmers-minimize-use-of-new  
category: "visual c++"  
tags: ["c++", "programs"]  
reading_time: 2 minutes  

---

# Why should C++ programmers minimize use of "new"?

Why should [C++](https://www.mindstick.com/blog/745/array-in-c-plus-plus) programmers minimize the use of 'new'?

## Replies

### Reply by Aryan Kumar

There are a few reasons why C++ programmers should minimize the use of `new`.

- **Memory leaks**

When you use `new` to allocate memory, you are responsible for calling `delete` to deallocate the memory when you are done with it. If you forget to call `delete`, you will create a memory leak. Memory leaks can cause programs to crash and can also waste memory.

- **Performance**

Allocating memory using `new` can be more expensive than allocating memory on the stack. This is because the `new` operator has to call the operating system's memory allocation function, which can be a relatively slow operation.

- **Safety**

Using `new` can be unsafe. If you make a mistake in your code, you can end up allocating memory that you don't have permission to access. This can lead to security vulnerabilities.

There are a few ways to minimize the use of `new` in C++.

- **Use RAII**

RAII (Resource Acquisition Is Initialization) is a design pattern that can help to prevent memory leaks. RAII uses objects that automatically deallocate their resources when they go out of scope. This means that you don't have to worry about calling `delete` to deallocate memory.

- **Use the stack**

The stack is a region of memory that is used to store local variables and function parameters. Variables that are allocated on the stack are automatically deallocated when the function returns. This means that you don't have to worry about calling `delete` to deallocate memory.

- **Use smart pointers**

Smart pointers are a type of object that automatically deallocates the memory that they are pointing to when they go out of scope. This can help to prevent memory leaks and can also improve performance.

Here are some additional things to keep in mind about using `new` in C++:

- `new` is a powerful tool, but it should be used with caution.
- If you are using `new`, make sure that you are calling `delete` to deallocate the memory when you are done with it.
- If you are using RAII, you don't have to worry about calling `delete`.
- If you are using the stack, you don't have to worry about calling `delete`.
- If you are using smart pointers, you don't have to worry about calling `delete`.


---

Original Source: https://www.mindstick.com/forum/159145/why-should-c-plus-plus-programmers-minimize-use-of-new

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
