elo2cx
elo2cx

Reputation: 33

I can't access an array that was dynamically allocated inside the function

I want to allocate memory for a two-dimensional array of char in a separate function. I defined a double pointer in the main function, then I passed this pointer into my separate function alloc_arr(), where the memory was allocated using malloc(). The problem is that after exiting the function alloc_arr(), I cannot access my array because I get a "segmentation fault" runtime error

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int alloc_arr(char **res)
{
    size_t i;

    res = (char **)malloc(200 * sizeof(char *));
    if (res == NULL)
        return 0;
    i = 0;
    while (i < 200)
    {
        res[i] = (char *)malloc(100 * sizeof(char));
        if (res[i] == NULL)
            return 0;
        i++;
    }
    strncpy(&res[0][0], "string", 100);
    printf("res[0] = %s\n", res[0]);
    return 1;
}
void free_arr(char **res)
{
    int i = 0;
    for (; i < 200; i++)
        free(res[i]);
    free(res);
}
int main(void)
{
    char **res;
    res = NULL;
    if (alloc_arr(res))
        printf("res[0] = %s\n", res[0]); /* here I get the error */
    else
        return 1;
    free(res);
    return 0;
}

In the function alloc_arr(), I return a numeric value to indicate the possible error. I assume I can use the 'res' pointer after the end of the function because it was defined in the main. The malloc() should return the address of dynamic memory that I can use. At the same time, if I just return the pointer 'res' from the function alloc_arr(), this example works well.

I want to dynamically allocate memory for a two-dimensional array of char type (an array of strings), put one string into this array and print the value of the first element of the array. I have calculated to split my code into several parts by moving the code responsible for dynamic memory allocation into a separate function. However after performing the above operation I cannot access the first array element because I get a "segmentation fault" error. When I try to do the same with two-dimensional int array I get the same problem. Yes, I can do this:

int **alloc_int(int **res)
{
    size_t i;

    res = (int **)malloc(20 * sizeof(int *));
    if (res == NULL)
        return NULL;
    i = 0;
    while (i < 20)
    {
        res[i] = (int *)malloc(10 * sizeof(int));
        if (res[i] == NULL)
            return NULL;
        i++;
    }
    res[0][0] = 10;
    printf("res[0] = %d\n", res[0][0]);
    return res;
}
void free_int(int **res)
{
    int i = 0;
    for (; i < 20; i++)
        free(res[i]);
    free(res);
}
int main(void)
{
    int **res;
    res = NULL;
    if ((res = alloc_int(res)) != NULL)
        printf("res[0] = %d\n", res[0][0]);
    else
        return 1;
    free(res);
    return 0;
}

And it works. Although I just want to know the reason why the first variant doesn't work. And how to force it to work.`

Upvotes: 0

Views: 21

Answers (0)

Related Questions