Detecting stack or heap instances

Discussion in 'C++' started by Anu, Apr 16, 2007.

  1. Anu

    Anu Guest

    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::eek:perator new(size_t size)
    {
    void *newobj = ::eek:perator 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.
     
    Anu, Apr 16, 2007
    #1
    1. Advertising

  2. 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::eek:perator new(size_t size)
    > {
    > void *newobj = ::eek:perator 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
     
    Victor Bazarov, Apr 16, 2007
    #2
    1. Advertising

  3. Anu

    James Kanze Guest

    On Apr 16, 3:01 pm, "Anu" <> wrote:
    > 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::eek:perator new(size_t size)
    > {
    > void *newobj = ::eek:perator 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:
    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
     
    James Kanze, Apr 17, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Wohlbier
    Replies:
    2
    Views:
    399
    Josiah Carlson
    Feb 22, 2004
  2. Michal Slocinski

    Heap dump file size vs heap size

    Michal Slocinski, Mar 25, 2008, in forum: Java
    Replies:
    1
    Views:
    774
    GArlington
    Mar 25, 2008
  3. viki
    Replies:
    6
    Views:
    631
    Erik Wikström
    Jun 28, 2008
  4. Replies:
    8
    Views:
    491
    James Stroud
    Jan 29, 2009
  5. Raymond Schanks
    Replies:
    0
    Views:
    597
    Raymond Schanks
    Apr 11, 2010
Loading...

Share This Page