You are here:

C++/What is size of node in linklist program?


Sir,I am beginner of c++.I just want to know that what is exact size of following node structure to be passed in malloc() to create new node dynamically:-
struct node
{ int data;
 node *next,*prev;
When i passed size 1,then program runs currectly.that means the size of above node is 1,which would have been 6.

First it looks like you are learning C not C++.

In C++ we create single objects dynamically using the operator new and destroy them using the operator delete, for dynamically creating and destroying arrays of objects we use the new[] and delete[] operators. E.g.

   node * pointer_to_node = new node;

   // ...

   delete pointer_to_node;

Unlike the C library functions malloc, free, etc.:

   - new, delete, new[], delete[] are operators - they are part of the language and not supporting library functions
   - they do not require a size as you specify the type to create object(s) of as part of a new expression.
   - they return a pointer to one (or maybe more than one for operator new[]) object of the type created
      so no need to cast from void*
   - new and new[] allocate raw memory then create the object (or the objects for new[])
      by executing a creation function called a constructor.
   - delete and delete[] first destroy the object or object by calling a destruction function
      called a destructor and then release the raw memory.
However to answer your question the size of an object of some type is given by the sizeof operator. A char is defined to have a size of 1 and sizeof returns the size of an object of some a type in units of char:

   std::size_t node_size = sizeof( node );  // returns size of objects of type node

(or in C:

   size_t node_size = sizeof( struct node );

   node n;

   std::size_t size_of_node = sizeof n;
(or in C:
   struct node n;

   size_t size_of_node = sizeof n;    // returns size of object n, which is of type node

The type alias size_t is defined in the header cstddef (or stddef.h for C). In C++ it is properly in the std namespace but you can generally use it without the std qualification as well.

Finally the size of 1 you use might have just worked in your program - which may have been simple and only allocated one node object - however it would likely have trashed some memory if you had actually used (written to) more than the one char-sized slot at the memory address pointed to by the pointer returned from malloc that you asked malloc for. That is if you ask for a pint pot and try to fit a quart of stuff in it bad things are likely to happen.

Finally I would suspect the size of a node object to be between 6 and 24 8-bit char objects depending on the platform the compiler was generating code for and possibly what options were in effect for the compilation. This should cater for 16-bit, 32-bit and 64-bit target machine architectures and maybe a few odder ones in between.


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.