27

I have a problem where I have a pointer to an area in memory. I would like to use this pointer to create an integer array.

Essentially this is what I have, a pointer to a memory address of size 100*300*2 = 60000 bytes

unsigned char *ptr = 0x00000000; // fictional point in memory goes up to 0x0000EA60

What i would like to achieve is to examine this memory as an integer array of size 100*150 = 15000 ints = 60000 bytes, like this:

unsigned int array[ 100 ][ 150 ];

I'm assuming it involves some casting though i'm not sure exactly how to formulate it. Any help would be appreciated.

2
  • How do you initially acquire the the address in ptr? It could be dangerous to cast an arbitrary pointer to unsigned int * if the initial pointer was not aligned to sizeof int. If that happens, then at best your accesses to the memory will be slow due to reads across cache lines. At worst, it could cause a bus error and your program will halt (depending on your cpu architecture). Commented Jul 13, 2012 at 2:05
  • hi chris, just saw your comment. em the address is actually similar to the one in the example and i'm not aquiring it from anywhere - it is an area in memory which i know to be free - working in an embedded environment. i can see what you mean though as this did cause an error when i had it aligned to a character then tried to cast to int. the (temporary) solution i am using is to manually input the address again as above Commented Jul 18, 2012 at 20:29

2 Answers 2

22

You can cast the pointer to unsigned int (*)[150]. It can then be used as if it is a 2D array ("as if", since behavior of sizeof is different).

unsigned int (*array)[150] = (unsigned int (*)[150]) ptr;
Sign up to request clarification or add additional context in comments.

4 Comments

thanks yeah thats what i need. could you elaborate further on how to cast the pointer?
Just cast it normally i.e. unsigned int (*array)[150] = (unsigned int (*)[150]) ptr;
@nhahtdh : I tried this :(unsigned long)result.c[2] = 126and I got this compiler error lvalue required as left operand of assignment.resultis a structure and cis of type unsigned short *.
@user2284570: What are you trying to do? From what I see, it is irrelevant to the question here. But what you probably want is ((unsigned long*) result.c)[2] = 126
1

Starting with your ptr declaration

unsigned char *ptr = 0x00000000; // fictional point in memory goes up to 0x0000EA60

You can cast ptr to a pointer to whatever type you're treating the block as, in this case array of array of unsigned int. We'll declare a new pointer:

unsigned int (*array_2d)[100][150] = (unsigned int (*)[100][150])ptr;

Then, access elements by dereferencing and then indexing just as you would for a normal 2d array.

(*array_2d)[50][73] = 27;

Some typedefs would help clean things up, too.

typedef unsigned int my_2d_array_t[100][150];
typedef my_2d_array_t *my_2d_array_ptr_t;
my_2d_array_ptr_t array_2d = (my_2d_array_ptr_t)ptr;
(*array_2d)[26][3] = 357;
...

And sizeof should work properly.

sizeof(array_2d); //4, given 32-bit pointer
sizeof(*array_2d); //60000, given 32-bit ints
sizeof((*array_2d)[0]); //600, size of array of 150 ints
sizeof((*array_2d)[0][1]); //4, size of 1 int

Comments

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.