草庐IT

new_value

全部标签

c++ - 初始化对象时丢弃placement new返回值是否可以

这个问题来自this中的评论部分线程,并且在那里也得到了答案。但是,我认为它太重要了,不能只留在评论部分。所以我为此做了这个问答。Placementnew可用于在分配的存储空间中初始化对象,例如,usingvec_t=std::vector;autop=(vec_t*)operatornew(sizeof(vec_t));new(p)vec_t{1,2,3};//initializeavec_tatp根据cppref,PlacementnewIfplacement_paramsareprovided,theyarepassedtotheallocationfunctionasaddit

c++ - C/C++ 中的 STL、iostream、new、delete for CUDA

我可以在C/C++中为CUDA使用STL、iostream、new、delete吗? 最佳答案 如果您有Fermi级GPU(因此计算能力>=2.0),并且正在使用CUDA4.0或更高版本,那么new和delete都可以使用在设备代码中。不支持STL容器和算法以及iostream。如果您想对CUDA使用“类似STL”的操作,您可能会对Thrust感兴趣模板库。它允许主机代码使用容器类型透明地与GPU交互,并实现许多非常有用的数据并行原语,如排序、缩减和扫描。请注意,这仍然是主机端设备,不能在您自己的内核代码中使用Thrust及其容器。

c++ - 从 back_insert_iterator 中提取容器的 value_type 的特征类

std::back_insert_iterator的value_type等于void,但它还有一个protected成员container包含指向底层Container的指针。我正在尝试编写一个traits类来提取容器的value_type,如下所示:#include#include#includetemplatestructoutit_vt:OutputIt{usingself_type=outit_vt;usingvalue_type=typenamestd::remove_pointer_t().container)>::value_type;};intmain(){std::v

c++ - 可以将堆栈对象地址提供给 placement new 吗?

忽略这种做法的用处。(当然,我们欢迎现实生活中的例子。)例如,以下程序输出a的正确值:#includeusingnamespacestd;intmain(){inta=11111;inti=30;int*pi=new(&i)int();cout但是new-allocation不应该在i附近创建一些簿记信息吗?(为了正确的后续释放),在这种情况下应该会破坏i周围的堆栈。? 最佳答案 是的,使用指向堆栈上对象的指针执行placement-new是完全可以的。它只会使用那个特定的指针来构造对象。Placement-new实际上并不是分配任

c++ - 在 C++ 中,为什么动态创建对象需要 `new` 而不仅仅是分配?

我有这个简单的类层次结构:classBase{public:virtualintx()const=0;};classDerived:publicBase{int_x;public:Derived(intx):_x(x){}intx()const{return_x;}};如果我使用malloc分配一个Derived的实例,然后尝试访问多态函数x,程序崩溃(我得到段错误):intmain(){Derived*d;d=(Derived*)malloc(sizeof(Derived));*d=Derived(123);std::coutx()当然,我的实际应用要复杂得多(它是一种内存池)。我很

c++ - new 和 delete 处理多线程问题

我正在看书EfficientC++:PerformanceProgrammingTechniques作者对全局新的和删除的运营商说了以下内容:Theymanagememoryintheprocesscontext,andsinceaprocessmayspawnmultiplethreads,new()anddelete()mustbeabletooperateinamultithreadedenvironment.Inaddition,thesizeofmemoryrequestsmayvaryfromonerequesttothenext.第6章单线程内存池。这是真的吗?我认为C+

c++ - *v8::String::Utf8Value(args[0]->ToString()) 不返回 node.js 插件参数的字符串

我发现*v8::String::Utf8Value(args[0]->ToString())在Node0.8.232位上返回正确的字符串,但在Node0.8上不返回正确的字符串。8个64位。有人知道为什么吗?我的node.js插件看起来像这样:#defineBUILDING_NODE_EXTENSION#include#defineMAX_OUTPUT_BUF80extern"C"char*do_sqlsig(char*in);usingnamespacev8;HandleSqlsig(constArguments&args){HandleScopescope;char*c_arg,*

C++ new 运算符——内存布局

new运算符是否保证分配连续的堆内存块?IE。是objects=newBase[1024];在内存分配方面与objects=(Base*)malloc(1024*sizeof(base));还是可以有差距? 最佳答案 是的,内存会是连续的。在分配方面,它与malloc版本相同,但有几个区别(调用构造函数,new不返回NULL,malloc不会抛出异常等`).请注意,您不能将new[]与delete或free混淆,您必须使用delete[]对象释放内存。 关于C++new运算符——内存布局

c++ - unordered_map - {{key,value},{key,value}} 语法无效

我正在尝试编译thecodetakenfromhere//constructingunordered_maps#include#include#includetypedefstd::unordered_mapstringmap;stringmapmerge(stringmapa,stringmapb){stringmaptemp(a);temp.insert(b.begin(),b.end());returntemp;}intmain(){stringmapfirst;//emptystringmapsecond({{"apple","red"},{"lemon","yellow"}}

C++14:你能在 constexpr 中调用 new 吗?

当C++14取消对constexpr的限制时,它似乎包括以下内容(从Wikipedia复制):Expressionsmaychangethevalueofanobjectifthelifetimeofthatobjectbeganwithintheconstantexpressionfunction.Thisincludescallstoanynon-constconstexpr-declarednon-staticmemberfunctions.这似乎意味着您可以使用new创建一个对象,只要您在表达式中delete它,它就被允许。 最佳答案