Two dimension float array on an address

0

I am trying to create a two dimension float array on a memory address. This is what I have:

float ** adresse = (float**)(0xC0000001);
uint8_t dim1Size = 16;
uint16_t dim2Size = 11;
for(int i = 0; i < dim1Size; i++)
{
    adresse[i] = (float*)(adresse+dim1Size*sizeof(float*) + dim2Size*sizeof(float));
}

I'm "fly out" at this line :

adresse[i] = (float*)(adresse+dim1Size*sizeof(float*) + dim2Size*sizeof(float));

So I am doing something wrong. Can you say me what is wrong and why?

c
arrays
pointers
asked on Stack Overflow Oct 18, 2016 by TheRealLife • edited Oct 18, 2016 by Bence Kaulics

1 Answer

2

The code makes a lot of assumptions.

The conversion of an integer to a pointer is implementation defined:

(float**)(0xC0000001);

The resulting pointer must be correctly aligned for the referenced type. The address ending with 1 is probably not correctly aligned for type float*.

Once you fix that, you need to have two allocations, one for the array of type pointer to float, and the other for the two dimensional array of type float.

float ** adresse = //aligned and valid memory of size sizeof( float* ) * dim1Size
float* objects = //aligned and valid memory of size sizeof( float ) * dim1Size * dim2Size

Then you iterate through the pointer array:

for( size_t i = 0; i < dim1Size; i++)
{
     adresse[i] = objects + dim1Size;
}
answered on Stack Overflow Oct 18, 2016 by 2501 • edited Oct 18, 2016 by 2501

User contributions licensed under CC BY-SA 3.0