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
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)?
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 ).
str
A few notes:
sizeof(void)
sizeof(void*)
, , values: values = (void*)calloc(3,sizeof( void )). sizeof(void *), sizeof(void).
values
values = (void*)calloc(3,sizeof(
void
))
sizeof(void *)
sizeof (void) - , , - , ... , , , .
: , ++ - new/delete C-style malloc/free. , delete -, malloc 'ed free - new' ed, , .
new
delete
malloc
free
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 , . , .
* alloc(). -, undefined.
, 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 );
, , - C ( ).
, , , , . , malloc. void.
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.
Source: https://habr.com/ru/post/1698312/More articles:Data disappearing after ItemUpdate in Sharepoint with Office 2007 documents - eventsКак сделать окна с интерфейсом UI отзывчивыми? - user-interfaceIn what context do users run SharePoint timer jobs? - sharepointHow to clear previous selection when asp: TreeView is in UpdatePanel? - ajaxAny good web chats? - pythonWell Explained Metric Space Indexing and Search Algorithms - algorithmOpenQuery for DB2 / AS400 from SQL Server 2000 causing locks - sql-serverWhen does the standard 404 page appear? - httpTCP connection quality in .NET. - .netShould the Cancel button be used as the Close button on a Windows form? - designAll Articles