3

How can I allocate a bi-dimensional array using malloc? This is my current code:

typedef struct object product, *pprod;
struct object{
 int type;
 int quantity;
 pprod next;
};
pprod t[4][3];

Thanks a lot for your help.

Eric Postpischil
232k15 gold badges197 silver badges377 bronze badges
asked May 31, 2013 at 17:30
4
  • What is the aim? To malloc some memory to use it as a pprod[n][3]? Commented May 31, 2013 at 17:34
  • 1
    Actually I need it to be pprod[n][m] Commented May 31, 2013 at 17:37
  • You can create a vector (singlePinter) with the size of n*m and access with [index/rowSize][index%rowSize] so you don`t have to create and array of pointers. Commented May 31, 2013 at 18:07
  • @demonofnight: C supports variable-length arrays, since 1999, so you do not need to do manual address arithmetic. Commented May 31, 2013 at 18:14

2 Answers 2

3

To allocate memory in such a way that the layout is compatible with a normal two-dimensional array - or array of arrays - you need a pointer to an array of appropriate size,

pprod (*t)[m] = malloc(n * sizeof *t);

Thus t is a pointer to arrays of m elements of type pprod, and you can simply use it

t[i][j]

as if it were declared

pprod t[n][m];

(if malloc doesn't return NULL).

This allocation allocates a contiguous block of memory, unlike allocating a pprod ** would.

(Note: If m is not a compile-time constant, that requires that the compiler supports variable length arrays, it would not work with MSVC.)

answered May 31, 2013 at 17:38
Sign up to request clarification or add additional context in comments.

5 Comments

That's the message i'm getting in Xcode "Initializer element is not a compile-time constant"
That variable length arrays are not supported? Hmm, I thought Xcode used llvm-gcc, and that should support VLAs. What's the compiler version you are using? Or are you perhaps compiling as C++? C++ doesn't have VLAs.
@CarlaPateiro "Initializer element is not a compile-time constant" looks like you try to initialise an array (or a struct) of static storage duration with something that is not a constant expression. Can you post the exact error message with the corresponding line of the source code?
The compiler version is: Apple LLVM compiler 4.2 This is what im trying to do: pprod (*t)[3] = malloc(4 * sizeof *t);
@CarlaPateiro At file scope? You can't call malloc at file scope, you must do that in a function.
1

For 2D array you should define a pointer like:

typedef struct obj OBJECT;
OBJECT **2Dptr = malloc (sizeof(OBJECT*)*rows)
for(i=0;i<rows;i++)
 2Dptr[i]=malloc(sizeof(OBJECT)*total_objects) //columns

There are other ways too, you can define array of pointers to your struct object.

if you want object[5][10]
you can create 5 pointers to array of 10 objects;

if you want the memory to be contiguous then you could do

*2Dptr=malloc(sizeof(OBJECT) * rows * cols); //allocate contiguosly
**access_ptr = malloc(sizeof(OBJECT*) * rows);
for(i=0;i<row;i++)
 access_ptr[i]= 2Dptr+(i*cols);
answered May 31, 2013 at 17:36

7 Comments

downvote is great if downvoter lets me know what am i missing or wrong! so that everyone can learn!
This does not create a two-dimensional array. It creates a one-dimensional array of pointers to one-dimensional arrays. The resulting memory is generally not contiguous and cannot be passed to routines expecting two-dimensional arrays. It also causes unnecessary pointer lookups and prevents the compiler from making certain optimizations that are possible with two-dimensional arrays.
@EricPostpischil: even if its not contiguous it can be passed to functions right?
It will not serve as a two-dimensional array when passed to a function expecting a two-dimensional array. It could be passed to a function expecting a pointer to an array of pointers to arrays of objects. They are not the same thing, and needless pointers-to-pointers should be avoided.
No, an expression of type int foo[m][n] (array of array of int) is converted to int (*)[n] (pointer to array of int). Only the first dimension is converted. int (*)[n] and int ** are not compatible.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.