0

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.

Scooter
  • 6,802
  • 8
  • 41
  • 64
TrustyCoder
  • 4,749
  • 10
  • 66
  • 119
  • 2
    If you *must* use C-style strings (`char *`) then add a destructor that explicitly calls `delete[]` if you called `new[]` or `free` if they were `malloc`ed. – Mike Bailey Jul 05 '12 at 15:22
  • Can you please show a proper way to initialize and free the structure using arrays of operation. I am having leaks if I only free the parent array. – TrustyCoder Jul 05 '12 at 16:18
  • @nitroxn There should be a `delete [] data;` at the end. – Mesop Jul 05 '12 at 18:35

5 Answers5

3

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.

Mesop
  • 5,187
  • 4
  • 25
  • 42
  • would you delete individual members of the struct or just the array of the struct itself. – TrustyCoder Jul 05 '12 at 16:24
  • 1
    @nitroxn If the individual members have been create with `new`, you will have to `delete` them first, then `delete[]` the list. Maybe this should help you: http://stackoverflow.com/questions/2486034/delete-an-array-of-objects. I think that in your case you should use `std::string` instead of `char*`. – Mesop Jul 05 '12 at 16:42
3

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.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
2

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.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

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;
  }
};
cygenb0ck
  • 53
  • 5
  • The structure is to be used again and again in a singleton scenario where destructor may not be called during the entire program execution. – TrustyCoder Jul 05 '12 at 16:29
  • Is this correct if( type ) delete[] type; if( label ) delete[] type; if( description ) delete[] type; – TrustyCoder Jul 05 '12 at 16:32
0

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

Fei Jiang
  • 330
  • 1
  • 6
  • 1
    What you've written means that `type` *must not* be null terminated, rather than there is no requirement that `type` be null terminated. I suspect this is an English issue rather than an understanding one. – Philip Kendall Jul 05 '12 at 15:22