What is the C equivalent of C++
delete[] (char *) foo->bar;
Edit: I’m converting some C++ code to ANSI C. And it had:
typedef struct keyvalue
{
char *key;
void *value;
struct keyvalue *next;
} keyvalue_rec;
// ...
for (
ptr = this->_properties->next, last = this->_properties;
ptr!=NULL;
last = ptr, ptr = ptr->next)
{
delete[] last->key;
delete[] (char *) last->value;
delete last;
}
Would this do it for C?
free(last->key);
free(last->value);
free(last)
In C, you don’t have
new; you just havemalloc(); to free memory obtained by a call tomalloc(),free()is called.That said, why would you cast a pointer to
(char*)before passing it todelete? That’s almost certainly wrong: the pointer passed todeletemust be of the same type as created withnew(or, if it has class type, then of a base class with a virtual destructor).