草庐IT

was_deleted

全部标签

c++ - 析构函数上的 =delete 如何防止堆栈分配?

在此SOquestion声明此构造可防止实例的堆栈分配。classFS_Only{~FS_Only()=delete;//disallowstackallocation};我的问题是,它如何防止分配?我了解,无论是显式还是隐式,都无法删除此实例。但我认为,这会分别导致内存泄漏或运行时错误。编译器是否足够聪明,可以解决这个问题并引发编译器错误?还有为什么需要这个? 最佳答案 具有自动存储持续时间的变量(即局部变量)的析构函数需要在变量的生命周期结束时运行。如果没有可访问的析构函数,编译器将拒绝编译分配此类变量的代码。基本上,“堆栈分配

c++ - 对 'operator delete(void*)' 的 undefined reference

我是C++编程的新手,但从事C和Java方面的工作已经很长时间了。我正在尝试在我正在处理的一些串行协议(protocol)中做一个类似接口(interface)的层次结构,并不断收到错误:Undefinedreferenceto'operatordelete(void*)'(简化的)代码如下:PacketWriter.h:classPacketWriter{public:virtual~PacketWriter(){}virtualuint8_tnextByte()=0;}StringWriter.h:classStringWriter:publicPacketWriter{publi

c++ - 在p=new string[0]和p=new int[0]之后,为什么string版本在delete[] p时会崩溃?

我有两段关于new[]和delete[]的代码:1)#includeintmain(){std::string*p=newstd::string[0];delete[]p;return0;}2)在这种情况下,我只是将std::string更改为intintmain(){int*p=newint[0];delete[]p;return0;}我的问题是:为什么第一个程序崩溃并显示以下消息(在linux环境中):Segmentationfault(coredumped)但是第二个程序运行良好,没有任何错误?编辑编译器:g++(Ubuntu/Linaro4.7.2-2ubuntu1)4.7.2

c++ - 如果使用 delete 释放使用 malloc() 获得的内存,是否应该产生警告甚至断言失败?

在C++中,使用delete来释放通过malloc()获得的内存并不一定会导致程序崩溃。如果使用delete来释放使用malloc()获得的内存,是否应该产生警告甚至断言失败?为什么Stroustrup在C++上没有这个功能? 最佳答案 InC++usingdeletetofreememoryobtainedwithmalloc()doesn'tnecessarilycauseaprogramtoblowup.不,但它必然会导致未定义的行为,这意味着任何事情都可能发生,包括程序崩溃或程序继续以看似正确的方式运行。Doyouguyst

c++ - 带有 Cygwin 1.7 的 GoogleTest 1.6 编译错误 : 'fileno' was not declared in this scope

带有Cygwin1.7的GoogleTest1.6:“fileno”未在此范围内声明在EclipseCDT中对Factorial()函数进行简单测试时出现错误消息:Invoking:CygwinC++Compilerg++-std=c++0x-DGTEST_OS_CYGWIN=1-I"E:\source\gtest-1.6.0\include"-O0-g3-Wall-c-fmessage-length=0-MMD-MP-MF"src/challenge.d"-MT"src/challenge.d"-o"src/challenge.o""../src/challenge.cpp"Infi

C++ 错误 : Sleep was not declared in this scope

我在Ubuntu中使用带有codeBlocks的C++,在GCC4.7中boost1.46[yield_k.hpp]我得到这个编译时错误:error:Sleepwasnotdeclaredinthisscope代码:#includeusingnamespacestd;intmain(){cout如何解决此错误?我希望程序挂起1秒。 最佳答案 Sleep是一个Windows函数。对于Unix,请考虑使用nanosleep(POSIX)或usleep(BSD;已弃用)。一个nanosleep示例:voidmy_sleep(unsigne

c++ - 调用析构函数或调用 `delete` 时是否释放内存?

假设你有一个classFool的对象。classFool{inta,b,c;double*array;//...~Fool(){//destroysthearray..delete[]array;}};Fool*fool=newFool();现在,Iknowyoushouldn't,但是有些傻瓜无论如何都会在fool上调用析构函数。傻瓜->~Fool();.这是否意味着fool的内存已被释放,(即a、b、c无效)或是否仅表示~Fool中的任何释放()函数发生(即只删除数组?)所以我想我的问题是,析构函数只是另一个函数,它在对象上调用delete时被调用,还是做更多的事情?

c++ - 对于 C++ 中的指针,delete 命令真正对内存有什么作用?

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:C++delete-ItdeletesmyobjectsbutIcanstillaccessthedata?Canalocalvariable'smemorybeaccessedoutsideitsscope?当我想释放用new分配的内存时,我不明白delete究竟做了什么。在C++Premiere书中写道:Thisremovesthememorytowhichpspointerpoints;itdoesn’tremovethepointerpsitself.Youcanreuseps,forexample,

c++ - 将运算符 new[] 和放置 new 与普通 delete[] 混合

出于好奇,以下是否合法?X*p=static_cast(operatornew[](3*sizeof(X)));new(p+0)X();new(p+1)X();new(p+2)X();delete[]p;//AmIallowedtousedelete[]here?Orisitundefinedbehavior?同样:X*q=newX[3]();(q+2)->~X();(q+1)->~X();(q+0)->~X();operatordelete[](q); 最佳答案 我很确定两者都给UB。§5.3.4/12说新表达式的数组形式可能会给

c++ - delete[] 具有不同类型的未定义行为?

我想知道这是否是未定义的行为:#includeintmain(){auto*p=newuint8_t[32];float*c=reinterpret_cast(p);delete[]c;}在standard有Ifnot,thebehaviorisundefined.Inthesecondalternative(deletearray),thevalueoftheoperandofdeletemaybeanullpointervalueorapointervaluethatresultedfromapreviousarraynew-expression.79Ifnot,thebehavi