dynamically allocating a 2d array

J

junky_fellow

What is the correct way of dynamically allocating a 2d array ?

I am doing it the following way. Is this correct ?

#include <stdlib.h>
int main(void)
{
int (*arr)(3);
arr = malloc(sizeof(*arr) * 4);
/* I want to dynamically allocate
int arr[4][3] */

arr[2][3] = 100; /* Can I initialize the 3rd column of
2nd row in this manner ? */
}

Thanx in advance for any help ...
 
U

Ulrich Eckhardt

What is the correct way of dynamically allocating a 2d array ?

There are a few correct ways.
I am doing it the following way. Is this correct ?

#include <stdlib.h>
int main(void)
{
int (*arr)(3);

This is wrong, it should be 'int (*arr)[3]' (pointer to array of three
ints). Even better, use a typedef: "typedef int row[3];".
arr = malloc(sizeof(*arr) * 4);
/* I want to dynamically allocate
int arr[4][3] */

arr[2][3] = 100; /* Can I initialize the 3rd column of
2nd row in this manner ? */

This code is correct.

An alternative way which works even it the presence of variable dimensions
is to allocate an array of size X*Y and then compute the index in the 1D
array from the position in the 2D array and its size.

Uli
 
I

Irrwahn Grausewitz

What is the correct way of dynamically allocating a 2d array ?

This is a FAQ, see section 6.16 in
http://www.faqs.org/faqs/C-faq/faq/

I suggest you read the whole thing, once you got it.
I am doing it the following way. Is this correct ?

At least you could've tried to compile it before you posted.
#include <stdlib.h>
int main(void)
{
int (*arr)(3);
syntax error
<snip>

Rhetorical question: What would you do with a pointer to a function
taking constant 3 returning int, anyway?

Best regards.
 
J

junky_fellow

Irrwahn said:
This is a FAQ, see section 6.16 in
http://www.faqs.org/faqs/C-faq/faq/

I suggest you read the whole thing, once you got it.


At least you could've tried to compile it before you posted.

syntax error
<snip>

Rhetorical question: What would you do with a pointer to a function
taking constant 3 returning int, anyway?
Sorry, it was a typo. I meant int (*arr)[3].
 
N

Nerox

Stick to the following rule:
-Generally, an array name in an expression is evaluated as a pointer to
its first element.

Thus,
int array[Y][X] is a 2d array, an array of Y arrays of X integers.
You can write its dynamic counterpart as follows:
int (*array)[X] which is a pointer to an array of X integers.
And then allocate it dynamically via malloc:
array = malloc(sizeof(*array) * Y );
 
S

sahu

i have a soln. just try it out it might work:
for dynamically allocating a 2d array of let it be something like
a[5][8].
int **t;
t=(int **)malloc(5*2);
for(i=0;i<5;i++)
{
*(t+i)=(int *)malloc(8*2);
}
 
E

Emmanuel Delahaye

sahu wrote on 11/09/05 :
i have a soln. just try it out it might work:
for dynamically allocating a 2d array of let it be something like
a[5][8].
int **t;
t=(int **)malloc(5*2);

Why 5 * 2 ? What is the cast made for ?

int **t; = malloc (5 * sizeof *t);

if (t != NULL)
{
for(i=0;i<5;i++)

Better to define some abstraction for this hard coded '5' ...
{
*(t+i)=(int *)malloc(8*2);

why 8 * 2 ? Be simple:

t = malloc (8 * sizeof *t);

The generic expression to allocate a variable (n = 1) or an array of n
variables of type T is:

T *p = malloc (n * sizeof *p);

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
 
K

Keith Thompson

Nerox said:
Stick to the following rule:
-Generally, an array name in an expression is evaluated as a pointer to
its first element.

Unless it's the argument of a sizeof or unary "&" operator.
 
O

Old Wolf

Ulrich said:
What is the correct way of dynamically allocating a 2d array ?

There are a few correct ways.
I am doing it the following way. Is this correct ?

#include <stdlib.h>
int main(void)
{
int (*arr)(3);

This is wrong, it should be 'int (*arr)[3]' (pointer to array of
three ints). Even better, use a typedef: "typedef int row[3];".
arr = malloc(sizeof(*arr) * 4);

arr[2][3] = 100; /* Can I initialize the 3rd column of
2nd row in this manner ? */

This code is correct.

arr[2][3] is the 4th column of the 3rd row. Arrays index
from 0 in C. Depending on how you read the Standard,
accessing arr[2][3] either causes undefined behaviour, or
ends up accessing arr[3][0].
 
J

John Bode

What is the correct way of dynamically allocating a 2d array ?

I am doing it the following way. Is this correct ?

#include <stdlib.h>
int main(void)
{
int (*arr)(3);

I'm assuming you meant

int (*arr)[3];
arr = malloc(sizeof(*arr) * 4);
/* I want to dynamically allocate
int arr[4][3] */

I'm assuming you meant

int arr[3][4]
arr[2][3] = 100; /* Can I initialize the 3rd column of
2nd row in this manner ? */
}

Thanx in advance for any help ...

Well, it's different (never thought to do it that way before). It
appears to work, though. However, this method requires one dimension
be fixed. If you want to allocate a 2D array of int and be able to
specify both dimensions dynamically, here's one method:

#include <stdlib.h>

int **new2DIntArray(size_t rows, size_t cols)
{
int **arr;
int mallocError = 0;
size_t lastRow = 0;

arr = malloc(sizeof arr[0] * rows);
if (arr)
{
size_t i;
for (i = 0; i < rows && !mallocError; i++)
{
arr = malloc(sizeof arr[0] * cols);
if (arr)
{
size_t j;
lastRow = i;
for (j = 0; j < cols; j++)
{
arr[j] = 0;
}
}
else
{
mallocError = 1;
break;
}
}

if (mallocError)
{
size_t i;
for (i = lastRow; i >= 0; i--)
{
free(arr);
}
free(arr);
arr = NULL;
}
}

return arr;
}

int main(void)
{
int **arr1 = new2DIntArray(4,3); /* int arr[4][3] */
int **arr2 = new2DIntArray(3,4); /* int arr[3][4] */
/* etc. */

/* do something with arrays */

return 0;
}

You'll want to add another function to free up the arrays when you're
done with them, but that should be straightforward (basically it's the
if(mallocError) branch above).
 
B

Barry Schwarz

What is the correct way of dynamically allocating a 2d array ?

I am doing it the following way. Is this correct ?

#include <stdlib.h>
int main(void)
{
int (*arr)(3);

I'm assuming you meant

int (*arr)[3];
arr = malloc(sizeof(*arr) * 4);
/* I want to dynamically allocate
int arr[4][3] */

I'm assuming you meant

int arr[3][4]

I hope not. He has allocated space for an array of 4 arrays of 3 int
each.
arr[2][3] = 100; /* Can I initialize the 3rd column of
2nd row in this manner ? */

The third column of the second row is arr[1][2].
}

Thanx in advance for any help ...

Well, it's different (never thought to do it that way before). It
appears to work, though. However, this method requires one dimension
be fixed. If you want to allocate a 2D array of int and be able to
specify both dimensions dynamically, here's one method:

#include <stdlib.h>

int **new2DIntArray(size_t rows, size_t cols)
{
int **arr;
int mallocError = 0;
size_t lastRow = 0;

arr = malloc(sizeof arr[0] * rows);
if (arr)
{
size_t i;
for (i = 0; i < rows && !mallocError; i++)
{
arr = malloc(sizeof arr[0] * cols);
if (arr)
{
size_t j;
lastRow = i;
for (j = 0; j < cols; j++)
{
arr[j] = 0;
}
}
else
{
mallocError = 1;
break;
}
}

if (mallocError)
{
size_t i;
for (i = lastRow; i >= 0; i--)


This for loop can never end. size_t is an unsigned type so i will
always be >= 0.
{
free(arr);
}
free(arr);
arr = NULL;
}
}

return arr;
}

int main(void)
{
int **arr1 = new2DIntArray(4,3); /* int arr[4][3] */
int **arr2 = new2DIntArray(3,4); /* int arr[3][4] */
/* etc. */

/* do something with arrays */

return 0;
}

You'll want to add another function to free up the arrays when you're
done with them, but that should be straightforward (basically it's the
if(mallocError) branch above).



<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top