0

I am trying to make two dimensional array function, but somehow it is not working. The code here:

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

int **multiTable (unsigned int xs, unsigned in ys)

{
unsigned int i, j;

int **table = int(**)malloc(xs * ys * sizeof(int*));
for(i = 0; i < ys; i++)
{
    for(j = 0; j < xs; j++)
    {
       table[i][j] = j * i;
    }


}
free(**table);
return table;


}

So first of all, should I also add inside the malloc the row (xs)? Or should it work, if I work only with the columns? --> like this:

int **table = int(**)malloc(ys * sizeof(int*));
2
  • you cannot do this in one shot. first allocate memory to table, then table[i](s). Commented Dec 2, 2014 at 12:31
  • what's the point of returning something that was just freed? Commented Dec 2, 2014 at 12:34

3 Answers 3

1

That is not going to work as an array of pointers int **table is not contiguous and it is not equivalent to a 2d array table[a][b].

You can however use a pointer to an array if you want to use a single malloc.

int (*table)[xs] = malloc( ys * sizeof(*table));
for( int i = 0; i < ys; i++)
{
    for( int j = 0; j < xs; j++)
    {
       table[i][j] = i;
    }
}
free( table ) ;

And do not return table after you free it as your return call does.

Sign up to request clarification or add additional context in comments.

5 Comments

Note, on some compilers, xs would have to be a define or a constant.
@rcgldr Not true, compilers have nothing to do with that. The Standard does. C99 and onward has variable-length arrays( and pointers to them ).
I had the impression even with C99, only the first (or only) dimension of a multi (or single) dimensional array could be variable. The rest of the dimensions need to be fixed in size. For example twodarrray[var][const1] or threedarray[var][const1][const2] .
@rcgldr There is not such limit, you can try it for yourself.
1

It seems you mean the following

int ** multiTable( unsigned int xs, unsigned int ys )
{
    unsigned int i, j;

    int **table = malloc( ys * sizeof( int * ) );

    for ( i = 0; i < ys; i++ )
    {
        table[i] = malloc( xs * sizeof( int ) );
        for ( j = 0; j < xs; j++ )
        {
           table[i][j] = j * i;
        }
    }

    return table;
}

Comments

0

'table' is a 2-D pointer. First understand what is a 2D pointer. A pointer is a special type of variable that is used to store the address of another variable. So a 2D pointer is a special variable that is again used to store the address of a pointer variable.

Now imagine that you have a single array of pointers(collection of base address of different 1-D array), that will store the base address of many 1-D arrays.

To dynamically allocate memory to this array of pointers you need the statement

table=(int**)malloc(sizeof(int*)*xs);   

Now you have an array with 'xs' number of elements, and you can access each element by table[0], table[1], table[2]..and so on..., but none of these arrays is allocated memory. So you need to allocate memory to each of the array individually using a loop like this:

for(i=0;i<xs;i++)
    table[i]=(int*)malloc(sizeof(int)*ys);

So your over-all program becomes:

int **table; // table is a 2D pointer

table=(int**)malloc(sizeof(int*)*xs);
for(i=0;i<xs;i++)
    table[i]=(int*)malloc(sizeof(int)*ys);

for(i = 0; i < ys; i++)
{
    for(j = 0; j < xs; j++)
    {
       table[i][j] = j * i;
    }
}
return table;

You don't need to free the array before returning it. Doing so will make your pointer 'table' a dangling pointer that is still referring to a memory that is no longer allocated, so just return 'table' without the statement:

free(table);

The above statement will lead to a dangling memory that has no access point, ans so is useless. This is a memory leakage problem that arises when memory gets accumulated, and this memory is no more accessible through your program and could not be relocated for other purpose, such a memory is called dangling memory.

1 Comment

Please do not cast the result of malloc. As of C89 it's no longer necessary, and under C89 it can suppress a useful diagnostic if you don't have a declaration for malloc in scope.

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.