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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:06:07+00:00 2026-06-08T00:06:07+00:00

Because list have one more pointer(previous pointer) than forward_list, so if they both hold

  • 0

Because list have one more pointer(previous pointer) than forward_list, so if they both hold the same number of element, i.e. 1<<30, list will use almost 1/3 more memory. Right?

Then if I repeat calling resize larger and larger, forward_list must be able to resize much larger than list.

Test code:

#include<forward_list>
#include<list>
#include<iostream>
int main(){
    using namespace std;
    typedef list<char> list_t;
    //typedef forward_list<char> list_t;
    list_t l;
    list_t::size_type i = 0;
    try{
        while(1){
            l.resize(i += (1<<20));
            cerr<<i<<" ";
        }
    }
    catch(...){
        cerr<<endl;
    }
    return 0;
}

To my surprise, when the process is killed, they have almost the same size …
Anybody could interpret it?

  • 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-08T00:06:08+00:00Added an answer on June 8, 2026 at 12:06 am

    You should find that with better memory sniffing that your initial hypothesis that a std::list<T> will consume three times as much energy is correct. On my Windows machine, I whipped up a quick memory usage program using GetProcessMemoryInfo

    Here is the core of my program:

    int main()
    {
        size_t initMemory = MemoryUsage();
        std::list<unsigned char> linkedList;
    
        for (int i = 0; i < ITERATIONS; i++)
            linkedList.push_back(i % 256);
        size_t linkedListMemoryUsage = MemoryUsage() - initMemory;
    
        std::forward_list<unsigned char> forwardList;
        for (int i = 0; i < ITERATIONS; i++)
            forwardList.push_front(i % 256);
        size_t forwardListMemoryUsage = MemoryUsage() - linkedListMemoryUsage - initMemory;
    
        std::cout << "Bytes used by Linked List: " << linkedListMemoryUsage << std::endl;
        std::cout << "Bytes used by Forward List: " << forwardListMemoryUsage << std::endl;
    
        return 0;
    }
    

    Results when running it under release build:

    #define ITERATIONS 128
    Bytes used by Linked List: 24576
    Bytes used by Forward List: 8192
    8192 * 3 = 24576
    

    Here’s a quote from cplusplus.com that even says that there should be noticeable memory difference between the two containers.

    The main design difference between a forward_list container and a list
    container is that the first keeps internally only a link to the next
    element, while the latter keeps two links per element: one pointing to
    the next element and one to the preceding one, allowing efficient
    iteration in both directions, but consuming additional storage per
    element and with a slight higher time overhead inserting and removing
    elements. forward_list objects are thus more efficient than list
    objects, although they can only be iterated forwards.

    Using the resize function on the lists, as you do in the posted code, the memory difference was even more pronounced with std::list<T> consuming four times as much memory.

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

Sidebar

Related Questions

I have a list of strings in which one or more subsets of the
I have a question about list views. I hope someone knows the solution, because
Whenever I use permutations, I have to create a list of the permutations because
I have a User control (because I use the same in other page, so
Is there any non-awful way to have a collection of objects of more than
I have a list of words. Lets say they are 'Apple', 'Orange', and 'Pear'.
Suppose I have one list: IList<int> originalList = new List<int>(); originalList.add(1); originalList.add(5); originalList.add(10); And
Assumptions about the list (updated): It will not contain more than 10 list elements
I've written a simple linked list because a recent interview programming challenge showed me
Because I've overloaded the operator++ for an iterator class template<typename T> typename list<T>::iterator& list<T>::iterator::operator++()

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.