Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4016586
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:45:28+00:00 2026-05-20T09:45:28+00:00

Example: Class *_obj1; Class *_obj2; void doThis(Class *obj) {} void create() { Class *obj1

  • 0

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 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: Hello!
Buff: ¯ø_ˇ

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”.

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. EDIT: Also, converting back to a stack object would be useful as well.

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:45:29+00:00Added an answer on May 20, 2026 at 9:45 am

    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.

    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.

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

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example: class MyClass { Composition m_Composition; void MyClass() { m_Composition = new Composition( this
Example: class C { public: void operator =(int i) {} }; class SubC :
Example class in pseudocode: class SumCalculator method calculate(int1, int2) returns int What is a
Given a really oversimplified example: Class A { B b = new B(); }
Here is code example class A{ int i; public: A(int i) : i(i) {}
I have 3 classes in my example: Class A, the main activity. Class A
For Example: class A{ int b; A *a1; A *a2; A(int c):b(c), a1(c), a2(c)
Example class User has_many :tickets end I want to create association which contains logic
An Example: class SpecialButton { public SpecialButton(ButtonConfig conf, Method definedMethod) { button = new
Example: Class A { public function __construct() { $this->b_Instance = new B(); } public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.