ch3-PointerIncrement for built-in types and struct

Chapter_3     Exercise_3-29 Exercise_3-23     Exercise_3-30







CONTENTS:     PointerIncrement1.cpp     PointerIncrement2.cpp




PointerIncrement1.cpp     TCP1, p. 205         download


#include <iostream>
using std::cout;
using std::endl;

int main()
{
int i[10];
double d[10];
int* ip = i;
double* dp = d;

cout << "sizeof(int) = " << sizeof(int) << endl;
cout << "ip = " << (long)ip << endl;
ip++;
cout << "ip = " << (long)ip << endl;
ip--;
cout << "ip = " << (long)ip << endl;
cout << "ip+2 = " << (long)(ip+2) << endl;
cout << endl;

cout << "sizeof(double) = " << sizeof(double) << endl;
cout << "dp = " << (long)dp << endl;
dp++;
cout << "dp = " << (long)dp << endl;
dp--;
cout << "dp = " << (long)dp << endl;
cout << "dp-1 = " << (long)(dp-1) << endl;

return 0;
}
/*
g++ PointerIncrement1.cpp -o PointerIncrement1
./PointerIncrement1
sizeof(int) = 4
ip = 140731920911760
ip = 140731920911764
ip = 140731920911760
ip+2 = 140731920911768

sizeof(double) = 8
dp = 140731920911808
dp = 140731920911816
dp = 140731920911808
dp-1 = 140731920911800
*/





Note:  See also Exercise_3-18.











PointerIncrement2.cpp     TCP1, p. 205-206         download


#include <iostream>
using std::cout;
using std::endl;

typedef struct
{ // word size = 8 bytes alignment?
char c; // sizeof(char) = 1 byte (next is 2B, so 1B padding), 2B total
short s; // 2 bytes (next 4B, currently 4B total, no padding here), 4B
int i; // 4 bytes (next 8B, 8B so far, no padding here), 8B
long l; // 8 bytes (16B total, multiple of 8, no padding here), 16B
float f; // 4 bytes (20B so far, 4B padding), 24B total (multiple of 8)
double d; // 8 bytes (32B total, multiple of 8 and 16, no padding), 32B
long double ld; // 16 bytes (forces 16B alignment), 48B
} Primitives; // 48 bytes in total (multiple of 16, largest member)

int main()
{
Primitives p[10];
Primitives* pp = p;
cout << "word size: sizeof(size_t) = " << sizeof(size_t) << endl;
cout << "sizeof(Primitives) = " << sizeof(Primitives) << endl;
cout << "p = " << (long)p << endl;
cout << "pp = " << (long)pp << endl;
// p++; // compile error: not lvalue
pp++;
cout << "After increment:" << endl;
cout << "p = " << (long)p << endl;
cout << "pp = " << (long)pp << endl;
// p--; // compile error: not lvalue
pp--;
cout << "After decrement:" << endl;
cout << "p = " << (long)p << endl;
cout << "pp = " << (long)pp << endl;
cout << "p+2 = " << (long)(p+2) << endl;
cout << "pp+2 = " << (long)(pp+2) << endl;
cout << "p-1 = " << (long)(p-1) << endl;
cout << "pp-1 = " << (long)(pp-1) << endl;

return 0;
}
/*
g++ PointerIncrement2.cpp -o PointerIncrement2
./PointerIncrement2
word size: sizeof(size_t) = 8
sizeof(Primitives) = 48
p = 140737044809504
pp = 140737044809504
After increment:
p = 140737044809504
pp = 140737044809552
After decrement:
p = 140737044809504
pp = 140737044809504
p+2 = 140737044809600
pp+2 = 140737044809600
p-1 = 140737044809456
pp-1 = 140737044809456
*/





Notes:  See also ch3-sizeof in Section ch3-Data_Types.

size_t is defined as long unsigned int in stddef.h, on disk /usr/lib/gcc/x86_64-linux-gnu/9/include/stddef.h (see also stdint.h, on disk /usr/include/stdint.h).

See How_to_detect_machine_word_size on StackOverflow and Data_structure_alignment on Wikipedia.

Check these structs:

typedef struct { // word size = 8 bytes alignment? char c; // sizeof(char) = 1 byte } Primitives; // 1 byte in total (multiple of 1, size of largest member)

typedef struct { // word size = 8 bytes alignment? short s; // sizeof(short) = 2 bytes } Primitives; // 2 bytes in total (multiple of 2)

typedef struct { // word size = 8 bytes alignment? char c; // sizeof(char) = 1 byte (next 2B, 1B padding) short s; // sizeof(short) = 2 bytes } Primitives; // 4 bytes in total (multiple of 2, largest member)

typedef struct { // word size = 8 bytes alignment? char c; // sizeof(char) = 1 byte (next 16B, 15B padding) long double ld; // sizeof(long double) = 16 bytes } Primitives; // 32 bytes in total (multiple of 16, largest member)









Chapter_3     Exercise_3-29 BACK_TO_TOP Exercise_3-23     Exercise_3-30



Comments

Popular posts from this blog

Contents