1

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!

asked May 8, 2012 at 15:21
10
  • 1
    Shouldn't result be an entity **? Commented May 8, 2012 at 15:26
  • 2
    Nope, and this basically works fine for me (once I add the missing headers, declare the count variables and replace the omitted random_int function with literal 4). Can you try the same (hardcode int count = 4) and see if it still fails? If it does, post the minimal compilable code that still shows the problem Commented May 8, 2012 at 15:33
  • 2
    @user There is nothing wrong in what you are doing(ideone.com/V4jgG). Show what random_int function is doing. And may be you are not showing what exactly you are doing. Commented May 8, 2012 at 15:33
  • 3
    How did you compile it? What did the compiler complain about? Where is count declared? Commented May 8, 2012 at 15:34
  • 2
    Have you checked that calloc and strdup aren't failing and returning NULL? Maybe your simulator is running out of memory somehow? Commented May 8, 2012 at 15:58

1 Answer 1

1

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
answered May 8, 2012 at 16:16
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.