C Question

TecHNooB

Diamond Member
Sep 10, 2005
7,458
1
76
If I am using malloc in a function to allocate space to 2-d array (double ptr), how come main does not see this? How do I make this work T_T

o wow, wuts up with attach code?

#include <stdio.h>
#include <stdlib.h>

void alloc_maze(int**, int, int);

int main(void)
{
int row, col; int i, j;
int **maze;

alloc_maze(&maze, row, col);

return(0);
}

void alloc_maze(int **maze, int row, int col)
{
int i;

**maze = malloc(row * sizeof(int *));

for (i = 0; i < row; i++){
*maze = malloc(col * sizeof(int));
}
}
 

dinkumthinkum

Senior member
Jul 3, 2008
203
0
0
Your types are wrong: the first argument to alloc_maze() as given is an int***. Your row initialization is also incorrect.

Why not just have alloc_maze() return a pointer to the array? Also, are you sure you want to allocate an array of pointers to arrays? Why not just do one block of memory: row*col?
 

seemingly random

Diamond Member
Oct 10, 2007
5,277
0
0
Technically, maze isn't a 2d array. It's (a pointer to a pointer). alloc_maze requires a pointer to (a pointer to a pointer) so it can return this pointer. So it should be alloc_maze(int ***mazep, ).

If alloc_maze will never be complex, I'd make it return int** for ease of use. And, of course, a zero failure return would be easy to check.


//untested
//
int **alloc_maze(int row, int col)
{
int **p;

if ((p = (int**) malloc(row...)) != 0)
{

for (int **rowp = p; row > 0; row--, rowp++)
{
int *x;
if ((x = (int*) malloc(col...)) == 0)
{
//free prev mallocs and return 0
}
*rowp = x;
}

}

return p;
}
 

slugg

Diamond Member
Feb 17, 2002
4,723
80
91
Just a tip... avoid 2D arrays whenever possible. Use a 1D array and arithmetic to index it.

Edit: here is an example of what I'm talking about. Look at AsciiMaze.h and AsciiMaze.cpp. I know, this is technically in C++, but you can clearly see what I'm talking about. Look at the constructors for the AsciiMaze class and also look at operator().
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
Originally posted by: seemingly random
Technically, maze isn't a 2d array. It's (a pointer to a pointer). alloc_maze requires a pointer to (a pointer to a pointer) so it can return this pointer. So it should be alloc_maze(int ***mazep, ).

If alloc_maze will never be complex, I'd make it return int** for ease of use. And, of course, a zero failure return would be easy to check.


//untested
//
int **alloc_maze(int row, int col)
{
int **p;

if ((p = (int**) malloc(row...)) != 0)
{

for (int **rowp = p; row > 0; row--, rowp++)
{
int *x;
if ((x = (int*) malloc(col...)) == 0)
{
//free prev mallocs and return 0
}
*rowp = x;
}

}

return p;
}

and considering that any pointer (in 32 bit world) is 4 bytes long, it also means that just returning a pointer will be almost as fast as doing this via pointer (its a little slower still, but not by a whole lot). But yeah, this would probably be the solution I would go with.

A dynamically allocated 2d array is going to be very slow, you should avoid it at all costs. It is better to us a 1d array for dynamic allocations. (malloc is slow, so you should minimize the number of calls you make to it.)