473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Override Stack Allocate?

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,

Jun 30 '07 #1
4 1608
co*******@yahoo.com wrote:
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
Jun 30 '07 #2
On 6 30 , 1 30 , coder_...@yahoo.com wrote:
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

Jun 30 '07 #3
Well, the "shell" of A would still be on the stack and not deallocated
properly.

On Jun 29, 10:51 pm, John Harrison <john_androni...@hotmail.com>
wrote:
coder_...@yahoo.com wrote:
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- Hide quoted text -

- Show quoted text -

Jun 30 '07 #4
co*******@yahoo.com wrote:
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
Jun 30 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Tom | last post by:
What I mean is why can I only allocate const size stuff on the stack in C++? If I want to allocate a variable amount I need to use the OS API (Win32 in my case). Thanks, Tom.
6
by: | last post by:
I have a class with overloading operator new. (Because, if an identical object exists, return a pointer to existed object instead of a new pointer) It has no sense (it is dangerous) to allocate an...
2
by: news.tkdsoftware.com | last post by:
Aside from comp.compilers, is there any other forum, newsgroup or medium where I can post questions concerning the development of a byte code compiler & virtual stack machine? --
11
by: Dan Elliott | last post by:
Hello all, I am writing a program which needs to run as quickly as possible, but holds a lot of data in memory (around 1GB for a usual run). Is this too much memory to even consider putting...
5
by: Gomaw Beoyr | last post by:
Hello Is there any explanation why Microsoft chose to implement arrays as objects allocated on the heap instead of structs allocated on the stack? For "mathematical stuff", one normally...
16
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area...
24
by: John | last post by:
I know this is a very fundamental question. I am still quite confused if the program call stack stack should always grows upwards from the bottom, or the opposite, or doesn't matter?? That means...
13
by: deepak | last post by:
Hi In the following function how the memory 'll be allocated. 1) Will it allocate memory for all the char's together or allocate for first char. then for int then for float and after this only...
10
by: kid joe | last post by:
Hi all, I'm using a temporary buffer to transfer some data and would rather not allocate it on the heap. The problem is that the size of the buffer is only known upon entry into the function...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.