C ++动态与堆栈对象和如何使用它们 [英] C++ Dynamic vs Stack Objects and How to Use Them
本文介绍了C ++动态与堆栈对象和如何使用它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
后续:请转到这个问题其实有一些有用的答案。
Follow-Up: Please head over to this question where there are actually some useful answers.
推荐答案
类型。你会这样做:
void create() {
int *obj1 = new int();
int obj2;
_obj1 = obj1;
_obj2 = &obj2;
}
您认为这会有效吗?显然不是。
这很简单。你不能把指针传递给一个分配给堆栈的对象(根据经验,你不应该把指针传递给刚刚分配的对象,如果有人分配了一个对象, it)
Would you think this would work? Clearly not. It's very simple. You can't pass out the pointer to an object allocated to the stack (and, as a rule of thumb, you shouldn't pass out the pointer to an object you have just allocated. If someone allocates an object he is responsable to free it)
这篇关于C ++动态与堆栈对象和如何使用它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文