草庐IT

new-assignment

全部标签

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++ new 运算符——内存布局

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

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