草庐IT

New-Project

全部标签

MongoDB $project : $filter sub-array

有一个项目(Mongoose)模式看起来像这样(简化为对问题很重要):{brand:{name:String,},title:String,description:[{lang:String,text:String}],shortDescription:[{lang:String,text:String}],variants:{cnt:Number,attrs:[{displayType:String,displayContent:String,displayName:[{lang:String,text:String}],name:String,},],}}我正在尝试按语言过滤项目,

MongoDB $project : $filter sub-array

有一个项目(Mongoose)模式看起来像这样(简化为对问题很重要):{brand:{name:String,},title:String,description:[{lang:String,text:String}],shortDescription:[{lang:String,text:String}],variants:{cnt:Number,attrs:[{displayType:String,displayContent:String,displayName:[{lang:String,text:String}],name:String,},],}}我正在尝试按语言过滤项目,

c++ - 是否使用 new char[] 或 malloc 的结果来转换 float * is IN(严格的别名违规)?

其中哪些代码有UB(具体来说,违反了严格的别名规则)?voida(){std::vectorv(sizeof(float));float*f=reinterpret_cast(v.data());*f=42;}voidb(){char*a=newchar[sizeof(float)];float*f=reinterpret_cast(a);*f=42;}voidc(){char*a=newchar[sizeof(float)];float*f=new(a)float;*f=42;}voidd(){char*a=(char*)malloc(sizeof(float));float*f=r

c++ - 为什么 sgi STL 源代码在 operator new 函数前面使用双冒号?

我正在阅读SGI标准模板库的源代码。我发现operatornew函数前面总是有一个双冒号。像这样:T*tmp=(T*)(::operatornew((size_t)(size*sizeof(T))));operatornew可以不加::字段直接调用,那为什么STLcoder会这样写呢?如果我们不使用它们前面的::,可能会遇到什么陷阱或情况。 最佳答案 您可以为类重载operatornew并在其前面加上"::"将调用全局"default"operatornew而不是可能的重载。例如:#includeclassFoo{public:Fo

c++ - Valgrind 和 "WARNING: new redirection conflicts with existing"

我在Valgrind中得到了这个。--24101--REDIR:0xbb20580(operatordelete(void*))redirectedto0x93b7d48(operatordelete(void*))--24101--REDIR:0xbb22580(operatornew[](unsignedlong))redirectedto0x93b88b7(operatornew[](unsignedlong))==24101==WARNING:newredirectionconflictswithexisting--ignoringit--24101--new:0x156320

c# - new 是否总是在 C++/C#/Java 中的堆上分配

我的理解一直是,无论是C++、C#还是Java,当我们使用new关键字创建对象时,它都会在堆上分配内存。我认为new只有引用类型(类)才需要,而原始类型(int、bool、float等)从不使用new并且总是继续堆栈(除非它们是使用new实例化的类的成员变量)。但是,我一直在阅读information这让我怀疑这个长期存在的假设,至少对于Java和C#。例如,我刚刚注意到在C#中new运算符可用于初始化值类型,请参阅here.这是否是规则的异常(exception),语言的辅助功能,如果是,还会有哪些其他异常(exception)?有人可以澄清一下吗? 最佳

c++ - C++ 构造 "placement new"的用途是什么?

我刚刚了解了名为“placementnew”的C++构造。它允许您精确控制指针在内存中指向的位置。它看起来像这样:#include//Must#includethistouse"placementnew"#include"Fred.h"//DeclarationofclassFredvoidsomeCode(){charmemory[sizeof(Fred)];void*place=memory;Fred*f=new(place)Fred();//CreateapointertoaFred(),//storedat"place"//Thepointersfandplacewillbee

c++ - 在p=new string[0]和p=new int[0]之后,为什么string版本在delete[] p时会崩溃?

我有两段关于new[]和delete[]的代码:1)#includeintmain(){std::string*p=newstd::string[0];delete[]p;return0;}2)在这种情况下,我只是将std::string更改为intintmain(){int*p=newint[0];delete[]p;return0;}我的问题是:为什么第一个程序崩溃并显示以下消息(在linux环境中):Segmentationfault(coredumped)但是第二个程序运行良好,没有任何错误?编辑编译器:g++(Ubuntu/Linaro4.7.2-2ubuntu1)4.7.2

c++ - 有没有办法使用模板特化将 new 与 new [] 分开?

我有一个自动指针类,在构造函数中我传入了一个指针。我希望能够在构造函数中将new与new[]分开,以便我可以在析构函数中正确调用delete或delete[]。这可以通过模板特化来完成吗?我不想在构造函数中传入一个bool值。templateclassMyAutoPtr{public:MyAutoPtr(T*aPtr);};//inuse:MyAutoPtrptr(newint);MyAutoPtrptr2(newint[10]); 最佳答案 很遗憾,没有。两者都返回相同的类型,T*。考虑使用调用适当的重载构造函数的构建器函数:te

C++:push_back(new Object()) 是内存泄漏吗?

以下C++代码是否存在内存泄漏?list.push_back(newString("hi"));据我了解,任何std集合/容器中的push_back总是会制作拷贝。因此,如果复制了新字符串,则没有什么可以删除新字符串,对吧?因为push_back之后没有引用它...我是对还是错?谢谢。Jbu编辑:我想我错了,因为new将返回一个指针......我们将始终拥有能够删除新字符串的指针 最佳答案 是的,但不是你想的那样。取决于如何list已定义并初始化,push_back可能会抛出异常。如果是,则从new返回的指针迷失了,永远无法解脱。但