myClassVar = MyClass(3);
Run Code Online (Sandbox Code Playgroud)
我期望在之前创建myClassVar的左侧调用析构函数.
但它实际上是在创建的新对象上调用的MyClass(3).
我的完整测试代码和输出如下..
我该如何解决这个问题?
实现赋值运算符?
MyClass实际上有指针和MYSQL_STMT*,我想知道我应该如何处理MYSQL_STMT*变量.
我只需要MyClassVar(3)对象而不是MyClassVar(),它是在创建ClientClass对象时首次创建的.
我经常遇到这种情况,并想知道是否有一个很好的方法来做到这一点.
#include <stdio.h>
class MyClass
{
public:
MyClass() { printf("MyClass %p\n", this); }
MyClass(int a) { printf("Myclass(int) %p\n", this); }
~MyClass() { printf("~MyClass %p\n", this); }
private:
int mA;
};
class ClientClass
{
public:
void Foo()
{
printf("before &myClassVar : %p\n", &myClassVar);
myClassVar = MyClass(3); // this is the important line
printf("after &myClassVar : %p\n", &myClassVar);
}
private:
MyClass myClassVar;
};
int main()
{
ClientClass …Run Code Online (Sandbox Code Playgroud) 我正在通过Troelsen的Pro C#书本来学习C#。
我熟悉堆栈和堆,以及C#如何存储这些东西。在C ++中,无论何时使用,new我们都会收到指向堆中某物的指针。但是,在C#中new,我的行为似乎与众不同:
new似乎只是调用int默认构造函数,但此类int的值仍将存储在堆栈中我了解所有对象/结构等都存储在堆中,无论是否new使用。
所以我的问题是:如何int在堆上实例化一个?(这与“装箱”有关吗?)
我想知道是否有任何C++专家可以对这种奇怪的情况有所了解.Box2D物理引擎附带的一个示例是消息"称为纯虚方法",但仅限于某个编译器(并且仅在发布版本中).
您可能知道Box2D是一个非常可靠的代码块,所以我认为这可能是编译器的问题,特别是考虑到它只发生在这个特定的编译器上.我在Windows7上使用mingw32:
> gcc.exe --version
gcc version 4.4.0 (GCC)
Run Code Online (Sandbox Code Playgroud)
以下是Box2D相关部分的简要摘录.您可以在以下网址查看完整资料来源:
b2Shape.h
b2CircleShape.h
b2CircleShape.cpp
SensorTest.h
//base class
class b2Shape
{
public:
virtual ~b2Shape() {}
virtual b2Shape* Clone(b2BlockAllocator* allocator) const = 0;
};
//sub class
class b2CircleShape : public b2Shape
{
public:
b2CircleShape();
b2Shape* Clone(b2BlockAllocator* allocator) const;
};
inline b2CircleShape::b2CircleShape() {}
b2Shape* b2CircleShape::Clone(b2BlockAllocator* allocator) const
{
void* mem = allocator->Allocate(sizeof(b2CircleShape));
b2CircleShape* clone = new (mem) b2CircleShape;
*clone = *this;
return clone;
}
Run Code Online (Sandbox Code Playgroud)
请注意克隆功能中的新位置.
现在导致问题的执行归结为:
{
b2CircleShape shape;
shape.Clone(allocator); //ok
}
{
b2CircleShape …Run Code Online (Sandbox Code Playgroud) 我最近一直在研究 FlatBuffers 库。我正在评估它在我的项目中的使用情况。查看flatbuffers.h后,我想知道是否违反了严格别名规则,如果它确实考虑了严格别名,有人可以解释它是如何做到这一点的吗?
在之前的项目中,我通过艰难的方式了解到了这条规则,优化会带来难以发现的微妙错误。我一直在使用放置新运算符来避免使用编译器标志来解决这个问题。
链接:
我正在寻找.Net中的数据结构,它保持异构结构在内存中连续,以便对cpu-cache友好.
这个类型的数据结构在这个博客中解释:T-machine.org在Iteration 4.
在.Net中,值类型(结构)数组使数据在内存中保持连续,但这仅适用于非泛型数组.我试图创建一个ValueType[],但结构框是盒装的.因此,引用在内存中是连续的,而不是真实的数据.
经过多次尝试,我认为在.Net中本身不可能.我看到的唯一可能的解决方案是手动管理字节数组中结构的分类和反序列化,但我不认为它会是高性能的.
你找到了原生解决方案吗?我的更好的解决方案?
编辑1:我正在尝试实现T-Machine.org博客中描述的实体组件系统.
我正在尝试理解用C++编写的其他人的代码,但是我从未见过构造函数的奇怪用法.代码如下所示:
A* a = new A(some initial values...);
...
B* b = new (a) B(some initial values...);
Run Code Online (Sandbox Code Playgroud)
初始化变量时b,(a)在new和之间B(...).这是什么意思?
这里有一些看起来很有趣的C++代码,但我知道它有效.
有一个结构定义,在程序中我们使用void指针分配内存.然后使用分配的缓冲区创建结构.
这是一些代码
typedef struct{
char buffer[1024];
} MyStruct
int main()
{
MyStruct* mystruct_ptr = 0;
void* ptr = malloc(sizeof(MyStruct));
// This is the line that I don't understand
mystruct_ptr = new (ptr) MyStruct();
free(ptr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
代码有更多的东西,但这是它的要点.
我没有测试过这段代码,但我正在查看的代码经过了很好的测试,并且可以运行.但是怎么样?
谢谢.
编辑:修复了内存泄漏.
我想尝试TBB的scalable_allocator,但在我不得不替换我的一些代码时感到困惑.这是分配器的分配方式:
SomeClass* s = scalable_allocator<SomeClass>().allocate( sizeof(SomeClass) );
Run Code Online (Sandbox Code Playgroud)
编辑:上面显示的不是如何使用scalable_allocator完成分配.正如ymett正确提到的,分配是这样完成的:
int numberOfObjectsToAllocateFor = 1;
SomeClass* s = scalable_allocator<SomeClass>().allocate( numberOfObjectsToAllocateFor );
scalable_allocator<SomeClass>().construct( s, SomeClass());
scalable_allocator<SomeClass>().destroy(s);
scalable_allocator<SomeClass>().deallocate(s, numberOfObjectsToAllocateFor);
Run Code Online (Sandbox Code Playgroud)
这很像使用malloc:
SomeClass* s = (SomeClass*) malloc (sizeof(SomeClass));
Run Code Online (Sandbox Code Playgroud)
这是我想要替换的代码:
SomeClass* SomeClass::Clone() const
{
return new SomeClass(*this);
}//Clone
Run Code Online (Sandbox Code Playgroud)
所以尝试了一个程序:
#include<iostream>
#include<cstdlib>
using namespace std;
class S
{
public:
int i;
S() {cout<<"constructed"<<endl;}
~S() {cout<<"destructed"<<endl;}
S(const S& s):i(s.i) {}
};
int main()
{
S* s = (S*) malloc(sizeof(S));
s = (S*) S();//this is obviously wrong
free(s);
} …Run Code Online (Sandbox Code Playgroud) 这包含在for循环中:
v[i] = new (&vv[i]) vertex(pts[i],i);
Run Code Online (Sandbox Code Playgroud)
vertex 是一个 structpts 是一个 point*v 是一个 vertex**vv 是一个 vertex*这(&vv[i])部分做什么?
c++ ×8
c# ×2
constructor ×2
struct ×2
.net ×1
allocation ×1
box2d ×1
cpu-cache ×1
destructor ×1
flatbuffers ×1
function ×1
implicit ×1
inheritance ×1
malloc ×1
mingw32 ×1
new-operator ×1
tbb ×1
vtable ×1