alireza_fn
alireza_fn

Reputation: 944

access violation error when reading from 4-dimentional array

I'm storing some arrays like this:

uint8_t a[2][4][4] = {
{
    { 1, 1, 1, 1 },
    { 0, 0, 0, 0 },
    { 0, 0, 0, 0 },
    { 0, 0, 0, 0 },
},
{
    { 1, 1, 1, 0 },
    { 1, 0, 0, 0 },
    { 0, 0, 0, 0 },
    { 0, 0, 0, 0 },
},
};

and, then I store an array of this arrays:

uint8_t ***data[5] = { 0, 0, (uint8_t ***)a, (uint8_t ***)b, (uint8_t ***)c};

so when I try to cout<<data[2][0][0][1]; it should print 1 but an read access violation exception occurs. why this doesn't work?

Upvotes: 0

Views: 51

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

(uint8_t ***)a has the compiler interpret what is pointed by (pointer converted from) a as uint8_t**, but what there actually is data in the array, say, 0x01010101 if pointers are 4-byte long. There are little chance to the number to be a valid address, so dereferencing the "pointer" will lead to Segmentation Fault.

Use correct type.

uint8_t (*data[5])[4][4] = { 0, 0, a, b, c};

Also the statement to print should be

cout<<(int)data[2][0][0][1];

otherwise, the number may be interpreted as character and something not readable may be printed.

Upvotes: 2

Related Questions