How does one create and subsequently delete a string variable in heap memory?

Robert Hoech 201 Reputation points
2021-03-01T04:51:49.02+00:00

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[];

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,600 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Igor Tandetnik 1,106 Reputation points
    2021-03-01T05:05:31.357+00:00

    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.

    0 comments No comments

  2. Sam of Simple Samples 5,516 Reputation points
    2021-03-01T14:49:24.313+00:00

    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.

    0 comments No comments

  3. WayneAKing 4,921 Reputation points
    2021-03-02T01:50:24.94+00:00

    (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.

    • Wayne
    0 comments No comments