草庐IT

new_value

全部标签

c++ -::operator new(size_t) 是否使用 malloc()?

::operatornew(size_t)是在内部调用malloc(),还是直接使用系统调用/操作系统特定的库调用?C++标准怎么说?在thisanswer它说:malloc()isguaranteedtoreturnanaddressalignedforanystandardtype.::operatornew(n)isonlyguaranteedtoreturnanaddressalignedforanystandardtypenolargerthann,andifTisn'tacharactertypethennewT[n]isonlyrequiredtoreturnanaddr

c++ - 我们是否需要显式调用分配给 "simple POD classes"的 "placement new"的析构函数?

这里的“简单”是指具有非虚空析构函数或POD类型的类。典型例子:charbuffer[SIZE];T*p=new(buffer)T;...p->~T();//如果我们不在p上调用显式析构函数会怎样?我不认为这是未定义的行为或内存泄漏。重用buffer有什么问题吗? 最佳答案 从技术上讲,假设析构函数不释放在构造期间获取的任何资源,则可能没有必要。但是,考虑到非技术方面——代码的维护和演进——我会坚持最佳实践——构建的东西应该被破坏。要考虑的场景-如果将来某些更改将确定要放入析构函数中的相关代码怎么办?你会记得你怀疑过那种类型的对象的

c++ - new[] 包含构造函数的字节对象数组没有错,对吧?

在我基于物理的渲染器中,我遇到了一个内存损坏错误(程序崩溃,调试器提供了一个毫无值(value)的虚假堆栈跟踪)。我追溯到这个SSCCE.与构造函数的行似乎是触发错误的原因:#includeclassFoofinal{public:uint8_tpacked;public:inlineFoo(void):packed(0xFF){}//causeserrorinline~Foo(void)=default;};static_assert(sizeof(Foo)==sizeof(uint8_t),"Implementationerror!");intmain(int/*argc*/,ch

c++ - 什么是 "a value not associated with an object"?

C++11和C++14标准(以及工作草案)在§3.10.1中说:Aprvalue(“pure”rvalue)isanrvaluethatisnotanxvalue.[Example:Theresultofcallingafunctionwhosereturntypeisnotareferenceisaprvalue.Thevalueofaliteralsuchas12,7.3e5,ortrueisalsoaprvalue.—endexample]和Anrvalue(socalled,historically,becauservaluescouldappearontheright-han

c++ - 技巧 : filling array values using macros (code generation)

AreC++TemplatesjustMacrosindisguise?我正在阅读上述主题,突然想到一个想法:为什么不尝试编写一些可以在我们的实际代码中使用的棘手宏,(不仅仅是作为在现实生活中无用的谜题)?所以首先想到的是:用宏填充数组值:intf(int&i){return++i;}#definee100r5(m20)#definem20m5,m5,m5,m5#definem5r5(e1)#definee1f(i)//avoiding++irighthere,toavoidUB!#definer5(e)e,e,e,e,eintmain(){inti=0;//thisisusedint

c++ - ldr [pc, #value] 的奇怪行为

我正在调试一些C++代码(ARM平台上的WinCE6),我发现有些行为很奇怪:4277220Cmovr3,#0x93,3042772210strr3,[sp]42772214ldrr3,[pc,#0x69C]42772218ldrr2,[pc,#0x694]4277221Cmovr1,#042772220ldrr0,[pc,#0x688]42772214ldrr3,[pc,#0x69C]行用于从.DATA部分获取一些常量,至少我是这么认为的。奇怪的是,根据代码r2应该从地址pc=0x42772214+0x69C=0x427728B0填充内存,但根据它从0x427728B8(8字节+)加

已解决matplotlib.units.ConversionError: Failed to convert value(s) to axis units: ‘LiR‘

已解决matplotlib.units.ConversionError:Failedtoconvertvalue(s)toaxisunits:‘LiR’下滑查看解决方法文章目录报错问题解决思路解决方法交流报错问题matplotlib.units.ConversionError:Failedtoconvertvalue(s)toaxisunits:‘LiR‘解决思路对于matplotlib.units.ConversionError:Failedtoconvertvalue(s)toaxisunits:‘LiR‘错误,这通常是由于尝试在matplotlib中使用无效的单位导致的。解决方法下滑查看

c++ - 如何使 is_arithmetic<myClass>::value 为真?

我的想法是我有一个函数可以对输入做一些算术运算,所以可能是这样的:#include#includeusingnamespacestd;templatedoublemean(constvector&vec){static_assert(is_arithmetic::value,"Arithmeticnotpossibleonthistype");//computemean(average)}//mean这很好用,可以计算我输入的所有数字类型的平均值。但是假设我随后创建了一个新类:classfoo{//classthathasarithmeticoperationscreated};//f

c++ - alignas 说明符是否与 'new' 一起使用?

我的问题很简单;alignas说明符是否与“new”一起使用?也就是说,如果定义了一个struct是对齐的,那么在分配new的时候会对齐吗? 最佳答案 在C++17之前,如果你的类型的对齐方式没有过度对齐,那么是的,默认的new将工作。“过度对齐”表示您在alignas中指定的对齐方式大于alignof(std::max_align_t).默认new将或多或少地与非过度对齐的类型一起工作;默认内存分配器将始终分配对齐等于alignof(std::max_align_t)的内存.但是,如果您的字体对齐过度,那您就不走运了。既不是默认的

c++ - decltype(new any_type()) 是否可能发生内存泄漏?

我正在使用valgrind检查类指针的任何内存泄漏可能性,并发现以下程序没有内存泄漏:#include#include#includeusingnamespacestd;classbase{};intmain(){unique_ptrb1=make_unique();base*b2=newbase();cout::value::value这怎么可能? 最佳答案 decltype(还有sizeof)的操作数不会被求值,所以任何副作用,包括内存分配,都不会发生。只有类型是在编译时确定的。所以这里唯一的内存分配是在make_unique和