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?

2
  • Please clarify if the data is a 2D array or a pointer-based look-up table. You speak of 2D arrays yet there is no 2D array in your code. Commented Oct 18, 2016 at 9:47
  • You cannot create a real two dimension array but specifying statically (at compilation time) the bounding dimensions of the array. You are using pointers, and pointers are not the same thing as arrays. Commented Oct 19, 2016 at 6:07

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

The main issue here is that there will most likely not be an array of pointers located at that address, but rather a 2D array of floats. In which case this answer is not helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.