如何转换堆栈和堆对象 [英] How to Convert beween Stack and Heap Objects

查看:251
本文介绍了如何转换堆栈和堆对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:

Class *_obj1;
Class *_obj2;

void doThis(Class *obj) {}

void create() {
    Class *obj1 = new Class();
    Class obj2;

    doThis(obj1);
    doThis(&obj2);

    _obj1 = obj1;
    _obj2 = &obj2;
}

int main (int argc, const char * argv[]) {

    create();

    _obj1->doSomething();
    _obj2->doSomething();

    return 0;
}

这将创建2个对象,创建指向它们的指针,然后main方法。 Class对象创建一个char *并存储C字符串Hello!。在里面; 〜Class()释放器释放内存。 doSomething()方法使用printf()打印出buff:%​​s。很简单。现在如果我们运行它,我们得到这个:

This creates 2 objects, creates pointers to them, then main() calls a method on each. The Class object creates a char* and stores the C string "Hello!" in it; the ~Class() deallocator frees the memory. The doSomething() method prints out "buff: %s" using printf(). Simple enough. Now if we run it we get this:


Dealloc

增益:你好!

Buff:¯ø_

Dealloc
Buff: Hello!
Buff: ¯ø_ˇ

很明显,堆栈对象在这里不工作退出指针_obj2指向堆栈中的一个位置。这是为什么我在上一个问题中使用堆对象,人们告诉我是愚蠢的。

Obviously the stack object does not work here - it's obvious that when the function exits the pointer _obj2 is pointing at a location in the stack. This is why I used heap objects in my previous question, which people told me was "stupid".

因此,第一个问题是:if 将堆栈对象(obj2)转换为堆对象,所以它不会在create()退出后释放?我想要一个直接的答案,而不是一个傲慢的你做错了,因为许多人做了。因为在这种情况下,堆栈对象不能工作,所以堆对象似乎是唯一的方法。编辑:此外,转换回堆栈对象也将是有用的。

So, the first question is: if how can I convert the stack object (obj2) to a heap object so it's not deallocated after create() exits? I want a straight answer, not an arrogant "you're doing it wrong" as so many have done. Because in this case stack objects cannot work so heap objects seem to be the only way. Also, converting back to a stack object would be useful as well.

第二个问题:堆对象的具体示例是错误的创建一个新的 new 运算符使用code> vector< string> * 如果动态分配STL对象是错误的,那么什么是正确的方式?显然,如果你将它们创建为堆栈对象,它会失败,因为它们会立即释放,但我已经被告知高级成员)动态分配它们可能会损坏堆。

The second question: the specific example of heap objects being "wrong" was creating a new vector<string>* using the new operator. If dynamically allocating STL objects is wrong, then what's the right way? Obviously if you create them as stack objects it fails because they're immediately deallocated, but I've been told (again, by a very high-ranking member) that dynamically allocating them can corrupt the heap. So what's the right way to do it?

推荐答案


因此,第一个问题是:如何我可以将堆栈对象(obj2)转换为堆对象,所以它不会释放create()退出后释放?我想要一个直接的答案,

So, the first question is: if how can I convert the stack object (obj2) to a heap object so it's not deallocated after create() exits? I want a straight answer,

直接的答案是:你不能转换堆。

The straight answer is: You can't "convert" an object between the stack and heap. You can create a copy of the object that lives in the other space, as others have pointed out, but that's it.


您可以像其他人指出的那样创建存在于其他空间中的对象的副本。

第二个问题:堆对象的错误的具体示例是使用new运算符创建一个新的向量*。如果动态分配STL对象是错误的,那么什么是正确的方法呢?显然,如果你将它们创建为堆栈对象,它会失败,因为它们会被立即释放,但我已经告诉(同样,一个非常高级别的成员)动态分配它们可能会破坏堆。

The second question: the specific example of heap objects being "wrong" was creating a new vector* using the new operator. If dynamically allocating STL objects is wrong, then what's the right way? Obviously if you create them as stack objects it fails because they're immediately deallocated, but I've been told (again, by a very high-ranking member) that dynamically allocating them can corrupt the heap.

动态分配STL对象本身不会损坏堆。 (不知道你可能听说过的地方。)

Dynamically allocating STL objects will not on its own corrupt the heap. (No idea where you might have heard that.)

如果你想使用在你创建的函数之外的堆栈分配的STL对象,您不能,因为对象所在的堆栈空间只在创建它的函数内有效。

If you want to use a stack-allocated STL object outside of the function that you created it in, you can't, since the stack space in which the object resides is only valid inside the function that created it.

但是,您可以返回对象的副本:

You can, however, return a copy of the object:

std::vector<char> SomeFunc()
{
    std::vector<char> myvector;
    // myvector.operations ...
    return myvector;
}

正如我所说,虽然,这将返回一个对象的副本,原始对象本身 - 这是不可能的,因为包含对象的堆栈在函数返回后被解开。

As I said, though, this will return a copy of the object, not the original object itself -- that would be impossible, since the stack that contains the object is unwound after the function returns.

另一个选择是让调用者传入

One other option is to have the caller pass in a reference / pointer to the object that your function manipulates, if this makes sense for your particular scenario:

void SomeFunc(std::vector<char>& destination)
{
    // destination.operations ...
}

void AnotherFunc()
{
    std::vector<char> myvector;
    SomeFunc(myvector);
}

正如你所看到的,您可以避免依赖复制构造函数返回对象的副本(有时会导致)。

As you can see, you've still allocated everything on the stack, and you avoid the (sometimes consequential) overhead of relying on the copy-constructor to return a copy of the object.

这篇关于如何转换堆栈和堆对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆