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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:59:52+00:00 2026-06-12T19:59:52+00:00

i will be using linked list example code seen here on stackoverflow to illustrate

  • 0

i will be using linked list example code seen here on stackoverflow to illustrate the problem.

my c++ written progam (x64) contains this linked list code :

old code snippets deleted; im sorry if comments doesnot make sense anymore.

added fully working code to show what my problem is.
compile : g++ linkedlist.cpp -o linked-list

#include <cstdlib>
#include <iostream>

using namespace std;


 struct node
{
    public :
          unsigned int part1; // 4 bytes
          unsigned int part2; // 4 bytes
          node *next;         //pointer, 8 bytes on 64 bit system
      unsigned int read_part1();
 };


struct LinkedList
 {
     public:
     LinkedList();
          void insert(unsigned int data[], unsigned int data1);
          bool isEmpty() const;
          node* head;
 };

unsigned int node::read_part1() {
return part1;
}
 LinkedList::LinkedList():
 head(NULL)
{
}

bool LinkedList::isEmpty() const
{
  return (head == NULL);
}

  void LinkedList::insert(unsigned int data[], unsigned int data1)
 {



    node* oldHead = head;
    node* newHead = new node();
    newHead->part1 = data[0];
    newHead->part2 = data1;
    newHead->next = oldHead;
    head = newHead;

  }

unsigned int allocations = 300000000;
unsigned int index_size = 430000000;//index of lists, 430m,.
                    //will be creatad on heap
    LinkedList *list = NULL;





int main(int argc, char *argv[])

{
LinkedList list_instance;

cout << "1 LinkedList instance takes [" << sizeof(list_instance) << "] bytes in memory!"<< endl;

node node_instance;

cout << "1 node instance takes [" << sizeof(node_instance) <<"] bytes in memory !"<< endl;


    try{
    list = new LinkedList[index_size];
    }

     catch(std::bad_alloc) {
    cout << "Error allocating memory" << endl;
     return 0;
    //reboot code
    }

unsigned int some_data[] = {00, 01};
unsigned int index;

LinkedList *list_instance2 = NULL;



cout << "Allocating ..." << endl;

for (int i=0; i<allocations; i++)
{

index = rand();
unsigned short inde = (unsigned short)index;
list_instance2 = &list[inde];

list_instance2->insert(some_data, some_data[1]);


}

unsigned long sk = ((allocations * sizeof(node_instance) + index_size*sizeof(list_instance))) / (1024*1024*1024);

cout << "This process *should* consume around " << sk <<" GBytes of memory, but does it ?"<< endl;

cout << "Allocating done, *check the process size* and press any number key + ENTER to exit ..." << endl;

int u=0;
cin >> u;


return 0;
}

compile it, run it and see if your process size even remotely matches whats expected.
if not – where is the problem ?

oh, and i run it on 64 bit slackware 13.37 with a unmodified default kernel.

  • 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-12T19:59:54+00:00Added an answer on June 12, 2026 at 7:59 pm

    On my box, with slightly modified source (see below with notes)

    • it uses 1243 MiB rather than the ‘expected’ 785 MiB using the standard library heap routines
    • it uses 791 MiB when using Google’s tcmalloc
    • it uses 840 MiB when using Boost Object Pool to allocate nodes (with standard library heap or tcmalloc)

    The overhead is very clearly in the implementation of the heap routines.

    Here’s the code:

    • Note the use of new (nothrow) there.
    • Also, the baseline measurement at the start (I used pmap $(pgrep test) | tail on linux)
    • Note the choice in insert:

      void LinkedList::insert(unsigned int data[], unsigned int data1)
      {
      #if 1
          head = new node { data[0], data1, head };
      #else
          static boost::object_pool<node> node_pool;
      
          node* add = node_pool.malloc();
          *add = node { data[0], data1, head };
          head = add;
      #endif
      }
      

      Change #if 1 to #if 0 to use the Boost Object Pool

    • There was a strangeness in the node allocation loop

      index = rand();
      unsigned short inde = (unsigned short)index;
      list_instance2 = &list[inde];
      list_instance2->insert(some_data, some_data[1]);
      

      I changed it to what you probably intended:

      list[rand() % index_size].insert(some_data, some_data[1]);
      

    #include <stdlib.h>
    #include <iostream>
    #include <boost/pool/object_pool.hpp>
    
    using namespace std;
    
    struct node
    {
        unsigned int part1; // 4 bytes
        unsigned int part2; // 4 bytes
        node *next;         //pointer, 8 bytes on 64 bit system
    };
    
    struct LinkedList
    {
    public:
        LinkedList();
        void insert(unsigned int data[], unsigned int data1);
        bool isEmpty() const;
        node* head;
    };
    
    LinkedList::LinkedList():
        head(NULL)
    {
    }
    
    bool LinkedList::isEmpty() const
    {
        return (head == NULL);
    }
    
    void LinkedList::insert(unsigned int data[], unsigned int data1)
    {
    #if 1
        head = new node { data[0], data1, head };
    #else
        static boost::object_pool<node> node_pool;
    
        node* add = node_pool.malloc();
        *add = node { data[0], data1, head };
        head = add;
    #endif
    }
    
    const unsigned int allocations = 30000000;
    const unsigned int index_size = 43000000;//index of lists
    //will be creatad on heap
    LinkedList *list = NULL;
    
    int main(int argc, char *argv[])
    {
        LinkedList list_instance;
        cout << "1 LinkedList instance takes [" << sizeof(list_instance) << "] bytes in memory!"<< endl;
        node node_instance;
        cout << "1 node instance takes [" << sizeof(node_instance) <<"] bytes in memory !"<< endl;
        cout << "Before dynamic allocations: *check the baseline process size* and press ENTER to start allocating ..." << endl;
        std::string s;
        std::getline(std::cin, s);
        list = new (nothrow) LinkedList[index_size];
        if (!list)
        {
            cout << "Error allocating memory" << endl;
            return 1;
        }
        unsigned int some_data[] = {00, 01};
        cout << "Allocating nodes ..." << endl;
        for (unsigned int i=0; i<allocations; i++)
        {
            list[rand() % index_size].insert(some_data, some_data[1]);
        }
        unsigned long sk = ((allocations * sizeof(node_instance) + index_size*sizeof(list_instance))) >> 20;
        cout << "This process *should* consume around " << sk <<" MiB of memory, but does it ?"<< endl;
        cout << "Allocating done, *check the process size* and press ENTER to exit ..." << endl;
        std::getline(std::cin, s);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Will using GetComInterfaceForObject and passing the returned IntPtr to unmanaged code keep the managed
Possible Duplicate: How to determine if a linked list has a cycle using only
I'm trying to make linked list similar too the one here: linked list in
Good day! Our teacher asked us to make a student list using a linked
I am to generate a solution to a maze using a linked list implementation
I need to Sort Linked list using Selection sort. But I can not use
This is a program trying to make a linked list. #include <iostream> using namespace
I've seen quite a few example C implementations of linked lists on this site,
Possible Duplicate: Copy a linked list Hello stackoverflow! I am trying to learn more
Will using PinchMedia and including Core Location frameworks make it unusable on the iPod

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.