什么是 C++ 的 C 等价物
delete[] (char *) foo->bar;
编辑:我正在将一些 C++ 代码转换为 ANSI C。它有:
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;
}
这会为 C 做吗?
free(last->key);
free(last->value);
free(last)
最佳答案
在 C 中,您没有 new
;你只有 malloc()
;要释放通过调用 malloc()
获得的内存,将调用 free()
。
也就是说,为什么要在将指针传递给 delete
之前将其转换为指向 (char*)
的指针?这几乎肯定是错误的:传递给 delete
的指针必须与使用 new
创建的指针类型相同(或者,如果它具有类类型,则为具有虚拟析构函数)。
关于c++ - C 等同于 C++ delete[] (char *),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2966997/