How to compute the size of class

D

Daniel Mark

Hello:

Does anyone know what the size of this class?


class T {
char a;
double b;
char c;
class T *p
};


thank you
-Daniel
 
A

Andre Kostur

Hello all:

I want to know how to compute manually?

Why would you want to? There are far too many platform-dependancies in
your question for us to answer.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Daniel said:
I want to know how to compute manually?

Take your compiler manual, see the size of each of the types used,
investigate when padding bytes are added in structs and classes. Then
consider that the result will be not valid with other compiler, or even in
the same compiler with different options.
 
A

Alan Johnson

Daniel said:
class T {
char a;
double b;
char c;
class T *p
};
Hello all:

I want to know how to compute manually?

thank you

Without a lot more information, there is no answer we can give that is
guaranteed to be correct. But here are some of the issues you need to
consider. First, find out what the size of each of the contributing
types is.
-A char is (I think) guaranteed to be 1 byte.
-A common size for double is 8.
-A common size for pointers is 4.

So, if we go with those numbers, the minimum size for your struct is
1+8+1+4 = 14 bytes. Is this correct for your system? Probably not.
Many systems require values to be aligned on certain byte boundaries.
For example, at least one system I work with requires doubles and
pointers to be aligned on a 4 byte boundary. So let's assume that is
the case. Now you have 1 + 3(padding) + 8 + 1 + 3(padding) + 4 = 20 bytes.

Is that correct for your system? Again, who knows? The compiler may
reserve some more space for padding at the end if it thinks it is
necessary for some reason.
 
G

Gianni Mariani

Daniel said:
Hello:

Does anyone know what the size of this class?


class T {
char a;
double b;
char c;
class T *p
};


thank you
-Daniel

This should give you an idea. I don't know of any implementation that
does not use the algorithm depicted by ComputeAlignment().

You'll need to know the "packing" of the struct (#pragma pack) which is
the 2 raised to the i_max_align parameter.


#include <algorithm>
#include <iostream>

struct SizeAlignment
{
int m_size;
int m_alignment; /** alignment is a power of 2 */

SizeAlignment( int i_size = 1, int i_alignment = 0 )
: m_size( i_size ),
m_alignment( i_alignment )
{
}

};

struct Offset
{
int m_offset;
SizeAlignment m_size_alignment;

Offset( const SizeAlignment & i_size_alignment )
: m_size_alignment( i_size_alignment )
{
}

};


int Align( int i_pos, int i_alignment )
{
int i_mask = 1 << i_alignment;

return 1 + ( ( i_pos - 1 ) | ( i_mask - 1 ) );
}


template <typename T>
SizeAlignment ComputeAlignment(
const T & i_begin,
const T & i_end,
int i_max_align = 2 // pack value
) {

Offset l_sa( SizeAlignment( 0, 0 ) );

T l_iter = i_begin;

while ( l_iter != i_end )
{
int l_new_alignment =
std::min( i_max_align, l_iter->m_size_alignment.m_alignment );

l_iter->m_offset =
Align( l_sa.m_size_alignment.m_size, l_new_alignment );

l_sa.m_size_alignment.m_alignment =
std::max( l_sa.m_size_alignment.m_alignment, l_new_alignment );

l_sa.m_size_alignment.m_size =
l_iter->m_offset + l_iter->m_size_alignment.m_size;

++ l_iter;
}

l_sa.m_size_alignment.m_size =
Align( l_sa.m_size_alignment.m_size,
l_sa.m_size_alignment.m_alignment );

return l_sa.m_size_alignment;

}


// These defs are system dependant - define these for all
// the system types for each platform. Some platforms will
// have ways of determining the alignments.
SizeAlignment g_Align_char( sizeof( char ), 0 );
SizeAlignment g_Align_short( sizeof( short ), 1 );
SizeAlignment g_Align_int( sizeof( int ), 2 );


template <typename T, int N >
T * End( T (& i_array )[ N ] )
{
return N + i_array;
}

template <typename T, int N >
int Nelem( T (& i_array )[ N ] )
{
return N;
}

void test()
{

// Create a struct with a char, a short and three following chars
//
Offset l_struct[] = {
Offset( g_Align_char ),
Offset( g_Align_short ),
Offset( g_Align_char ),
Offset( g_Align_char ),
Offset( g_Align_char ),
};

// compute the alignment with a maximum packing 4 (2^2).
SizeAlignment l_sa =
ComputeAlignment( & l_struct[0], End( l_struct ), 2 );

// print out the results.
for ( int i = 0; i < Nelem( l_struct ); ++ i )
{
std::cout
<< "Offset element "
<< i
<< " "
<< l_struct[ i ].m_offset << "\n";
}

std::cout
<< "Struct size "
<< l_sa.m_size
<< " alignment "
<< l_sa.m_alignment << "\n";

}

int main()
{
test();
}
 
P

Phil Staite

Then you'll need to hit the docs for your compiler. Find out the size
of the various data members. Then you'll have to find out what
padding/alignment scheme (if any) is in use, and if that is alterable
via a #pragma directive...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top