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

  • Home
  • SEARCH
  • 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 6237965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:07:38+00:00 2026-05-24T11:07:38+00:00

Why cast a char* to char** when allocating a buffer in the code sample

  • 0

Why cast a char* to char** when allocating a buffer in the code sample below, and
what is happening here?

first_ = *reinterpret_cast<char **>(first_);

        //CODE SAMPLE
    public:    
        char * Allocate()
        {
            if (!first_)
                return 0;
            char *result = first_;
            first_ = *reinterpret_cast<char **>(first_); // WHY?
            --available_;
            return result;
        }




    private:
        char *buffers_;
        char *first_;
        std::size_t available_;
        std::size_t maxnum_;
        std::size_t buffersize_;

    //WHOLE CLASS IS HERE

    class Chunk
    {
    public:
        Chunk(std::size_t buffersize, std::size_t buffernum)
            : buffers_(0),
              first_(0),
              available_(0),
              maxnum_(0),
              buffersize_(0)
        {
            assert(buffersize > sizeof(char *) && buffernum > 0);
            std::size_t len = buffersize * buffernum;
            buffers_ = new char[len];
            first_ = buffers_;
            available_ = buffernum;
            maxnum_ = buffernum;
            buffersize_ = buffersize;

            char *begin = buffers_;
            char *end = buffers_ + len - buffersize_;
            *reinterpret_cast<char **>(end) = 0;
            for (; begin < end; begin += buffersize_)
            {
                char **next = reinterpret_cast<char **>(begin);
                *next = begin + buffersize_;
            }
        }

        ~Chunk()
        {
            delete [] buffers_;
        }

        char * Allocate()
        {
            if (!first_)
                return 0;
            char *result = first_;
            first_ = *reinterpret_cast<char **>(first_);
            --available_;
            return result;
        }

        void Deallocate(char *buffer)
        {
            *reinterpret_cast<char **>(buffer) = first_;
            first_ = buffer;
            ++available_;
        }

        bool IsFull() const
        {
            return available_ == 0;
        }

        // the buffer is one of this chunk
        bool IsChunkBuffer(char *buffer) const
        {
            assert(buffer);
            return buffer >= buffers_ && buffer < buffers_ + maxnum_ * buffersize_;
        }

    private:
        char *buffers_;
        char *first_;
        std::size_t available_;
        std::size_t maxnum_;
        std::size_t buffersize_;
    };
  • 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-05-24T11:07:39+00:00Added an answer on May 24, 2026 at 11:07 am

    It is a pool allocator. At the beginning of each free chunk, there is a pointer to the next free chunk. When the code above is executed, first_ points to a free chunk which is the first in a singly linked list of free chunks. Then it sets first_ to the next free chunk and returns the previous one, which becomes allocated since it’s no longer in the list of free chunks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help in understanding the code snipped below...allocate is a function that would
In Informix, how can I cast a char(8) type into a money type, so
Is it necessary to use reinterpret_cast< char*> when reading from a binary file. Since
I am unable to find out the bug in the below code which i
I am trying to cast something to char(n) where n is a function argument
How can i cast a pointer to char to a double ? I am
In cpp EBD_HEADER* pHeader; pHeader = reinterpret_cast <EBD_HEADER*>(reinterpret_cast<void*>(const_cast <char*> (GetHeader()))); In Objective C EBD_HEADER*
Can you cast a List<int> to List<string> somehow? I know I could loop through
How do I cast an int to an enum in C#?
How can I cast long to HWND (C++ visual studio 8)? Long lWindowHandler; HWND

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.