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

The Archive Base Latest Questions

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

I have a Visual Studio 2008 C++ program where I’m populating a std::list with

  • 0

I have a Visual Studio 2008 C++ program where I’m populating a std::list with the addresses of a memory pool.

I have an implementation that works using std::generate and it’s not bad, but it can be a bit slow with large pools of small allocation blocks.

/// fill the allocation list with the memory addresses of each block
struct fill
{
    fill( void* start, ulong alloc ) 
        : start_( start ), 
          alloc_( alloc ), 
          count_( 0 ) 
    {
    };

    void* operator()()
    {
        return ( void* )( ( ulong ) start_ + ( count_++ ) * alloc_ );
    }

    /// starting address
    void* start_;
    /// size of the blocks
    ulong alloc_;
    /// internal counter
    int count_;
}; // struct fill

ulong begin = 0;            // beginning address
ulong max_size = 0x1000;    // maximum memory pool size (4KB)
ulong block_size = 0x20;    // size of each memory block (32B)

std::list< void* > memory;
memory.resize( max_size / block_size ); // 128 memory blocks
std::generate( memory.begin(), memory.end(), fill( begin, block_size ) );

I was just wondering if anybody had a faster or more efficient method of filling the linked-list.

Thanks,
PaulH

  • 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-22T11:26:38+00:00Added an answer on May 22, 2026 at 11:26 am

    Your code passes over the list twice instead of once.

    So it might help to define an iterator that returns the addresses, so that everything is done in a single pass:

    struct blocks {
        void *current;
        size_t increment;
    
        blocks(void* start, size_t size = 0) : current(start), increment(size) {}
    
        bool operator==(const blocks &rhs) const { return current == rhs.current; }
        bool operator!=(const blocks &rhs) const { return current != rhs.current; }
        void *operator*() const { return current; }
        blocks &operator++() {
            current = (void*)( (char*)current + increment );
            return *this;
        }
    };
    
    std::list<void*> memory(blocks(begin, block_size), blocks(max_size));
    

    (Code not tested, and I’ve left out some of the stuff you need in order to be a proper iterator – it needs tagging if nothing else, and post-increment is usually welcome.)

    Currently it’s just a ForwardIterator (or would be, if it was tagged). You could make it a RandomAccessIterator easily enough, but you’d have to give the end iterator the correct size. If you used a container of char(*)[block_size] instead of a container of void*, then I think you could just use a boost::counting_iterator<char(*)[block_size]> to populate it.

    Fundamentally, though, std::list is moderately slow at this. Unless you’re going to do insert/remove in the middle (which seems unnecessary for a memory pool free list – if all the blocks are the same size you should be able to always add and remove at the end), you might do better with a vector or deque, or at least with an intrusive linked list.

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

Sidebar

Related Questions

I have a program written in VB.Net (Visual Studio 2008) that uses a DLL
I have visual studio 2008. I have a written a small C program. I
I'm using Visual Studio 2008. I have a DLL and a test program EXE.
Visual Studio C++ 2008 / GCC 4.4.2 I have written a program to run
I have created a custom control using VB.NET in Visual Studio 2008 that gives
I'm stuck and I need help. Anyway I have Visual Studio 2008 console program
I have a Visual Studio 2008 C# Project. Here is what works: It runs
I have a relatively large C#/WPF Visual Studio 2008 solution that I am trying
I'm using Visual Studio 2008. I'm aware that std::vector has bounds checking with the
Dear All: I have a C++ project (in Visual Studio 2008) that comprises of

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.