Structure initialization problem?

I use a structure like this:

define struct _Fragment{
     int a;
     char *seq;
}Fragment;

I want to initialize the structure, and using the malloc () method returns dynamic memory like this

Fragment *frag=malloc(10*sizeof(Fragment));

Then I would use a fragment pointer like this:

frag->seq="01001";

Then the problem arises when I return a lot of fragments. the error message states that (using the valgrind tool):

Uninitialised value was created by a heap allocation

who can tell me how I can handle this. thanks!

+3
source share
4 answers

I'm not sure you have a real problem, but for proper etiquette, your distribution will be:

Fragment *frag=malloc(10*sizeof(Fragment));
if (frag) memset(frag,0,10*sizeof(Fragment));
+6
source

, , malloc , . , malloc, - ,

Fragment* frag = malloc(10*sizeof(Fragment));
int i = 0;
for ( i = 0; i < 10; i++ ) { 
  frag[i].a = 0;
  frag[i].seq = NULL;
}

, calloc. zero'ing , .

, malloc :)

+4

, malloc - , . Valgrind , .

, , , Valgrind (, ), , -, , , . , .

+2

, :

Fragment *frag=malloc(10*sizeof(Fragment));

, 10*?

10 , 10.

0

Source: https://habr.com/ru/post/1712634/


All Articles