-3

I am allocating a memory for union in heap and i need to delete the object of union when the element Id of union is 900.

Please help me to delete groupUnion[i] object when the Id is 900 below is my code.

groupUnion = (SettingsUnion *) malloc(sizeof(SettingsUnion) * (NumAttrs + 1));
if(groupUnion == (SettingsUnion *) NULL)
{
    return (FALSE);
}

for (unsigned int i=0; i < NumAttrs; i++)
{
    inFile.read(reinterpret_cast<char*>(&groupUnion[i]),sizeof(SettingsUnion));
    if(groupUnion[i].Id == 900)
    {
        free groupUnion[i]; // this is bad how can i delete groupUnion[i] when groupUnion[i].Id == 900
        groupUnion[i] = NULL;
    }
}

inFile.close()

Thanks in Advance!!

Jonas
  • 6,915
  • 8
  • 35
  • 53
Tina
  • 11
  • 1
  • 1
    You cannot "unallocate" a piece of memory in the middle of an already allocated memory. However, I highly doubt it's what you want - if you deallocate a piece of memory, you cannot access it anymore, so you cannot know that it was `Id==900`. What you really want to do? – yeputons Jun 07 '17 at 10:38
  • [This link explains](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) how you can do this. – Sam Varshavchik Jun 07 '17 at 10:40
  • 1
    Do not use malloc/free in C++. Use vector, unique_ptr, make_unique, new/delete. –  Jun 07 '17 at 11:00

2 Answers2

0

Your code fragment free groupUnion[i];groupUnion[i] = NULL lets me assume that you actually want to express an array of pointers to SettingUnion-objects rather than an array of SettingUnion-objects. So your code could look like as follows (I used your malloc/free-style, though in C++ you actually would use new/delete):

groupUnion = (SettingsUnion **) malloc(sizeof(SettingsUnion*) * (NumAttrs + 1));
if(groupUnion == NULL)
{
    return (FALSE);
}

for (unsigned int i=0; i < NumAttrs; i++)
{
    groupUnion[i] = malloc(sizeof(SettingsUnion));
    inFile.read(reinterpret_cast<char*>(&groupUnion[i]),sizeof(SettingsUnion));
    if(groupUnion[i]->Id == 900)
    {
        free groupUnion[i];
        groupUnion[i] = NULL;
    }
}
inFile.close()
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
0

You cannot free part of the allocated memory: free groupUnion[i]

What you can do however is to allocate individually the elements and then free them individually:

// not sure why you need the +1 (anyway you allocate an array of pointers to the struct here. Consider using new operator)
groupUnion = (SettingsUnion **) malloc(sizeof(SettingsUnion *) * (NumAttrs + 1)); 
if(groupUnion == (SettingsUnion *) NULL)
{
    return (FALSE);
}

for (unsigned int i=0; i < NumAttrs; i++)
{
    // you allocate the individual groupUnion here:
    groupUnion[i] = (SettingsUnion *) malloc(sizeof(SettingsUnion));
    if(groupUnion[i] == (SettingsUnion *) NULL)
    {
        return (FALSE);
    }

    inFile.read(reinterpret_cast<char*>(&groupUnion[i]),sizeof(SettingsUnion));
   if(groupUnion[i].Id == 900)
   {
        free groupUnion[i]; // this is bad how can i delete groupUnion[i] when groupUnion[i].Id == 900
        groupUnion[i] = NULL;
   }
}

inFile.close()
Noel Lopes
  • 106
  • 6