delete [] *M;
is the same as delete [] M[0]
, so it is not equivalent to deleting all M[i]
in a loop because only the first one would be deleted. The loop is the correct way to avoid memory leaks.
Or better, use std::vector
instead of manual allocations and you won’t need to worry about deleting pointers.
Related Posts:
- Return array in a function
- Passing a 2D array to a C++ function
- How to create a dynamically-allocated array of const objects, but have values assigned to them?
- How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)?
- C++: Expression must have a constant value when declaring array inside function
- C++ pass an array by reference
- C++ Array of pointers: delete or delete []?
- C++ correct way to return pointer to array from function
- Warning: comparison of distinct pointer types
- Is C++ Array passed by reference or by pointer?
- Array of Linked Lists C++
- How to dynamically allocate arrays in C++
- How to dynamically allocate arrays in C++
- What are the differences between a pointer variable and a reference variable in C++?
- How do I find the length of an array?
- Initializing an array of objects
- What does “dereferencing” a pointer mean?
- Regular cast vs. static_cast vs. dynamic_cast
- What is a smart pointer and when should I use one?
- What does “dereferencing” a pointer mean?
- What is a char*?
- Passing an array by reference
- What exactly is nullptr?
- How to convert a char array to a string?
- How to fix a “invalid operands to binary expression” error?
- How do I declare a 2d array in C++ using new?
- What is a dangling pointer?
- What does `*&` in a function declaration mean?
- (->) arrow operator and (.) dot operator , class pointer
- lvalue required as left operand of assignment – Array
- What is uintptr_t data type
- What is the difference between const int*, const int * const, and int const *?
- C++ – No matching member function for call to ‘push_back’
- What is uintptr_t data type
- What does int & mean
- What is the difference between const int*, const int * const, and int const *?
- Pointer to incomplete class type is not allowed
- Is there a function to copy an array in C/C++?
- Reverse Contents in Array
- C++ error: “Array must be initialized with a brace enclosed initializer”
- warning: ISO C++ forbids variable length array
- c++ array – expression must have a constant value
- Can’t resolve Error: indirection requires pointer operand (‘int’ invalid)
- Static array vs. dynamic array in C++
- How to access the contents of a vector from a pointer to the vector in C++?
- Fill array with random numbers within a specified range (C++)
- Remove an array element and shift the remaining ones
- How to add element to C++ array?
- How to convert vector to array
- How to find the size of an int[]?
- What does int & mean
- splitting a string into an array in C++ without using vector
- Printing an array in C++?
- Is the sizeof(some pointer) always equal to four?
- How do you initialise a dynamic array in C++?
- C++ array assign error: invalid array assignment
- C++ Expression must have pointer-to-object type
- C++ strings and pointers
- initial value of reference to non-const must be an lvalue
- Typedef function pointer?
- How to initialize a vector of pointers
- How to add element to C++ array?
- Reading from .txt file into two dimensional array in c++
- extended initializer lists only available with
- Creation of Dynamic Array of Dynamic Objects in C++
- how to initialize an empty integer array in c++
- Check if C++ Array is Null
- Correct way to work with vector of arrays
- Resizing dynamic array in c++
- C++ Initializing a Global Array
- Comparing the values of char arrays in C++
- Dynamically allocated string array, then change it’s value?
- Using C-string gives Warning: “Address of stack memory associated with local variable returned”
- Reference to non-static member function must be called
- How can I assign an array from an initializer list?
- Why use pointers?
- How do I return a char array from a function?
- What’s the difference between * and & in C?
- creating an array of structs in c++
- Reading data from file into an array
- How to read lines of text from file and put them into an array
- Why can’t we pass arrays to function by value?
- Double pointer array in c++
- How to memset char array with null terminating character?
- Function stoi not declared
- creating dynamic array of string c++
- too many initializers for ‘int [0]’ c++
- Function stoi not declared
- C++ 2d char array to string
- how to find 2d array size in c++
- Initializing pointers in C++
- Getting error “array bound is not an integer constant before ‘]’ token”
- Using cin to input a single letter into a char
- Difference between function arguments declared with & and * in C++
- set head to NULL (‘NULL’ : undeclared identifier)
- What is a `char*`?
- Proper way to pass dynamic arrays to other functions
- clearing a vector of pointers [duplicate]
- Differences between unique_ptr and shared_ptr
- C++ float array initialization