Mail Archives: djgpp/2001/11/06/09:19:03
Martin Stromberg wrote:
> Alex Vinokur (alexvn AT bigfoot DOT com) wrote:
[snip]
>
> : : Struct type1 :
> : sizeof = 52
> : All items sum = 43
> : Diff (52 - 43) = 9
> : All holes sum = 6
>
> : 2. struct type1 :
> : Why is not 'Diff' (between 'sizeof' and 'All items sum')
> : equal 'All holes sum' ?
> : Note. 'Diff' = 9, 'All holes sum' = 6.
> : Where has 3 bytes ('Diff' - 'All holes sum') gone?
>
> It's padding after item15 I guess.
>
> Right,
>
> MartinS
You are right. Thanks.
Here is updated program and run log file.
#########################################################
################### C++ code : BEGIN ####################
// File main.c
#include <string>
#include <iostream>
#include <typeinfo>
#include <iomanip.h>
//===========================================
//================= DEFINES =================
//===========================================
#define ITEM_INTERNAL_ADDRESS(s, item) &(((s*)0)->item)
#define ITEM_ITSELF(s, item) ((s*)0)->item
#define SHOW_ITEM_INFO(s, item) \
show_item_info ( \
#s, \
#item, \
gstr_builtin_typename(ITEM_ITSELF(s, item)), \
(size_t)ITEM_INTERNAL_ADDRESS(s, item), \
sizeof (ITEM_ITSELF(s, item)) \
)
#define SHOW_ENDOF_INFO(s) \
show_endof_info ( \
#s, \
sizeof(s) \
)
#define SHOW_STRUCT_INFO(s) \
show_struct_info ( \
#s, \
sizeof(s) \
)
//===========================================
//============= STATIC VARIABLES ============
//===========================================
static size_t prev_item_size_s = 0;
static size_t prev_item_address_s = 0;
static size_t size_of_all_items_s = 0;
static size_t sum_of_all_holes_s = 0;
//===========================================
//=========== AUXILIARY FUNCTIONS ===========
//===========================================
//--------------------------------
void show_item_info (
const string& struct_name_i,
const string& item_name_i,
const string& item_type_name_i,
const size_t item_address_i,
const size_t item_sizeof_i
)
//--------------------------------
{
size_t hole_value = 0;
hole_value = item_address_i - (prev_item_address_s + prev_item_size_s);
cout.setf (ios::left, ios::adjustfield);
cout << struct_name_i
<< "."
<< setw(6)
<< item_name_i.c_str()
<< setw(10)
<< string (" (" + item_type_name_i + ")").c_str()
<< "-> Address = "
<< setw(3)
<< item_address_i
<< " : sizeof = "
<< item_sizeof_i
<< " : ";
if (hole_value)
{
cout << "Hole = " << hole_value << " (before item)" ;
}
else
{
cout << "No hole";
}
cout << endl;
//--------------
prev_item_size_s = item_sizeof_i;
prev_item_address_s = item_address_i;
size_of_all_items_s += item_sizeof_i;
sum_of_all_holes_s += hole_value;
//--------------
} // show_item_info
//--------------------------------
void show_endof_info (
const string& struct_name_i,
const size_t struct_sizeof_i
)
//--------------------------------
{
size_t hole_value = 0;
hole_value = struct_sizeof_i - (prev_item_address_s + prev_item_size_s);
cout.setf (ios::left, ios::adjustfield);
cout << struct_name_i
<< "."
<< setw(45)
<< ""
<< " : ";
if (hole_value)
{
cout << "Hole = " << hole_value << " (before end)" ;
}
else
{
cout << "No hole";
}
cout << endl;
//--------------
sum_of_all_holes_s += hole_value;
//--------------
} // show_endof_info
//--------------------------------
void show_struct_info (
const string& struct_type_name_i,
const size_t struct_sizeof_i
)
//--------------------------------
{
cout << "Struct "
<< struct_type_name_i
<< " : "
<< endl
<< "\t"
<< "sizeof\t\t = "
<< struct_sizeof_i
<< endl
<< "\t"
<< "All items sum\t = "
<< size_of_all_items_s
<< endl
<< "\t"
<< "Diff ("
<< struct_sizeof_i
<< " - "
<< size_of_all_items_s
<< ")\t = "
<< (struct_sizeof_i - size_of_all_items_s)
<< endl
<< "\t"
<< "All holes sum\t = "
<< sum_of_all_holes_s
<< endl;
} // show_struct_info
//--------------------------------
void init_counters ()
//--------------------------------
{
prev_item_size_s = 0;
prev_item_address_s = 0;
size_of_all_items_s = 0;
sum_of_all_holes_s = 0;
} // init_counters
//--------------------------------
#define IF_TYPE(x) if (typeid(T) == typeid(x)) return #x
template <typename T>
string gstr_builtin_typename(const T&)
//--------------------------------
{
IF_TYPE(char);
IF_TYPE(short);
IF_TYPE(int);
IF_TYPE(long);
IF_TYPE(float);
IF_TYPE(double);
return "Undefined typename";
} // gstr_builtin_typenameconst
//===========================================
//============= TESTED STRUCTURES ===========
//===========================================
//-----------
struct type0
{
};
//-----------
struct type1
{
long item1;
char item2;
short item3;
char item4;
long item5;
long item6;
int item7;
short item8;
short item9;
char item10;
char item11;
long item12;
float item13;
double item14;
char item15;
};
//===========================================
//============== MAIN FUNCTION ==============
//===========================================
int main ()
{
//------ struct type1 ------
cout << "\t### Test for struct type1 ###" << endl;
init_counters ();
SHOW_ITEM_INFO (type1, item1);
SHOW_ITEM_INFO (type1, item2);
SHOW_ITEM_INFO (type1, item3);
SHOW_ITEM_INFO (type1, item4);
SHOW_ITEM_INFO (type1, item5);
SHOW_ITEM_INFO (type1, item6);
SHOW_ITEM_INFO (type1, item7);
SHOW_ITEM_INFO (type1, item8);
SHOW_ITEM_INFO (type1, item9);
SHOW_ITEM_INFO (type1, item10);
SHOW_ITEM_INFO (type1, item11);
SHOW_ITEM_INFO (type1, item12);
SHOW_ITEM_INFO (type1, item13);
SHOW_ITEM_INFO (type1, item14);
SHOW_ITEM_INFO (type1, item15);
SHOW_ENDOF_INFO(type1);
cout << endl;
SHOW_STRUCT_INFO (type1);
//------ struct type0 ------
cout << endl;
cout << endl;
cout << "\t### Test for struct type0 ###" << endl;
init_counters ();
SHOW_ENDOF_INFO(type0); \
SHOW_STRUCT_INFO (type0);
return 0;
}
#################### C++ code : END #####################
#########################################################
############### Running (log file) : BEGIN ##############
%a.exe
### Test for struct type1 ###
type1.item1 (long) -> Address = 0 : sizeof = 4 : No hole
type1.item2 (char) -> Address = 4 : sizeof = 1 : No hole
type1.item3 (short) -> Address = 6 : sizeof = 2 : Hole = 1 (before item)
type1.item4 (char) -> Address = 8 : sizeof = 1 : No hole
type1.item5 (long) -> Address = 12 : sizeof = 4 : Hole = 3 (before item)
type1.item6 (long) -> Address = 16 : sizeof = 4 : No hole
type1.item7 (int) -> Address = 20 : sizeof = 4 : No hole
type1.item8 (short) -> Address = 24 : sizeof = 2 : No hole
type1.item9 (short) -> Address = 26 : sizeof = 2 : No hole
type1.item10 (char) -> Address = 28 : sizeof = 1 : No hole
type1.item11 (char) -> Address = 29 : sizeof = 1 : No hole
type1.item12 (long) -> Address = 32 : sizeof = 4 : Hole = 2 (before item)
type1.item13 (float) -> Address = 36 : sizeof = 4 : No hole
type1.item14 (double) -> Address = 40 : sizeof = 8 : No hole
type1.item15 (char) -> Address = 48 : sizeof = 1 : No hole
type1. : Hole = 3 (before end)
Struct type1 :
sizeof = 52
All items sum = 43
Diff (52 - 43) = 9
All holes sum = 9
### Test for struct type0 ###
type0. : Hole = 1 (before end)
Struct type0 :
sizeof = 1
All items sum = 0
Diff (1 - 0) = 1
All holes sum = 1
################ Running (log file) : END ###############
===========================
Alex Vinokur
mailto:alexvn AT bigfoot DOT com
mailto:alexvn AT dr DOT com
http://up.to/alexvn
http://go.to/alexv_math
===========================
- Raw text -