TrustyCoder
TrustyCoder

Reputation: 4789

delete structures in c++

Suppose a structure defined as below is allocated dynamically in an array. Would the type, label and description need to null terminated in order to safely delete the allocated structures?

struct operation_data
{
    int number;             
    char* type;             
    char* label;                
    char* description;
}

operation *data=new operation_data[5];  
for (int i=0; i<5; i++)
{
    data[i].type=new char[250];
    data[i].label=new char[250];
    data[i].description=new char[250];
}    
for (int i=0; i<5; i++)
{
    if (data[i].type) delete[] data[i].type;
    if (data[i].label) delete[] data[i].label;
    if (data[i].description) delete[] data[i].description;    
}

My code represents the snippet above. this is causing a Heap corruption detected error in the second delete statement. Please help me rectify this.

Upvotes: 0

Views: 5434

Answers (5)

cygenb0ck
cygenb0ck

Reputation: 53

no, they dont need to be null terminated. null termination is only an indicator where the string inside the char array ends.

i would recommend to add a ctor and dtor to your struct to safely delete it.

struct operation {
  int number;
  char* type;
  char* label;
  char* description;

  operation() : type(0), label(0), description(0) {}
  ~operation() {
    if( type )
          delete[] type;
    if( label )
      delete[] label;
    if( description )
      delete[] description;
  }
};

Upvotes: 1

Fei Jiang
Fei Jiang

Reputation: 330

The type, label and description need not to be null terminated

Upvotes: 0

David Grayson
David Grayson

Reputation: 87486

No. When you delete the structure, you are only deleting the ~16 bytes of memory for the structure itself (one int and three pointers). You are not actually deleting the memory that the pointers are pointing to. You must do that separately and it does not require null termination.

Upvotes: 3

Mike Seymour
Mike Seymour

Reputation: 254631

No, null-termination is not necessary to delete an array. If they were allocated with new char[size], then they can be deleted with delete [], whatever their contents.

If they have been allocated, then you'll need to delete each of them before deleting the struct itself; member pointers are not automatically deleted.

Null termination is necessary to use them with functions such as those in the C library that work with null-terminated strings. In C++, it's usually more convenient to use the standard std::string class, which manages the memory allocation for you.

Upvotes: 2

Mesop
Mesop

Reputation: 5263

If they really are arrays, then they must have been created with new[].

In that case, you must delete them with delete[], this should deallocate the array correctly.

So, you should have somewhere:

char* description = new [someSize];

and to delete it, you should do:

delete [] description;

Also, if you are using c++, you can use std::string instead of char*. That way, when the string is destroyed, the underlying memory is automatically freed.

Upvotes: 3

Related Questions