Suppose you have a node declared like this:

struct node 
{
  int data;
  node * next;
};

and a list class declared like this:

struct myList
{
  node * head;
  node * tail;
  int numNodesInList;
  //member functions etc, but no other member variables.
};

Then what is the size of a myList object? Is it the size of three pointers (two member variables and the this pointer) plus the size of an int or is it the size of a node pointer or is it numNodesInList times the size of a node?

The reason I ask is a recent thread involved using an array of list objects and one of the people posting in that thread indicated that this:

myList array[10];

involved storing the head node of 10 myList objects in an array whereas I was skeptical of the entire construct because I felt there was no way of knowing the size of a list object because there is no way of knowing what the value of numNodesInList is at compile time.

However, now that I've had more time to think about this I suspect that the true answer is that the size of a myList object as declard above is the size of three pointers and an int. I am now reasonable comfortable with the idea that the number of nodes in the list and the memory each node takes up is relevant when contemplating how much memory the list uses in RAM, but the number of nodes in the list is not relevant when it comes to determining how much memory would need to be allocated to declare an array of lists (which would be the number of elements the array could hold times the memory needed to declare one element in the array), and that an array of myList objects as proposed by the initial poster does actually make sense.

I am happy to entertain any corrections or further discussion.

Recommended Answers

All 4 Replies

The size of myList is found by using the sizeof() operator. It should be a multiple of 4 bytes.

>> It should be a multiple of 4 bytes.
Depends on the compiler and how it aligns the structure object. You can change the alignment with most modern compilers so that it is 1, 2, 4, 8, 16, 32 etc aligned. The default alignment is compiler dependent too.

commented: Thanks for the info. +5

Here is a program, using your example structures, that will demonstrate for you:

#include <iostream>
using namespace std;
struct node 
{
  int data;
  node * next;
};

struct myList
{
  node * head;
  node * tail;
  int numNodesInList;
  //member functions etc, but no other member variables.
};

const int LIST_COUNT = 10;
int main(){       
  cout << "The size of struct node is: " << sizeof(node) << endl;
  cout << "The size of a node* is: " << sizeof(node*) << endl;
  cout << "The size of struct myList is: " << sizeof(myList) << endl;
  cout << "The size of a myList* is: " << sizeof(myList*) << endl;

  cout << "Declaring an array of " << LIST_COUNT << " myList objects..." << endl;
  myList listArray[LIST_COUNT];
  cout << "The size of listArray is: " << sizeof(listArray) << endl;
  return 0;
}

The size of struct node is: 8
The size of a node* is: 4
The size of struct myList is: 12
The size of a myList* is: 4
Declaring an array of 10 myList objects...
The size of listArray is: 120
Press any key to continue . . .

Note that all the sizes shown are calculated at compile time, not run time. Also note, that these are for VC++ 2008 YMMV.

thanks to one and all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.