Releasing memory allocated for void pointer array

I declare an array of void pointers. Each of them indicates a value of an arbitrary type.
 void **values; // Array of void pointers to each value of arbitary type

Initialization of values ​​as follows:


    values = (void**)calloc(3,sizeof(void*));
    //can initialize values as: values = new void* [3];
    int ival = 1;
    float fval = 2.0;
    char* str = "word";
    values[0] = (void*)new int(ival);
    values[1] = (void*)new float(fval);
    values[2] = (void*)str;

    //Trying to Clear the memory allocated
    free(*values); 
    //Error: *** glibc detected *** simpleSQL: free(): invalid pointer: 0x080611b4
    //Core dumped
    delete[] values*;
    //warning: deleting 'void*' is undefined
    //Similar Error.

Now, how can I free / delete the memory allocated for values ​​(array of void pointers)?

+3
source share
7 answers

You have three things that are dynamically allocated that need to be freed in two different ways:

delete reinterpret_cast<int*>( values[0]);    
delete reinterpret_cast<float*>( values[1]);

free( values); // I'm not sure why this would have failed in your example, 
               //    but it would have leaked the 2 items that you allocated 
               //    with new

Please note that since it is strnot dynamically allocated, it should not be freed (in fact, it cannot ).

A few notes:

  • I suppose it sizeof(void) should have been sizeof(void*) because what you have will not compile
  • , , ,
+6

, , values: values = (void*)calloc(3,sizeof( void )). sizeof(void *), sizeof(void).

sizeof (void) - , , - , ... , , , .

: , ++ - new/delete C-style malloc/free. , delete -, malloc 'ed free - new' ed, , .

+7

boost::
, .

std::vector<boost::any>   data;
boost::any i1 = 1; // add integer
data.push_back(i1);

boost::any f1 = 1.0; // add double
data.push_back(f1);

data.push_back("PLOP"); // add a char *

std:: cout << boost::any_cast<int>(data[0]) + boost::any_cast<double>(data[1])
           << std::endl;

, :

values = (void*)calloc(3,sizeof(void));

// This should  have been
void** values = (void**)calloc(3,sizeof(void*));

// Freeing the members needs care as you need to cast them
// back to the correct type before you release the memory.

// now you can free the array with
free(values);

: /delete, calloc/free , . , .

+4

* alloc(). -, undefined.

+1

, void * calloc'd, , , .

darn formatting... ( ).

int ct = 3;
values = (void*)calloc(ct,sizeof(void));
//can initialize values as: values = new void* [3];
int ival = 1;
float fval = 2.0;
char* str = "word";
values[0] = (void*)new int(ival);
values[1] = (void*)new float(fval);
values[2] = (void*)str;

for ( int i = 0; i < ct; i++ ) [
    delete( values[i] );
}
free( values );
0

, , - C ( ).

, , , , . , malloc. void.

0

Note that you also do not delete the values ​​[0] and values ​​[1], which are a memory leak. However, according to your design, you cannot release values ​​[2], since a pointer to it is located in the .data section.

0
source

Source: https://habr.com/ru/post/1698312/


All Articles