Is there any way to malloc a large array, but refer to it with 2D syntax? I want something like:
int *memory = (int *)malloc(sizeof(int)*400*200);
int MAGICVAR = ...;
MAGICVAR[20][10] = 3; //sets the (200*20 + 10)th element
UPDATE: This was important to mention: I just want to have one contiguous block of memory. I just don't want to write a macro like:
#define INDX(a,b) (a*200+b);
and then refer to my blob like:
memory[INDX(a,b)];
I'd much prefer:
memory[a][b];
UPDATE: I understand the compiler has no way of knowing as-is. I'd be willing to supply extra information, something like:
int *MAGICVAR[][200] = memory;
Does no syntax like this exist? Note the reason I don't just use a fixed width array is that it is too big to place on the stack.
UPDATE: OK guys, I can do this:
void toldyou(char MAGICVAR[][286][5]) {
//use MAGICVAR
}
//from another function:
char *memory = (char *)malloc(sizeof(char)*1820*286*5);
fool(memory);
I get a warning, passing arg 1 of toldyou from incompatible pointer type
, but the code works, and I've verified that the same locations are accessed. Is there any way to do this without using another function?
If extra indirection isn't a concern, you can use an array of pointers.
Edit
Here's a variation on @Platinum Azure's answer that doesn't make so many calls to malloc. Besides faster allocation, all the elements are guaranteed to be contiguous:
Use a pointer to arrays:
Edit:
Arrays and pointers look very much the same, but the compiler treats them very differently. Let's see what needs to be done for an array indexing and de-referencing a pointer with offset:
Say we declared a static array (array on the stack is just a bit more complicated, fixed offset from a register, but essentially the same):
static int array[10];
And a pointer:
static int* pointer;
We then de-deference each as follows:
x = array[i];
x = pointer[i];
The thing to note is that address of the beginning of
array
, as well as, address ofpointer
(not its contents) are fixed at link/load time. Compiler then does the following:array
de-reference:i
,array
, i.e. its fixed address, to form target memory address,pointer
de-reference:i
,pointer
, i.e. the contents at its address,Same happens for 2D array with additional steps of loading the second index and multiplying it by the row size (which is a constant). All this is decided at compile time, and there's no way of substituting one for the other at run-time.
Edit:
@caf here has the right solution. There's a legal way within the language to index a pointer as two-dimentional array after all.
The compiler and runtime have no way of knowing your intended dimension capacities with only a multiplication in the malloc call.
You need to use a double pointer in order to achieve the capability of two indices. Something like this should do it:
Make sure you check all your malloc return values for NULL returns, indicating memory allocation failure.
Yes, you can do this, and no, you don't need another array of pointers like most of the other answers are telling you. The invocation you want is just:
If you wish to declare a function returning such a pointer, you can either do it like this:
Or use a typedef, which makes it a bit clearer: