相关疑难解决方法(0)

在分配期间,在不需要的对象上调用析构函数

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)

c++ destructor variable-assignment

5
推荐指数
1
解决办法
1269
查看次数

如何将int或其他“ C#值类型”存储在堆中(使用C#)?

我正在通过Troelsen的Pro C#书本来学习C#。

我熟悉堆栈和堆,以及C#如何存储这些东西。在C ++中,无论何时使用,new我们都会收到指向堆中某物的指针。但是,在C#中new,我的行为似乎与众不同:

  • 当与int等值类型一起使用时,using new似乎只是调用int默认构造函数,但此类int的值仍将存储在堆栈中

我了解所有对象/结构等都存储在堆中,无论是否new使用。

所以我的问题是:如何int在堆上实例化一个?(这与“装箱”有关吗?)

c#

5
推荐指数
1
解决办法
2338
查看次数

堆栈框架内重复的内联构造函数导致"纯虚方法调用"?

我想知道是否有任何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)

c++ inheritance vtable box2d mingw32

5
推荐指数
1
解决办法
413
查看次数

FlatBuffers 是否以某种方式避免了严格的别名?

我最近一直在研究 FlatBuffers 库。我正在评估它在我的项目中的使用情况。查看flatbuffers.h后,我想知道是否违反了严格别名规则,如果它确实考虑了严格别名,有人可以解释它是如何做到这一点的吗?

在之前的项目中,我通过艰难的方式了解到了这条规则,优化会带来难以发现的微妙错误。我一直在使用放置新运算符来避免使用编译器标志来解决这个问题。

链接:

c++ strict-aliasing flatbuffers

5
推荐指数
1
解决办法
584
查看次数

.Net中的数据结构保持异构结构在内存中是连续的

我正在寻找.Net中的数据结构,它保持异构结构在内存中连续,以便对cpu-cache友好.

这个类型的数据结构在这个博客中解释:T-machine.orgIteration 4.

在.Net中,值类型(结构)数组使数据在内存中保持连续,但这仅适用于非泛型数组.我试图创建一个ValueType[],但结构框是盒装的.因此,引用在内存中是连续的,而不是真实的数据.

经过多次尝试,我认为在.Net中本身不可能.我看到的唯一可能的解决方案是手动管理字节数组中结构的分类和反序列化,但我不认为它会是高性能的.

你找到了原生解决方案吗?我的更好的解决方案?

编辑1:我正在尝试实现T-Machine.org博客中描述的实体组件系统.

.net c# data-structures cpu-cache

5
推荐指数
1
解决办法
566
查看次数

在C++中奇怪地使用构造函数

我正在尝试理解用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++ constructor

5
推荐指数
1
解决办法
241
查看次数

构造函数是函数吗?是否可以调用构造函数

我发现了这一点,其中一条用户评论说:

构造函数不能被调用,它不是函数。创建新对象时会自动调用它。

我的问题是上述评论是否正确/正确?如果是,那么为什么构造函数不被视为函数,为什么我们不能调用它?

c++ constructor function implicit

5
推荐指数
1
解决办法
1765
查看次数

来自void指针缓冲区的struct实例化

这里有一些看起来很有趣的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)

代码有更多的东西,但这是它的要点.

我没有测试过这段代码,但我正在查看的代码经过了很好的测试,并且可以运行.但是怎么样?

谢谢.

编辑:修复了内存泄漏.

c++ struct allocation void-pointers placement-new

4
推荐指数
2
解决办法
1278
查看次数

使用malloc而不是new,并在创建对象时调用复制构造函数

我想尝试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)

c++ malloc memory-management tbb new-operator

4
推荐指数
2
解决办法
5453
查看次数

我不认为这是铸造,但它是什么?

这包含在for循环中:

v[i] = new (&vv[i]) vertex(pts[i],i);
Run Code Online (Sandbox Code Playgroud)
  • vertex 是一个 struct
  • pts 是一个 point*
  • v 是一个 vertex**
  • vv 是一个 vertex*

(&vv[i])部分做什么?

c++ struct

4
推荐指数
1
解决办法
146
查看次数