 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Daniel Mark Guest
|
Posted: Fri Jul 29, 2005 10:02 pm Post subject: How to compute the size of class |
|
|
Hello:
Does anyone know what the size of this class?
class T {
char a;
double b;
char c;
class T *p
};
thank you
-Daniel
|
|
| Back to top |
|
 |
Phlip Guest
|
Posted: Fri Jul 29, 2005 10:05 pm Post subject: Re: How to compute the size of class |
|
|
Daniel Mark wrote:
| Quote: | Does anyone know what the size of this class?
class T {
char a;
double b;
char c;
class T *p
};
|
Is this for a quiz?
sizeof(T) or T aT; sizeof aT
--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
|
|
| Back to top |
|
 |
Julián Albo Guest
|
Posted: Fri Jul 29, 2005 10:05 pm Post subject: Re: How to compute the size of class |
|
|
Daniel Mark wrote:
| Quote: | Does anyone know what the size of this class?
|
sizeof (T)
--
Salu2
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Fri Jul 29, 2005 10:05 pm Post subject: Re: How to compute the size of class |
|
|
Daniel Mark wrote:
| Quote: | Does anyone know what the size of this class?
class T {
char a;
double b;
char c;
class T *p
|
;
Fix the semicolon, then the size is 'sizeof(T)'
V
|
|
| Back to top |
|
 |
Daniel Mark Guest
|
Posted: Fri Jul 29, 2005 10:14 pm Post subject: Re: How to compute the size of class |
|
|
Hello all:
I want to know how to compute manually?
thank you
|
|
| Back to top |
|
 |
Andre Kostur Guest
|
Posted: Fri Jul 29, 2005 10:20 pm Post subject: Re: How to compute the size of class |
|
|
"Daniel Mark" <danielmarkhot (AT) gmail (DOT) com> wrote in
news:1122675260.216242.15110 (AT) g47g2000cwa (DOT) googlegroups.com:
| Quote: | 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.
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Fri Jul 29, 2005 10:23 pm Post subject: Re: How to compute the size of class |
|
|
Daniel Mark wrote:
| Quote: | I want to know how to compute manually?
|
What for?
|
|
| Back to top |
|
 |
Julián Albo Guest
|
Posted: Fri Jul 29, 2005 10:36 pm Post subject: Re: How to compute the size of class |
|
|
Daniel Mark wrote:
| Quote: | 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.
--
Salu2
|
|
| Back to top |
|
 |
Alan Johnson Guest
|
Posted: Sat Jul 30, 2005 12:52 am Post subject: Re: How to compute the size of class |
|
|
Daniel Mark wrote:
| Quote: | 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.
|
|
| Back to top |
|
 |
Gianni Mariani Guest
|
Posted: Sat Jul 30, 2005 3:08 am Post subject: Re: How to compute the size of class |
|
|
Daniel Mark wrote:
| Quote: | 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
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();
}
|
|
| Back to top |
|
 |
Phil Staite Guest
|
Posted: Sat Jul 30, 2005 4:08 am Post subject: Re: How to compute the size of class |
|
|
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...
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|