草庐IT

new_array

全部标签

c++ - 如果重载了一个new但是没有加载对应的delete会怎样?

任何人都可以解释如果在C++中重载了new但未加载相应的delete会发生什么情况? 最佳答案 这只是对象构造抛出异常时的问题,在C++115.3.4/18中有描述:Ifnounambiguousmatchingdeallocationfunctioncanbefound,propagatingtheexceptiondoesnotcausetheobject’smemorytobefreed.[Note:Thisisappropriatewhenthecalledallocationfunctiondoesnotallocatem

c++ - 什么是 string array[] = "";意思是为什么它有效?

stringarray[]="";如何将constchar*分配给数组?是否与以下内容相同:stringarray[]={""};??这对我来说很有意义。然而,这仍然不起作用intarray[]=5;那么它们对int数组不起作用有什么区别呢? 最佳答案 这是错误的代码;接受它是你的编译器(可能是gcc/g++?)中的错误。clang给出以下错误(link):a.cpp:5:17:error:arrayinitializermustbeaninitializerliststd::stringarray[]="";^1errorgene

c++ - 在 C++ 中用 new/delete 替换 malloc/free

我只是想确定一下。这是我的代码int*Image=(int*)malloc(sizeof(int)*m_Width/2*m_Height);free(Image);如果我想使用new而不是malloc和free而不是delete。这是我写的int*Image=newint[m_Width/2*m_Height];delete[]Image;对吗? 最佳答案 从技术上讲,这是正确的。然而,这是我们正在谈论的C++,动态分配数组的C++方法是使用std:vector代替:std::vectorImage(m_Width/2*m_Heig

c++ - 没有 new 的动态数组 (C++)

我是C++的新手,这是一个非常基本的问题。在C++中,只有两种创建动态数组的方法(读过一本书,如果我错了请纠正我)使用new运算符或malloc()函数,取自C。当声明一个数组intarray[size]时,方括号[]必须有一个const.但是在下面的代码中,size是一个unsignedint变量。#includeintmain(){usingnamespacestd;unsignedintsize;cout>size;intarray[size];//DynamicallyAllocatingMemorycout>array[i];}//DisplayingElementscout

c++ - 当您调用 new[] 为 N 个整数分配一个数组时,是否保证该数组将按顺序分配到物理内存中?

据我了解,每个计算机程序总是使用虚拟内存,而处理物理内存的方式取决于操作系统。我正在参加一个算法工程类(class),在某个时候有人提到,如果缓存内存是无限的并且一个缓存行的大小为B那么预计会发生的缓存未命中数如果您只想扫描N元素的数组,则为N/B我可以看出这在理论上是如何工作的,因为我们假设N元素在物理内存中一个接一个地放置。但是,这实际上是真的吗?如果虚拟内存是顺序分配的,那是否也意味着物理内存也将是顺序分配的?在我看来,在实践中,假设N不大于缓存大小,如果N元素未在物理内存(RAM)中按顺序分配。也许我误解了虚拟内存和物理内存之间的区别,我不确定。 最

c++ - 是否有像 C# 中那样的 C++ new 声明

我想知道是否有像C#中那样的new声明用于C++C#允许您这样做,它只是稍微整理了代码:FuncCall(newFoo(){Bar="sausage",Boo=4});只是我觉得这在C++中有点草率:unique_ptrfoo(newFoo());foo.Bar="sausage";foo.Boo=4;FuncCall(move(foo));Foo可能看起来像这样:classFoo{public:Foo();stringBar;intBoo;}为什么我不将所有内容都放入构造参数中?因为当你必须初始化这么多的时候,这很愚蠢:Foo(intwidth,intheight,stringtit

c++ - map 、集合等的 array_view 替代方案

假设我有一些类层次结构,其中有几个virtual返回容器引用的函数:#include#include#include#include#includeclassInterface{public:virtualconststd::vector&getArray()const=0;virtualconststd::set&getSet()const=0;virtualconststd::map&getMap()const=0;};classSubclassA:publicInterface{public:conststd::vector&getArray()constoverride{ret

c++ - c++11 严格别名规则是否允许通过 char *、char(&)[N]、甚至 std::array<char, N>& 使用 -fstrict-aliasing -Wstrict-aliasing=2 访问 uint64_t?

根据this关于C++11/14严格别名规则的stackoverflow回答:Ifaprogramattemptstoaccessthestoredvalueofanobjectthroughaglvalueofotherthanoneofthefollowingtypesthebehaviorisundefined:thedynamictypeoftheobject,acv-qualifiedversionofthedynamictypeoftheobject,atypesimilar(asdefinedin4.4)tothedynamictypeoftheobject,atypet

c++ - 当类没有 constexpr 构造函数时简化冗余 std::array 初始化

我有以下代码的更复杂版本:#include#includeusingnamespacestd;classDummy{public:Dummy(constdoublea,constdoublef){//Somecomplexcalculations}};constexprdoublevalues[]{0.1,0.2,0.3,0.4};constexprautoN=sizeof(values)/sizeof(values[0]);staticconstarraydummies{Dummy(10*values[0],M_PI*0),Dummy(10*values[1],M_PI*1),Dum

c++ - 类模板中 std::array 的大小取决于模板参数

我有一个下面的类模板templateconstexprintarraySize(){returnarraySize()+N;}templateconstexprintarraySize(){return0;}templateclassMyClass{public:std::array()>arr;};intmain(){MyClasscls;std::cout一切正常,但我想要calculateArraySize()作为成员函数。我尝试了以下方法:templateclassMyClass{public:staticconstexprintarraySize();std::array::