473,320 Members | 2,094 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,320 software developers and data experts.

Detecting stack or heap instances

Anu
Hi,

I have code like this in my legacy class library :-

class Base
{
public:
void* operator new (size_t size);
Base();

private:
unsigned int magic;
}
void* Base::operator new(size_t size)
{
void *newobj = ::operator new(size);//call global operator new

//initialize the "magic"
((Base *)newobj)->magic = 0x89AE;
}

//constructor
Base::Base()
{
//if magic is "valid" then the object is allocated on heap
if (magic == 0x89AE)
{
//actions for heap object
}
else
{
//actions for stack object
}
}

All the library classes derive from Base. All this is part of a custom
caching solution. My questions are :-

1) Apart from possible uninitialized memory read in the constructor
for stack objects and the probability that the "magic" for a stack
object could be set to the valid value, is there any other problem?

2) In the operator new(), can we typecast the newly allocated chunk of
memory and start accessing the "Base" class members?

Thanks in advance

Anu.

Apr 16 '07 #1
2 1569
Anu wrote:
Hi,

I have code like this in my legacy class library :-

class Base
{
public:
void* operator new (size_t size);
Base();

private:
unsigned int magic;
}
void* Base::operator new(size_t size)
{
void *newobj = ::operator new(size);//call global operator new

//initialize the "magic"
((Base *)newobj)->magic = 0x89AE;
}

//constructor
Base::Base()
{
//if magic is "valid" then the object is allocated on heap
if (magic == 0x89AE)
{
//actions for heap object
}
else
{
//actions for stack object
}
}

All the library classes derive from Base. All this is part of a custom
caching solution. My questions are :-

1) Apart from possible uninitialized memory read in the constructor
for stack objects and the probability that the "magic" for a stack
object could be set to the valid value, is there any other problem?

2) In the operator new(), can we typecast the newly allocated chunk of
memory and start accessing the "Base" class members?
The only concern I'd have would be with descendants of Base if they
have virtual functions. But thinking about it, the vtbl pointer is
usually written to the object as part of constructing, so when some
descendant's c-tor puts the proper vtbl into the object's memory,
the Base's c-tor has already done its stuff.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 16 '07 #2
On Apr 16, 3:01 pm, "Anu" <annu_iyer2...@yahoo.co.inwrote:
I have code like this in my legacy class library :-
class Base
{
public:
void* operator new (size_t size);
Base();

private:
unsigned int magic;
}
void* Base::operator new(size_t size)
{
void *newobj = ::operator new(size);//call global operator new

//initialize the "magic"
((Base *)newobj)->magic = 0x89AE;
}
//constructor
Base::Base()
{
//if magic is "valid" then the object is allocated on heap
if (magic == 0x89AE)
{
//actions for heap object
}
else
{
//actions for stack object
}
}
All the library classes derive from Base. All this is part of a custom
caching solution. My questions are :-
1) Apart from possible uninitialized memory read in the constructor
for stack objects and the probability that the "magic" for a stack
object could be set to the valid value, is there any other problem?
In other words, apart the fact that it doesn't work, is there
any other problem?
2) In the operator new(), can we typecast the newly allocated chunk of
memory and start accessing the "Base" class members?
No. Formally, it's undefined behavior, period. In practice,
try it with virtual inheritance, or even ordinary multiple
inheritance, and it won't work.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 17 '07 #3

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

Similar topics

8
by: Andre | last post by:
Hi, If I say: int i = 5; Does 'i' get stored on the stack? If yes, where *is* the stack? On the heap? What manages the stack and how does it get created? Thanks -Andre
20
by: Sushil | last post by:
Hi gurus I was reading FAQ "alloca cannot be written portably, and is difficult to implement on machines without a conventional stack." I understand that the standard does not mandate...
22
by: bitshadow | last post by:
using the following code, i was able to have my compiler seg fault on me when i gave the argument as anythng greater than 20,832,000bytes. In the case of the struct its 868 instances of said...
2
by: Nick McCamy | last post by:
I have a question related to allocating on the stack. In this program below, are my following assumptions true? - variable a is allocated on the heap since it's static - variable b is...
4
by: Robbie | last post by:
Someone at work asked me this question: Value types--do they hit the heap or the pile? Well, I was a bit dumbfounded, because I never heard of the term "pile". I've heard of heap and stack,...
13
by: gmccallum | last post by:
General Info: A struct is stored on the stack and a class on the heap. A struct is a value type while a class is a reference type. Question: What if a struct contains a string...
9
by: shine | last post by:
what is the difference between a heap and a stack?
24
by: arcticool | last post by:
I had an interview today and I got destroyed :( The question was why have a stack and a heap? I could answer all the practical stuff like value types live on the stack, enums are on the stack, as...
3
by: Kirit Sælensminde | last post by:
I know that making new protected or private will (generally) prevent instances from being created on the heap, but I was wondering about preventing them on the stack. I saw in another post a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.