Override Stack Allocate?

C

coder_lol

class A
{
};


int main()
{
A a; //Allocated on the stack.
}

Can I override the allocator for "a" to put a on the heap? I have a
problem where if my thread is killed asynchronously, the stack does
not get unwound, so I'd need everything to be on a heap I can control
and free.

Thank-you,
 
J

John Harrison

class A
{
};


int main()
{
A a; //Allocated on the stack.
}

Can I override the allocator for "a" to put a on the heap? I have a
problem where if my thread is killed asynchronously, the stack does
not get unwound, so I'd need everything to be on a heap I can control
and free.

Thank-you,

There's no way to do what you are asking for, a is on the stack and
there's nothing you can do about it.

You could do this though

class A_data
{
...
};

class A
{
A() : ptr(new A_data) {}
A_data* ptr;
};

int main()
{
A a;
}

Now class A is in effectively just a pointer to some data which has been
allocated on the heap.

john
 
K

kmoving

class A
{

};

int main()
{
A a; //Allocated on the stack.

}

Can I override the allocator for "a" to put a on the heap? I have a
problem where if my thread is killed asynchronously, the stack does
not get unwound, so I'd need everything to be on a heap I can control
and free.

Thank-you,

If U want to store your obj in heap not stack,why not use something
like
A *a = new A();

If the stand new and delete operator do not comfort u,
you can also implement your own.addational your thread lock model on
memory.

BestRegards
Kmoving
 
J

John Harrison

Well, the "shell" of A would still be on the stack and not deallocated
properly.

Yes that is true.

Here's a suggestion, instead of asking questions about what you think
might be possible solutions to your problem, just describe your problem
in detail and with precision and then ask for any solutions.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top