How to convert stack and heap objects

Example:

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; } 

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

Dealloc
Buff: Hi! Buff: ¯ø_

Obviously, the stack object does not work here - it is obvious that when the function exits the _obj2 pointer it points to the location on the stack. That's why I used heap objects in my previous question, which I was told was "stupid."

So the first question: if how can I convert the stack object (obj2) to a heap object so that it does not get freed after create () completes? I want a direct answer, not the arrogant "you're doing it wrong," as many have done. Because in this case, the stack objects cannot work, so the heap objects seem to be the only way. EDIT: It would also be useful to convert back to a stack object.

Second question: a concrete example of heap objects, "wrong", created a new vector<string>* using the new operator. If dynamically allocating STL objects is wrong, then what is the right way? Obviously, if you create them as stack objects, this fails because they are immediately freed, but they told me (again, a very high-ranking member) that dynamically allocate them, they can damage the heap. So what is the right way to do this?

+4
source share
7 answers

So the first question is: if I can convert the stack object (obj2) to a heap object so that it doesn't get freed after create () completes? I want a direct answer,

The direct answer: you cannot "transform" an object between the stack and the heap. You can create a copy of an object that lives in a different space, as others have pointed out, but what is it.

The second question: a concrete example of heap objects that are "wrong" is creating a new vector * using the new operator. If dynamically allocating STL objects is wrong, then what is the right way? Obviously, if you create them as stack objects, this fails because they are freed immediately, but I was told (again, a very high-ranking member) that their dynamic allocation could ruin the heap.

Dynamic allocation of STL objects alone will not damage the heap. (I don't know where you may have heard this.)

If you want to use the STL object allocated for the stack outside the function into which you created it, you cannot , since the stack space in which the object is located acts only inside which 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, this will return a copy of the object, not the original object itself - this would be impossible, since the stack containing this object is unwound after the function returns.

Another option is to pass the calling object to a link / pointer to the object your function is working with, if that makes sense for your specific scenario:

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

As you can see, you still allocated everything on the stack and avoided (sometimes indirect) overhead by relying on the copy constructor to return a copy of the object.

+9
source

So the first question is: if I can convert the stack object (obj2) to a heap object so that it doesn't get freed after create () completes?

This line:

 _obj2 = &obj2; 

Change to:

 _obj2 = new Class(obj2); // Create an object on the heap invoking the copy constructor. 

I need a direct answer, not the arrogant "you're doing it wrong," as many have done.

This is the direct answer you can get. Obviously, you are new to C ++, so I’m sure it will work as planned, because you probably made a couple of mistakes in defining the class "Class" (by the way, a terrible name).

It would also be useful to convert back to a stack object.

 class obj3(*_obj2); // dereference the heap object pass it to the copy constructor. 

The second question: a concrete example of heap objects, the “wrong” one, was to create a new vector <string> * using the new operator. If dynamically allocating STL objects is wrong, then what is the right way?

Why are you dynamically highlighting a vector. Just create it locally.

 std::vector<std::string> funct() { std::vector<std::string> vecString; // fill your vector here. return vecString; // Notice no dynamic allocation with new, } 

Using new / delete uses C ++, for example C. What you need to read is smart pointers. These are those that control the life of an object and automatically delete the object when they go out of scope.

 std::auto_ptr<Class> x(new Class); 

Here x is a smart pointer (like auto_ptr), when it goes out of scope, the object will be deleted. But you can return the auto_ptr of the calling function and it will be safely transferred from the function. This is actually much more complicated than you, and you need a book.

Obviously, if you create them as stack objects, this fails because they are freed immediately,

Its de'allocated when it goes beyond.

but they told me (again, a very high-ranking member) that their dynamic allocation could ruin the bunch.

If you do it wrong. This, given your knowledge, is very likely. But it’s hard to check, since you did not specify a class definition.

So what is the right way to do this?

  • Find out why you should use stack objects.
  • Find out what smart pointers are.
  • Learn how to use smart pointers to manage lifespans.
  • Explore the different types of smart pointers.
  • See what separation of problems is (you do not follow this basic principle).
+4
source

You need to either copy-build a new heap object ( Class * foo = new Class(obj2) ), or assign a stack object to a heap object ( *obj1 = obj2 ).

+1
source

the only way is to copy the object.

Change listing to:

Class _obj2;

and assign:

_obj2 = obj2;

0
source

Your stack object is created inside the create function and freed as soon as you exit the function scope. The pointer is invalid.

You can change Class* obj2 to Class obj2 and assign (i.e. copy) the object obj2 = obj2;

0
source

Taking the address of a stack variable will not result in a magic heap. You need to write the correct copy constructor for your class and use _obj2 = new Class(obj2); .

As for STL containers, they still distribute their data on the heap, why do you want to allocate the container to the heap? Place them in an area that will keep them alive until you need them.

0
source

I think you are really trying to ask: "How can I return an object created inside my function?" There are several valid ways:

  • Select the heap and return the pointer
  • Use an automatic variable and return its value, not a pointer (the compiler will copy it).
  • Allow the caller to provide storage with either a pointer or a reference parameter, and create your own object there.
0
source

All Articles