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 8928225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:23:19+00:00 2026-06-15T08:23:19+00:00

i want do memory management in my project. i do not want operator global

  • 0

i want do memory management in my project. i do not want operator global new/delete so i implement a simple memory alloctor. this is my code:

class IAllocator
{
public:
    void* Alloc( unsigned int size )
    {
        1. alloc memory.
        2. trace alloc.
    }
    void Dealloc( void* ptr )
    {
        1. free memory.
        2. erase trace info.
    }
    template< typename T >
    void Destructor( T* ptr )
    {
        if ( ptr )
            ptr->~T();
    }
};
// macro for use easy.
# define MYNEW( T ) new ( g_Allocator->Alloc( sizeof( T ) ) ) T
# define MYDEL( ptr ) if (ptr) { g_Allocator->Destructor(ptr); g_Allocator->Dealloc(ptr); }

Then, i can use MYNEW to construct object( also trace alloc info for check memory leak ), and MYDEL to destroy object( erase trace info ).

Everything looks fine… but, when i try to use this method for multiple inheritance class, i found a very serious problem. look my test code below:

class A { ... };
class B { ... };
class C : public A, public B { ... };

C* pkC = MYNEW( C );
B* pkB = (B*)pkA;
MYDEL( pkB );

the address of pkB and pkA does not equal. so the memory will not free correct, and the alloc trace info will not erase coorect too…oh…

Is there any way to solve this problem?

  • 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-06-15T08:23:20+00:00Added an answer on June 15, 2026 at 8:23 am

    If ptr points to an instance of a polymorphic class, dynamic_cast<void*>(ptr) will result in a pointer to the most derived object pointed to by ptr. In other words, this dynamic cast yields a pointer to the as-allocated address.

    However, using g_Allocator->Dealloc(dynamic_cast<void*>(ptr)) is not a viable solution. The problem is that dynamic_cast<void*>(ptr) is illegal if ptr points to a non-class object (e.g., a primitive) or to an instance of a non-polymorphic class.

    What you can do is use SFINAE to create a function that uses this dynamic cast for pointers to polymorphic classes but uses a static cast for pointers to non-class objects and instances of non-polymorphic classes. Boost (and now C++11) provides is_class<T> and is_polymorphic<T> type traits that will help in this regard.

    Example:

    template <typename T, bool is_poly>
    struct GetAllocatedPointerHelper {
       static void* cast (T* ptr) { return ptr; }
    };
    
    template <typename T>
    struct GetAllocatedPointerHelper<T, true> {
       static void* cast (T* ptr) { return dynamic_cast<void*>(ptr); }
    };
    
    template<typename T>
    inline void*
    get_allocated_pointer (T* ptr)
    {
       const bool is_poly = Boost::is_polymorphic<T>::value;
       return GetAllocatedPointerHelper<T, is_poly>::cast(ptr);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to measure time memory allocation using this code: long AForMemory = DateTime.Now.Ticks;
I want to implement my own dynamic memory management system in order to add
I want to execute a program in memory on Windows. I do not want
Under manual memory management, I use this pattern fairly often: NSString * myStr =
I'm fairly new to Objective-C, and am currently reading up on memory management. I'd
I am writing the code using cocos2d. I want to release all the memory
Hai guys, I want to know,is memory management a concern with asp.net mvc.. comparision
i want to know the difference between drain and release in memory management..please help
I want to know which is better way regarding memory management from both cause
I'm fairly new to Delphi and have been doing all my memory management manually,

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.