2

I would like to call a function with some arguments and within this function allocate a 2D array and then pass the pointer to it back to main. this is not the actual code I'm using but a simplified "toy" code to explain what I'm trying. I am getting a segfault and I don't quite understand why. I've done stuff like this before and I thought this would work. any idea what I'm screwing up?

void func(int val, int ** array);
int main()
{
    int **array;
    int i = 2;

    func(i,array);

    // do some stuff with info from array[i][2]

    free(array);
}

void func(int val, int **array)
{

    // do some calculation here to find the row size for array

    array = malloc(row_size*sizeof(int *));
    for (i = 0; i < row_size; i++)
        array[i] = malloc(2* sizeof(int));

// fill the 2d array with some values


}
1
  • Just a side note: Depending on your needs it is easier to allocate one contiguous area of memory, malloc(sizeof(int) * rows * columns) Commented Jun 6, 2015 at 9:44

3 Answers 3

3

You need to pass a pointer to your int **array; pointer. func will therefore take an int ***arrayp argument. Modify the code this way:

void func(int rows, int ***arrayp);
int main()
{
    int **array;
    int rows = 2;

    func(rows, &array);

    // do some stuff with info from array[rows][2]

    free(array);
}

void func(int rows, int ***arrayp)
{
    // do some calculation here to find the row size for array
    *arrayp = malloc(rows * sizeof(int *));
    for (int i = 0; i < rows; i++)
        (*arrayp)[i] = malloc(2 * sizeof(int));  

    // fill the 2d array with some values
}

An alternative is to change func prototype to make return the int **array it allocates.

It seems silly to allocate subarrays of only 2 ints. Do you intend to make these subarrays dynamic? You would then need to keep track of their allocated sizes.

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

1 Comment

In func, when assigning arrayp, you need to de reference it once.
2

What you can do is

In main()

int **array = func(i);

The func() should be

int **func(int row_size)
{
    int  **array = malloc(row_size * sizeof(int *));
    for (int i = 0; i < row_size; i++)
        array[i] = malloc(2 * sizeof(int));  

    // fill the 2d array with some values
    return array;
}

Allocate memory inside the func() and return the array. Else you need to pass the refernce of the variable which needs to be modified within the function, what you pass is the value which is uninitilaized and the changes done inside the func() is not visible in main().

You can avoid passing the reference by doing what I have shown.

Comments

0

You shouldn't use a special function for a real 2D array. Since 1999 it is possible to have them with variable bounds.

What you have here is not a 2D array but an array of pointers. These are two very different things. Unless you have a really specific reason to use pointer to array, don't do it. It is error prone and less efficient.

A for a real 2D array this simply looks like

int (*array)[n] = malloc(sizeof(int[n][n]));

// use the array as you like

free(array);

Comments

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.