user1382094
user1382094

Reputation: 41

Use an array of struct dynamically allocated in C

I'm writing an application using the C language. I created an new type based on a structure:

typedef struct ENTITY
{
    char * field1;
    char * field2;
} entity;

Then, I defined a function to dynamically allocate an array of entities:

int my_function(entity ** my_array)
{
    count = random_int(1, 10);

    entity * result;
    result = (entity *) calloc(count, sizeof(entity));

    int i;
    for(i = 0 ; i < count ; i++)
    {
        (result+i)->field1 = strdup("Blabla in field1");
        (result+i)->field2 = strdup("Blabla in flied2");

        // This line print correctly "Blabla in field1" for each element in the array.
        printf("->{%s}\n", (result+i)->field1);
    }

    *my_array = result;
    return count;
}

In my main file, I use this function:

entity * my_array;
count = my_function(&my_array);

for(i = 0 ; i < count ; i++)
{
    printf("field1 of the element %d: %s\n", i, my_array[i].field1);
}

For some reason, this code works when my array is fulfilled by <= 3 elements, from 4 elements in the array I get a segmentation fault error:

->{Blabla in field1}
->{Blabla in field1}
->{Blabla in field1}
->{Blabla in field1}
field1 of the element 0: `�fZ$
Segmentation fault

I've read a lot about dynamic allocations on here but I can't manage to fix this problem. Any clue?

Thanks for you help!

Upvotes: 1

Views: 749

Answers (1)

Chimera
Chimera

Reputation: 6038

You code works after removing the call to random_int() and replacing it with a hard coded amount as shown below:

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


typedef struct ENTITY
{
    char * field1;
    char * field2;
} entity;


int my_function(entity ** my_array)
{
    int count = 10;

    entity * result;
    result = (entity *) calloc(count, sizeof(entity));

    int i;
    for(i = 0 ; i < count ; i++)
    {
        (result+i)->field1 = strdup("Blabla in field1");
        (result+i)->field2 = strdup("Blabla in flied2");
    }

    for(i = 0 ; i < count ; i++)
    {
        // This line print correctly "Blabla in field1" for each element in the array.
        printf("->{%s}\n", (result+i)->field1);
    }    


    *my_array = result;
    return count;
}



int main( int argc, char *argv[] )
{
    int count;
    int i;

    entity * my_array;
    count = my_function(&my_array);

    for(i = 0 ; i < count ; i++)
    {
        printf("MAIN::field1 of the element %d: %s\n", i, my_array[i].field1);
        printf("MAIN::field2 of the element %d: %s\n", i, my_array[i].field2);
   }

    return( 0 );
}

The output of this is:

[root@jrn SO]# gcc  array.c
[root@jrn SO]# ./a.out 
->{Blabla in field1}
->{Blabla in field1}
->{Blabla in field1}
->{Blabla in field1}
->{Blabla in field1}
->{Blabla in field1}
->{Blabla in field1}
->{Blabla in field1}
->{Blabla in field1}
->{Blabla in field1}
MAIN::field1 of the element 0: Blabla in field1
MAIN::field2 of the element 0: Blabla in flied2
MAIN::field1 of the element 1: Blabla in field1
MAIN::field2 of the element 1: Blabla in flied2
MAIN::field1 of the element 2: Blabla in field1
MAIN::field2 of the element 2: Blabla in flied2
MAIN::field1 of the element 3: Blabla in field1
MAIN::field2 of the element 3: Blabla in flied2
MAIN::field1 of the element 4: Blabla in field1
MAIN::field2 of the element 4: Blabla in flied2
MAIN::field1 of the element 5: Blabla in field1
MAIN::field2 of the element 5: Blabla in flied2
MAIN::field1 of the element 6: Blabla in field1
MAIN::field2 of the element 6: Blabla in flied2
MAIN::field1 of the element 7: Blabla in field1
MAIN::field2 of the element 7: Blabla in flied2
MAIN::field1 of the element 8: Blabla in field1
MAIN::field2 of the element 8: Blabla in flied2
MAIN::field1 of the element 9: Blabla in field1
MAIN::field2 of the element 9: Blabla in flied2

Upvotes: 1

Related Questions