Exercise 3-6 (Pets, data types addresses, memory layout)
Chapter_3 Exercise_3-5 YourPets | Function_call Exercise_3-7 |
Exercise 3-6 TCP1, p. 227
Exercise 3-6. Modify YourPets2.cpp (see ch3-YourPets) so that it uses various different data types (char, int, float, double, and their variants). Run the program and create a map of the resulting memory layout. If you have access to more than one kind of machine, operating system, or compiler, try this experiment with as many variations as you can manage.
Pets.cpp TCP1, p. 146 download
// tested in Lubuntu Linux
#include <iostream>
using std::cout;
using std::endl;
char canary;
unsigned char parrot;
short pup;
unsigned short puppy;
int cat;
unsigned dog;
long lizard;
unsigned long grassLizard;
long long snake;
unsigned long long python;
float bird;
double fish;
long double pony, horse;
// void f(); // cannot take the address of an undefined function
void f1 (void) {}
void f2 (int pet)
{
cout << "pet id number: " << pet << endl;
}
int f3 (int) {return 0;}
int f4 (int, int) {return 0;}
int f5 (int, int) {return 0;}
int main()
{
char c;
unsigned char cu;
short sh;
unsigned short shu;
int i;
unsigned iu;
long l;
unsigned long lu;
long long ll;
unsigned long long llu;
float f;
double d;
long double ld1, ld2;
cout << "f1(): " << (long)&f1 << endl; // 11 bytes
cout << "f2(): " << (long)&f2 << endl; // longest definition (74 bytes)
cout << "f3(): " << (long)&f3 << endl; // 18 bytes
cout << "f4(): " << (long)&f4 << endl; // 21 bytes
cout << "f5(): " << (long)&f5 << endl; // same as f4()
cout << endl;
cout << "canary: " << (long)&canary << endl; // 1 byte (char)
cout << "parrot: " << (long)&parrot << endl; // 1 byte (unsigned char)
cout << "pup: " << (long)&pup << endl; // 2 bytes (short)
cout << "puppy: " << (long)&puppy << endl; // 2 bytes (unsigned short)
cout << "cat: " << (long)&cat << endl; // 4 bytes (int)
cout << "dog: " << (long)&dog << endl; // 4 bytes (unsigned int)
cout << "lizard: " << (long)&lizard << endl; // 8 bytes (long)
cout << "grassLizard: " << (long)&grassLizard << endl; // 8B (UL)
cout << "snake: " << (long)&snake << endl; // 8 bytes (long long)
cout << "python: " << (long)&python << endl; // 8 bytes (ULL)
cout << "bird: " << (long)&bird << endl; // global float (8B) aligned
cout << "fish: " << (long)&fish << endl; // as expected, 8 bytes (double)
cout << "pony: " << (long)&pony << endl; // 16 bytes (long double)
cout << "horse: " << (long)&horse << endl; // same as pony
cout << endl;
cout << "c: " << (long)&c << endl; // 1 byte (char)
cout << "cu: " << (long)&cu << endl; // 1 byte (unsigned char)
cout << "sh: " << (long)&sh << endl; // 2 bytes (short)
cout << "shu: " << (long)&shu << endl; // 2 bytes (unsigned short)
cout << "i: " << (long)&i << endl; // 4 bytes (int)
cout << "iu: " << (long)&iu << endl; // 4 bytes (unsigned int)
cout << "f: " << (long)&f << endl; // local float (4B) is aligned here,
cout << "l: " << (long)&l << endl; // although defined after `llu'
cout << "lu: " << (long)&lu << endl; // 8 bytes, (unsigned) long
cout << "ll: " << (long)&ll << endl; // 8 bytes (long long)
cout << "llu: " << (long)&llu << endl; // 8 bytes (ULL)
cout << "d: " << (long)&d << endl; // 8 bytes (double)
cout << "ld1: " << (long)&ld1 << endl; // 16 bytes (long double)
cout << "ld2: " << (long)&ld2 << endl; // same as ld1
return 0;
}
/*
g++ Pets.cpp -o Pets
./Pets
f1(): 94899903296009
f2(): 94899903296020
f3(): 94899903296094
f4(): 94899903296112
f5(): 94899903296133
canary: 94899903308128
parrot: 94899903308129
pup: 94899903308130
puppy: 94899903308132
cat: 94899903308136
dog: 94899903308140
lizard: 94899903308144
grassLizard: 94899903308152
snake: 94899903308160
python: 94899903308168
bird: 94899903308176
fish: 94899903308184
pony: 94899903308192
horse: 94899903308208
c: 140733349822726
cu: 140733349822727
sh: 140733349822728
shu: 140733349822730
i: 140733349822732
iu: 140733349822736
f: 140733349822740
l: 140733349822744
lu: 140733349822752
ll: 140733349822760
llu: 140733349822768
d: 140733349822776
ld1: 140733349822784
ld2: 140733349822800
*/
Chapter_3 Exercise_3-5 YourPets | BACK_TO_TOP | Function_call Exercise_3-7 |
Comments
Post a Comment