http://sequitur.info/sequitur_simple.cc 处有一个代码
我更改了 malloc
和 free
行
R = (rules **) malloc(sizeof(rules *) * num_rules);
free(R);
到新建
和删除
R = (rules **) new(sizeof(rules *) * num_rules); //ERROR
delete(R);
但是 g++
显示此错误:
error: expected type-specifier before â;â token
error: invalid conversion from âlong unsigned intâ to âvoid*â [-fpermissive]
/usr/include/c++/4.6/new:103:14: error: initializing argument 2 of âvoid* operator new(std::size_t, void*)â [-fpermissive]
最佳答案
假设你的变量类型是正确的,应该是
R = new rules*[num_rules];
稍后,当您要释放它时,使用
delete[] R;
但很可能,OP 正在尝试解决此处的“错误”问题(设计这段代码时所做的假设可能不正确......)。 更多详情引用Jerry Coffin的回答here
关于什么时候new
像函数一样使用(placement new),引用refp的回答here
关于c++ - 将 malloc 更改为新的会导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15018331/