delete[] cp;
Memory allocated with array version of new
should be deleted with array version of delete
.
Better still, use std::string
for your string manipulation needs.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
For decades the following sample code has worked without a problem in every version of Visual Studio I have ever used:
char tstr[11];
strcpy(tstr,"0123456789");
char *cp;
cp = new char[strlen(tstr)+1];
. .
delete cp;
However, ever anally retentive Visual Studio 2019 issues the following warning:
C6283: "cp is allocated with array new [], but is deleted with scalar delete."
This warning is not only bad grammar but is also completely unhelpful.
Ditto, the text reached via the hotlink on C6283.
What kind of code is "new []"?
How about providing a freaking example of code that WON'T cause VS2019 to wet its pants?
Once again completely left in the dark by Microsoft, I tried to the following remedies w/o success.
Each version produced the same warning (but not an error).
(1) cp = malloc(strlen(tstr)+1);
. .
free cp;
(2) cp = new char[strlen(tstr)+1];
. .
delete cp[];
(3) cp = new char[strlen(tstr)+1];
. .
delete cp[strlen(tstr)+1];
(4) cp = new char[strlen(tstr)+1];
. .
delete [] cp;
(5) cp = new char[strlen(tstr)+1]; . .
delete [] cp[];
delete[] cp;
Memory allocated with array version of new
should be deleted with array version of delete
.
Better still, use std::string
for your string manipulation needs.
We can get additional information about complier errors and warnings by searching for the error code.
See C6283. It shows sample code for solving the problem.
(4) cp = new char[strlen(tstr)+1];
. .
delete [] cp;
I don't see why your example (4) would generate an error or warning.
Although it is customary to append the brackets to the "delete"
delete[] cp;
compilers usually don't care about inserted spaces there.
Just to add some history, for decades deleting an array using the
scalar delete:
char *cp;
cp = new char[10];
//. .
delete cp;
has been incorrect coding. (Does not conform to the
ANSI/ISO C++ Standard's requirements.)
The form of the delete should match the form of the new - scalar
or aggregate.
char *cp;
cp = new char;
//. .
delete cp;
char *cp;
cp = new char[10];
//. .
delete[] cp;
Historically there has been no functional difference when you use a
scalar delete on a pointer to an aggregate object, as in your example,
as long as the array is of a POD (Plain Old Data) type: char, int, etc.
However that result is not guaranteed and the resultant behavior is
generally undefined.
The problems have always arisen when the objects in the array are of
a class type. If you use a scalar delete on an array of class objects,
while the allocated heap memory may be freed, only the destructor for
the first object in the array will be invoked (if any). When you use
the correct form of delete:
delete[] cp;
then the destructors for each object in the array will be invoked before
their allocation is released.