草庐IT

new_feature

全部标签

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

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

c++ - 如果对象的构造函数是noexcept,placement new(expression)可以抛出吗?

templatestructObj{//PlainOldDataforTusingInternalPod=typenamestd::aligned_storage::value>::type;InternalPodvalue_pod_;templateObj(Args&&...args){//myconstructor//placementnew:constructthevalueinthestaticallyallocatedspacenew(&value_pod_)T(std::forward(args)...);//Normalnew可以在分配失败或构造失败时抛出(如果有其他情况

c++ - gcc编译C++代码: undefined reference to `operator new[](unsigned long long)'

有一段C++代码:#includeintmain(){intb=sizeof('a');if(b==4)printf("I'maCprogram!\n");elseprintf("I'maC++program!\n");}像这样编译:gccmain.cpp-omain它成功并给出:I'maC++program!然后在函数main的某处添加一行int*p1=newint[1000];它失败了:C:\Users\...\AppData\Local\Temp\cccJZ8kN.o:main1.cpp:(.text+0x1f):undefinedreferencetooperatornew[]

java中Lists.newArrayList和new ArrayList的详细区别?

下面是对Lists.newArrayList()和newArrayList()的详细区别进行举例说明:创建具有初始数据的列表:javaCopycodeimportcom.google.common.collect.Lists;Listlist1=Lists.newArrayList("apple","banana","orange");Listlist2=newArrayList(Arrays.asList("apple","banana","orange"));在这个例子中,Lists.newArrayList()使用Guava库提供的方法可以直接将初始数据作为参数传递进去创建一个包含指定元

c++ - new 和 make_shared 用于共享指针

我遇到了this@kerekSB状态的帖子和答案之一std::shared_ptrp1=std::make_shared("foo");std::shared_ptrp2(newObject("foo"));Inyourcode,thesecondvariableisjustanakedpointer,notasharedpointeratall.Nowonthemeat.make_sharedis(inpractice)moreefficient,becauseitallocatesthereferencecontrolblocktogetherwiththeactualobject

c++ - 我们应该删除一个不是 new/malloc 的指针吗?

classClassA{public:ClassA(ClassB*p)b(p){}~ClassA(){deleteb;}ClassB*b;};这样的设计好吗? 最佳答案 答案是视情况而定。您必须明确谁负责对象的生命周期。此外,ClassA缺少用户定义的复制构造函数和赋值运算符,这可能会导致未定义的行为。例如:ClassAobject1(newClassB());//object1takesownershipoftheobjectClassAobject2(object1);//object2takesownershipofthesa

c++ - new[] 表达式不遵守 Microsoft VC++ 中的对齐方式

如果使用new[]表达式来创建具有析构函数的对象数组,数组中的对象可能没有正确对齐#include#include#pragmapack(8)structA{int64_ti;chardummy;~A(){}};intmain(){A*pa=newA[2];printf("sizeof(A)=%d,pointer=%p",sizeof(A),pa);}(我用VC++2010express构建32位目标)输出(在我的电脑上)是:sizeof(A)=16pointer=00344f4c(sizeof(A)=16表明编译器理解A的对齐要求并且该结构用7个字节填充[编辑:__alignof(A

c++ - C++中 "new_handler"除了垃圾回收还有什么用?

C++程序可以定义和设置new_handler(),如果无法分配请求的内存,则应从内存分配函数(如operatornew())调用该函数。自定义new_handler()的一个用途是dealingwithC++implementationsthatdon'tthrowanexceptiononallocationfailure.另一种用途是在实现垃圾收集的系统上启动垃圾收集。自定义new_handler()还有哪些其他用途? 最佳答案 与垃圾收集应用程序类似,您可以使用新的处理程序来释放您可能保留的任何缓存数据。假设您正在缓存从磁盘

C++初阶:C/C++内存管理、new与delete详解

之前结束了类与对象:今天进行下面部分内容的学习文章目录1.C/C++内存分布2.C语言中动态内存管理方式:malloc/calloc/realloc/free3.C++动态内存管理方式3.1new/delete操作内置类型3.2new和delete操作自定义类型4.operatornew与operatordelete函数5.new和delete的实现原理5.1内置类型5.2自定义类型6.定位new表达式(placement-new)7.知识点梳理malloc/free和new/delete的区别1.C/C++内存分布具体说明:栈又叫堆栈–非静态局部变量/函数参数/返回值等等,栈是向下增长的。栈

c++ - 在 C++ 中,在创建对象时,隐式使用 new 吗?

当我创建一个类的对象时,比如说,classA{public:A(){}};Aa;是否只调用了构造函数?还是隐式使用了new运算符?就像我们必须做的那样A*b=newA();另外,a和b在内存中的存储位置?栈还是堆? 最佳答案 第一种情况,如果a不是全局变量,那么它会被放入栈中,而b会被放入堆中。在第一种情况下,只调用了构造函数。new永远不会被调用,除非您像第二种情况那样明确地执行它。 关于c++-在C++中,在创建对象时,隐式使用new吗?,我们在StackOverflow上找到一个类