[snip]
Here is a sample based on Siemel and Jonathan's postings.
[snip]
Here is an updated sample based on Siemel, Jonathan and Tom's postings.
========= foo.cpp : BEGIN =========
#include <iostream>
#include <iomanip>
using namespace std;
typedef unsigned long ulong;
#define COUT cout << "[Line-" \
<< setw(3) \
<< std::right \
<< __LINE__ \
<< "] "
#define SHOW(x, y, z, t) \
COUT << "" \
<< y \
<< setw(19) \
<< std::left \
<< #x \
<< z \
<< setw(9) \
<< std::left \
<< ulong(x) \
<< dec \
<< std::right \
<< t \
<< endl
#define SHOW_HEX(x,t) SHOW(x, hex, " = 0x", t)
#define SHOW_DEC(x,t) SHOW(x, dec, " = ", t)
int main()
{
#define ARRAY_ELEMS 5
int array[ARRAY_ELEMS] = { 1, 2, 3, 4, 5 };
int * ptr = array;
void * vp_array = array; // == &array[0]
void * vp_ptr = &ptr;
int * * ipp_vp_ptr = static_cast<int * *>(vp_ptr);
int * ip_ipp_vp_ptr = *ipp_vp_ptr;
int * ip_vp_array = static_cast<int *>(vp_array);
int * * ipp_vp_array = static_cast<int * *>(vp_array);
int * ip_ipp_vp_array = *ipp_vp_array;
cout << endl;
SHOW_DEC (sizeof(*ipp_vp_array), "");
int (* ipa_vp_array)[ARRAY_ELEMS] = static_cast<int (*)[ARRAY_ELEMS]>(vp_array);
int * ip_ipa_vp_array = *ipa_vp_array;
SHOW_DEC (sizeof(*ipa_vp_array), "");
cout << endl;
SHOW_HEX (&array[0], "");
SHOW_HEX (&array[1], "");
SHOW_HEX (&array[2], "");
SHOW_HEX (&array[3], "");
SHOW_HEX (&array[4], "");
cout << endl;
SHOW_HEX (array, "&array[0]");
SHOW_HEX (&array, "&array[0]");
SHOW_DEC (*array, "array[0]");
cout << endl;
SHOW_HEX (ptr, "&array[0]");
SHOW_HEX (&ptr, "*ptr == &array[0]");
SHOW_DEC (*ptr, "array[0]");
cout << endl;
SHOW_HEX (vp_array, "&array[0]");
SHOW_HEX (vp_ptr, "*vp_ptr == &array[0]");
SHOW_HEX (ipp_vp_ptr, "*ipp_vp_ptr == &array[0]");
SHOW_HEX (ip_ipp_vp_ptr, "&array[0]");
cout << endl;
cout << endl;
cout << "------ ip_vp_array ------" << endl;
cout << endl;
SHOW_HEX (ip_vp_array, "&array[0]");
SHOW_HEX (ip_vp_array+1, "&array[1]");
SHOW_HEX (ip_vp_array+2, "&array[2]");
SHOW_HEX (ip_vp_array+3, "&array[3]");
SHOW_HEX (ip_vp_array+4, "&array[4]");
cout << endl;
SHOW_DEC (ip_vp_array[0], "array[0]");
SHOW_DEC (ip_vp_array[1], "array[1]");
SHOW_DEC (ip_vp_array[2], "array[2]");
SHOW_DEC (ip_vp_array[3], "array[3]");
SHOW_DEC (ip_vp_array[4], "array[4]");
cout << endl;
cout << endl;
cout << "------ ipp_vp_array ------" << endl;
cout << endl;
SHOW_HEX (ipp_vp_array, "&array[0]");
SHOW_HEX (ipp_vp_array+1, "&array[1]");
SHOW_HEX (ipp_vp_array+2, "&array[2]");
SHOW_HEX (ipp_vp_array+3, "&array[3]");
SHOW_HEX (ipp_vp_array+4, "&array[4]");
cout << endl;
SHOW_DEC (*ipp_vp_array, "array[0]");
SHOW_DEC (*(ipp_vp_array+1), "array[1]");
SHOW_DEC (*(ipp_vp_array+2), "array[2]");
SHOW_DEC (*(ipp_vp_array+3), "array[3]");
SHOW_DEC (*(ipp_vp_array+4), "array[4]");
cout << endl;
cout << endl;
cout << "------ ip_ipp_vp_array ------" << endl;
cout << endl;
SHOW_HEX (ip_ipp_vp_array, "array[0]");
SHOW_HEX (ip_ipp_vp_array+1, "array[0] + sizeof(*ipp_vp_array)" );
SHOW_HEX (ip_ipp_vp_array+2, "array[0] + 2*sizeof(*ipp_vp_array)");
SHOW_HEX (ip_ipp_vp_array+3, "array[0] + 3*sizeof(*ipp_vp_array)");
SHOW_HEX (ip_ipp_vp_array+4, "array[0] + 4*sizeof(*ipp_vp_array)");
cout << endl;
COUT << "\"cout << (*ip_ipp_vp_array)\" causes Segmentation fault" << endl;
// -------------------------
// Causes Segmentation fault
// SHOW_DEC (*ip_ipp_vp_array);
// SHOW_DEC (*(ip_ipp_vp_array+1));
// SHOW_DEC (*(ip_ipp_vp_array+2));
// SHOW_DEC (*(ip_ipp_vp_array+3));
// SHOW_DEC (*(ip_ipp_vp_array+4));
// -------------------------
cout << endl;
cout << endl;
cout << "------ ipa_vp_array ------" << endl;
cout << endl;
SHOW_HEX (&ipa_vp_array[0], "&array[0]");
SHOW_HEX (&ipa_vp_array[1], "&array[0] + sizeof(*ipa_vp_array)");
SHOW_HEX (&ipa_vp_array[2], "&array[0] + 2*sizeof(*ipa_vp_array)");
cout << endl;
SHOW_HEX (ipa_vp_array[0], "&array[0]");
SHOW_HEX (ipa_vp_array[1], "&array[0] + sizeof(*ipa_vp_array)");
SHOW_HEX (ipa_vp_array[2], "&array[0] + 2*sizeof(*ipa_vp_array)");
cout << endl;
SHOW_HEX (ipa_vp_array, "&array[0]");
SHOW_HEX (ipa_vp_array+1, "&array[0] + sizeof(*ipa_vp_array)");
SHOW_HEX (ipa_vp_array+2, "&array[0] + 2*sizeof(*ipa_vp_array)");
cout << endl;
SHOW_HEX (&ipa_vp_array[0][0], "&array[0]");
SHOW_HEX (&ipa_vp_array[0][1], "&array[1]");
SHOW_HEX (&ipa_vp_array[0][2], "&array[2]");
SHOW_HEX (&ipa_vp_array[0][3], "&array[3]");
SHOW_HEX (&ipa_vp_array[0][4], "&array[4]");
SHOW_HEX (&ipa_vp_array[1][0], "");
SHOW_HEX (&ipa_vp_array[1][1], "");
SHOW_HEX (&ipa_vp_array[1][2], "");
cout << endl;
SHOW_DEC (ipa_vp_array[0][0], "array[0]");
SHOW_DEC (ipa_vp_array[0][1], "array[1]");
SHOW_DEC (ipa_vp_array[0][2], "array[2]");
SHOW_DEC (ipa_vp_array[0][3], "array[3]");
SHOW_DEC (ipa_vp_array[0][4], "array[4]");
cout << endl;
cout << endl;
cout << "------ ip_ipa_vp_array ------" << endl;
cout << endl;
SHOW_HEX (ip_ipa_vp_array, "&array[0]");
SHOW_HEX (ip_ipa_vp_array+1, "&array[1]");
SHOW_HEX (ip_ipa_vp_array+2, "&array[2]");
SHOW_HEX (ip_ipa_vp_array+3, "&array[3]");
SHOW_HEX (ip_ipa_vp_array+4, "&array[4]");
cout << endl;
SHOW_DEC (ip_ipa_vp_array[0], "array[0]");
SHOW_DEC (ip_ipa_vp_array[1], "array[1]");
SHOW_DEC (ip_ipa_vp_array[2], "array[2]");
SHOW_DEC (ip_ipa_vp_array[3], "array[3]");
SHOW_DEC (ip_ipa_vp_array[4], "array[4]");
return 0;
}
========= foo.cpp : END ===========
========= Compilation & Run : BEGIN =========
// g++ (GCC) 3.3.3 (cygwin special)
$ g++ -W -Wall foo.cpp
$ a
[Line- 47] sizeof(*ipp_vp_array) = 4
[Line- 52] sizeof(*ipa_vp_array) = 20
[Line- 56] &array[0] = 0x22f040
[Line- 57] &array[1] = 0x22f044
[Line- 58] &array[2] = 0x22f048
[Line- 59] &array[3] = 0x22f04c
[Line- 60] &array[4] = 0x22f050
[Line- 63] array = 0x22f040 &array[0]
[Line- 64] &array = 0x22f040 &array[0]
[Line- 65] *array = 1 array[0]
[Line- 67] ptr = 0x22f040 &array[0]
[Line- 68] &ptr = 0x22f03c *ptr == &array[0]
[Line- 69] *ptr = 1 array[0]
[Line- 72] vp_array = 0x22f040 &array[0]
[Line- 73] vp_ptr = 0x22f03c *vp_ptr == &array[0]
[Line- 74] ipp_vp_ptr = 0x22f03c *ipp_vp_ptr == &array[0]
[Line- 75] ip_ipp_vp_ptr = 0x22f040 &array[0]
------ ip_vp_array ------
[Line- 82] ip_vp_array = 0x22f040 &array[0]
[Line- 83] ip_vp_array+1 = 0x22f044 &array[1]
[Line- 84] ip_vp_array+2 = 0x22f048 &array[2]
[Line- 85] ip_vp_array+3 = 0x22f04c &array[3]
[Line- 86] ip_vp_array+4 = 0x22f050 &array[4]
[Line- 89] ip_vp_array[0] = 1 array[0]
[Line- 90] ip_vp_array[1] = 2 array[1]
[Line- 91] ip_vp_array[2] = 3 array[2]
[Line- 92] ip_vp_array[3] = 4 array[3]
[Line- 93] ip_vp_array[4] = 5 array[4]
------ ipp_vp_array ------
[Line-101] ipp_vp_array = 0x22f040 &array[0]
[Line-102] ipp_vp_array+1 = 0x22f044 &array[1]
[Line-103] ipp_vp_array+2 = 0x22f048 &array[2]
[Line-104] ipp_vp_array+3 = 0x22f04c &array[3]
[Line-105] ipp_vp_array+4 = 0x22f050 &array[4]
[Line-108] *ipp_vp_array = 1 array[0]
[Line-109] *(ipp_vp_array+1) = 2 array[1]
[Line-110] *(ipp_vp_array+2) = 3 array[2]
[Line-111] *(ipp_vp_array+3) = 4 array[3]
[Line-112] *(ipp_vp_array+4) = 5 array[4]
------ ip_ipp_vp_array ------
[Line-119] ip_ipp_vp_array = 0x1 array[0]
[Line-120] ip_ipp_vp_array+1 = 0x5 array[0] + sizeof(*ipp_vp_array)
[Line-121] ip_ipp_vp_array+2 = 0x9 array[0] + 2*sizeof(*ipp_vp_array)
[Line-122] ip_ipp_vp_array+3 = 0xd array[0] + 3*sizeof(*ipp_vp_array)
[Line-123] ip_ipp_vp_array+4 = 0x11 array[0] + 4*sizeof(*ipp_vp_array)
[Line-127] "cout << (*ip_ipp_vp_array)" causes Segmentation fault
------ ipa_vp_array ------
[Line-142] &ipa_vp_array[0] = 0x22f040 &array[0]
[Line-143] &ipa_vp_array[1] = 0x22f054 &array[0] + sizeof(*ipa_vp_array)
[Line-144] &ipa_vp_array[2] = 0x22f068 &array[0] + 2*sizeof(*ipa_vp_array)
[Line-147] ipa_vp_array[0] = 0x22f040 &array[0]
[Line-148] ipa_vp_array[1] = 0x22f054 &array[0] + sizeof(*ipa_vp_array)
[Line-149] ipa_vp_array[2] = 0x22f068 &array[0] + 2*sizeof(*ipa_vp_array)
[Line-152] ipa_vp_array = 0x22f040 &array[0]
[Line-153] ipa_vp_array+1 = 0x22f054 &array[0] + sizeof(*ipa_vp_array)
[Line-154] ipa_vp_array+2 = 0x22f068 &array[0] + 2*sizeof(*ipa_vp_array)
[Line-158] &ipa_vp_array[0][0] = 0x22f040 &array[0]
[Line-159] &ipa_vp_array[0][1] = 0x22f044 &array[1]
[Line-160] &ipa_vp_array[0][2] = 0x22f048 &array[2]
[Line-161] &ipa_vp_array[0][3] = 0x22f04c &array[3]
[Line-162] &ipa_vp_array[0][4] = 0x22f050 &array[4]
[Line-163] &ipa_vp_array[1][0] = 0x22f054
[Line-164] &ipa_vp_array[1][1] = 0x22f058
[Line-165] &ipa_vp_array[1][2] = 0x22f05c
[Line-168] ipa_vp_array[0][0] = 1 array[0]
[Line-169] ipa_vp_array[0][1] = 2 array[1]
[Line-170] ipa_vp_array[0][2] = 3 array[2]
[Line-171] ipa_vp_array[0][3] = 4 array[3]
[Line-172] ipa_vp_array[0][4] = 5 array[4]
------ ip_ipa_vp_array ------
[Line-179] ip_ipa_vp_array = 0x22f040 &array[0]
[Line-180] ip_ipa_vp_array+1 = 0x22f044 &array[1]
[Line-181] ip_ipa_vp_array+2 = 0x22f048 &array[2]
[Line-182] ip_ipa_vp_array+3 = 0x22f04c &array[3]
[Line-183] ip_ipa_vp_array+4 = 0x22f050 &array[4]
[Line-186] ip_ipa_vp_array[0] = 1 array[0]
[Line-187] ip_ipa_vp_array[1] = 2 array[1]
[Line-188] ip_ipa_vp_array[2] = 3 array[2]
[Line-189] ip_ipa_vp_array[3] = 4 array[3]
[Line-190] ip_ipa_vp_array[4] = 5 array[4]
========= Compilation & Run : END ===========