Click here to Skip to main content
15,888,270 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, How can i get the size of my own list like sizeof struct. I know how sizeof works for example.
C++
enum NETCDF_Type
	{
		NETCDF_NONE,      //!< unknown data foramt
		NETCDF_STRING,  
		NETCDF_UCHAR,		//!< signed 1 byte integer
		NETCDF_CHAR,		//!< signed 1 byte integer
		NETCDF_SHORT, 	//!< signed 2 byte integer
		NETCDF_INT,		//!< signed 4 byte integer
		NETCDF_USHORT,	//!< unsigned 2-byte int
		NETCDF_UINT,		//!< unsigned 4-byte int
		NETCDF_FLOAT32,	//!< signed 4-byte int
		NETCDF_DOUBLE64,	//!< signed 8-byte int
		NETCDF_LONGLONG,
		NETCDF_ULONGLONG
	};

void getSize( const QList< NETCDF_Type > myList )
{
     for( int i = 0; i < myList.count(); i++ )
     {
          //do something
          //I need a size of which NETCDF_Type i am using.
          //Without sizeof();
     }
}


What I have tried:

C++
struct MyStruct
{
     char v1;
     char v2;
     char v3;
     short v4;
};

int mSize = sizeof( MyStruct ); mSize => 6;

struct MyStruct
{
     char v1;
     char v2;
     char v3;
     char v4;
     short v;
};


int mSize = sizeof( MyStruct ); mSize => 6;
Posted
Updated 23-Nov-17 20:12pm
v4
Comments
Jochen Arndt 23-Nov-17 3:41am    
You have to clarify your question. With lists there are usually two sizes:
- The size of each item
- The number of items actually stored in the list

The first is defined by the item type while the second is managed by the list (stored in a member variable). A list implementations has usually a function to access the member variable which is often called GetCount().

Most lists track also the allocation in a separate variable. But that is only of interest when memory usage / performance matters.
Sukerbek 24-Nov-17 2:13am    
Thank you so much , i have updated my question take a look please.
Jochen Arndt 24-Nov-17 3:22am    
It is still unclear because we don't know what size do you want and why you can't use sizeof().

All you have shown us is an enumeration. Those are just int. If you need the size of the data which are described by the enum, you have to show us the corresponding data definitions.

For all types except NETCDF_STRING, sizeof() will do the work:
NETCDF_LONGLONG : sizeof(long long)

If you need platform independent sizes, you should use data types with fixed width like int32_t instead of int.
Sukerbek 30-Nov-17 20:37pm    
Thank you so much i have solved my problem with another way.

1 solution

It depends on your requirements, but the short answer is you cannot: the QList<T> is not a POD and, moreover, you do not have access to its implementation details. You can get an extimate of the memory usage multipling the size of each item by the size of the list, i.e. sizeof(T) * myqlist.size().


[update]
Quote:
//I need a size of which NETCDF_Type i am using.
//Without sizeof();
I would write a method for that, e.g.
C++
size_t getSize(NETCDF_Type typ);

The method implementation is up to you, in a straightforward approach, use a switch.
[/update]
 
Share this answer
 
v3
Comments
Sukerbek 24-Nov-17 2:14am    
Thank you so much , i have update my question take a look please.
Sukerbek 30-Nov-17 20:37pm    
Thank you so much i have solved my problem with another way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900