I started learning about struct in C. Today I found a problem, that I can't solve. I have this code:
typedef struct fraze
{
char *mostSearch = NULL; // for string from user
double freq;
} s_FRAZE;
int readFraze( )
{
int i = 2, k;
size_t len = 0;
char c;
s_FRAZE *s;
s = (s_FRAZE *)malloc( i * sizeof( int ));
k = 0;
while( (c = getchar()) != '\n')
{
ungetc( c, stdin );
if( scanf( "%lf%c", &s[k].freq, &c) != 2 || c != ':' )
{
return 1;
}
if( k + 1 >= i )
{
i *= 2;
printf("%d\n", i );
s = (s_FRAZE *)realloc( s, i * sizeof( int ));
}
len = getline(&s[k].mostSearch, &len, stdin );
s[k].mostSearch[len-1] = '\0';
k++;
}
return 0;
}
I want to read while user don't type '\n', but it works 2x and then I get this erorr realloc(): invalid next size: 0x0000000001559010 *** I tryed use valgrind and there are more errors:
==7662== Invalid write of size 8
==7662== at 0x56AEBB4: _IO_vfscanf (vfscanf.c:2328)
==7662== by 0x56BBD3A: scanf (scanf.c:33)
==7662== by 0x40089F: readFraze() (main.c:31)
==7662== by 0x400818: main (main.c:15)
==7662== Address 0x59fe048 is 0 bytes after a block of size 8 alloc'd
==7662== at 0x4C27C0F: malloc (vg_replace_malloc.c:299)
==7662== by 0x400847: readFraze() (main.c:25)
==7662== by 0x400818: main (main.c:15)
==7662==
==7662== Conditional jump or move depends on uninitialised value(s)
==7662== at 0x56BFCA2: getdelim (iogetdelim.c:63)
==7662== by 0x40093E: readFraze() (main.c:44)
==7662== by 0x400818: main (main.c:15)
Can anyone tell me, what am I doing wrong?
malloc
& friends in C. – too honest for this sitemalloc( i * sizeof( int ));
-->malloc( i * sizeof( s_FRAZE ));
assuming you wanti
elements in the array. – Weather Vanerealloc( s, i * sizeof( int ));
-->realloc( s, i * sizeof( s_FRAZE ));
– Weather Vanemalloc()
and family of functions, always check (!=NULL) the returned value to assure the operation was successful – user3629249-Wall -Wextra -pedantic
) The posted code not only raises numerous warnings, but also raises some errors. I.E. the posted code will NEVER run until all the errors are fixed. The only possibility, that I see, is this is not your real code. Please post the real code – user3629249