Watcom multi dimension array

I am running QNX 4.25 with the Watcom 10.6 compiler. I am using the 32
bit flat memory compiler option. This is what I am trying to do:


#include <stdio.h>
#include <stdlib.h>

typedef unsigned int ArrayType[8][8][8][32][512];

void main (void)
{

ArrayType *Array;

Array = (ArrayType *)malloc(sizeof(ArrayType));

}


When I access Array[0][0][0][0][1] it’s memory address is 0xC9C8;
When I access Array[0][0][0][0][2] it’s memory address is 0xCDC8;


I expected the next element to be at memory location 0xC9CA. (sizeof
(unsigned int) == 2).


Can anyone explain how the QNX/Watcom Compiler processes multi
dimensional arrays within memory?


Thanks for the help.


Sent via Deja.com http://www.deja.com/
Before you buy.

“Barcio” <barcio@my-deja.com> wrote in message
news:8lkrq5$5bh$1@nnrp1.deja.com

I am running QNX 4.25 with the Watcom 10.6 compiler. I am using the 32
bit flat memory compiler option. This is what I am trying to do:


#include <stdio.h
#include <stdlib.h

typedef unsigned int ArrayType[8][8][8][32][512];

void main (void)
{

ArrayType *Array;

Array = (ArrayType *)malloc(sizeof(ArrayType));

}

You got bitten by a simple problem :wink: The type ArrayType is really a type
just like an int, so if you do

&Array[1] it’s actuall 32Megs after Array[0] since the ArrayType is 32Megs.

This is the behavior I would expect.

So your problem is that you are missing a [] or a *:

Array[0] is the first array. Array[1] is the second.
Array[0][0] is the first “dimension” of the first array;
Array[1][0] is the first “dimention” of the second array.

Array[0][0][0][0][0][1] or *Array[0][0][0][0][1] is the int you meant
to access in you example.


When I access Array[0][0][0][0][1] it’s memory address is 0xC9C8;
When I access Array[0][0][0][0][2] it’s memory address is 0xCDC8;

The diff between the two addresses is 1024, that matches the sizes of the
dimension,
(512-sizeof(int) ). So that proves you are missing a [] to access
the int per say.

Think about it:

int *a;

a = malloc ()

// here you either access it using
a[0] = 0; ( the [] is the one you are missing) you did the “equivalent”
of a = 0;
// or
*a = 0;

The [] or * is there to indicated to what element you are refering to. The
same is
true if your element is a multidimentional array or not !

I expected the next element to be at memory location 0xC9CA. (sizeof
(unsigned int) == 2).


Can anyone explain how the QNX/Watcom Compiler processes multi
dimensional arrays within memory?

Are you impliying that it behaves differently under other compiler???

I tried in another compiler to malloc and also just define it (a
simplified 3D version)


#include <stdio.h>
#include <stdlib.h>

typedef unsigned int ArrayType[8][8][8];

void main (void)
{

ArrayType *Array;
ArrayType NewArray;

Array = (ArrayType *)malloc(sizeof(ArrayType));

*Array[0][0][0] = 3;
*Array[0][0][1] = 3;

NewArray[0][0][0] = 4;
NewArray[0][0][1] = 4;

printf(“Address Array %x %x\n”,
Array[0][0][0], Array[0][0][1]);

printf(“Address of NewArray %x %x\n”,
&(NewArray[0][0][0]),
&(NewArray[0][0][1]));

printf(“Size of Array %x\n”, sizeof(*Array));

printf(“Size of NewArray %x\n”, sizeof(NewArray));
}

This is what I receive

Address Array 674260 674280
Address of NewArray 65f5f4 65f5f8
Size of Array 800
Size of NewArray 800

I expected that the Array address would be the same as New Array.


Problem being when I try to fill the array I get a memory access error
on the array that I used malloc for:

for (x=0; x<8; x++)
{
for (y=0; y<8; y++)
{
for (z=0; z<8; z++)
{
*Array[x][y][z] = z; // I also tried *(Array[x][y][z])
}
}
}

Am I accessing the malloced array wrong?

Are you impliying that it behaves differently under other compiler???


Sorry to make you think that no it doesn’t.

Sent via Deja.com http://www.deja.com/
Before you buy.

“Barcio” <barcio@my-deja.com> wrote in message
news:8lmpok$i0l$1@nnrp1.deja.com

I tried in another compiler to malloc and also just define it (a
simplified 3D version)


#include <stdio.h
#include <stdlib.h

typedef unsigned int ArrayType[8][8][8];

void main (void)
{

ArrayType *Array;
ArrayType NewArray;

Array = (ArrayType *)malloc(sizeof(ArrayType));

*Array[0][0][0] = 3;
*Array[0][0][1] = 3;

NewArray[0][0][0] = 4;
NewArray[0][0][1] = 4;

printf(“Address Array %x %x\n”,
Array[0][0][0], Array[0][0][1]);

printf(“Address of NewArray %x %x\n”,
&(NewArray[0][0][0]),
&(NewArray[0][0][1]));

printf(“Size of Array %x\n”, sizeof(*Array));

printf(“Size of NewArray %x\n”, sizeof(NewArray));
}

This is what I receive

Address Array 674260 674280
Address of NewArray 65f5f4 65f5f8
Size of Array 800
Size of NewArray 800

Make sense!

I expected that the Array address would be the same as New Array.

One is allocated on the heap (malloc) and one is on the stack They are two
different objects, why would that have the same address?

Problem being when I try to fill the array I get a memory access error
on the array that I used malloc for:

for (x=0; x<8; x++)
{
for (y=0; y<8; y++)
{
for (z=0; z<8; z++)
{
*Array[x][y][z] = z; // I also tried *(Array[x][y][z])

Try (*Array)[x][y][z] = z

}
}
}