On 8/18/2012 12:52 PM, Черкасов Эдгар wrote:
> Hello!
> I checked my small prog with valgrind and faced with the problem: I can't
> understand which uses uninitialised value.
> I tried to replace keyword.key = (char *) malloc (sizeof (char) * (strlen
> (key) + 1)); to keyword.key = (char *) calloc (sizeof (char), (strlen (key) +
> 1)); and there is no errors.
>
> Function with errors:
> struct key {
> char *key;
> int *index;
> int length;
> };
>
> struct key
> gen_key_struct (char *key)
> {
> int i;
> struct key keyword;
>
> prepare_key (key);
> keyword.key = (char *) malloc (sizeof (char) * (strlen (key) + 1));
> i = 0;
> do {
> keyword.key[i] = key[i];
> i++;
> } while (key[i] != '\0');
Try replacing this loop with:
strcpy(keyword.key,key);
If necessary, I can explain why there is a problem with your original
implementation but feel it is best that you try to figure it out on your
own first. This is important to understand; your implementation is not
equivalent to strcpy().
> keyword.index = (int *) malloc (sizeof (int) * strlen (keyword.key));
> for (i = 0; i < strlen (keyword.key); i++)
> keyword.index[i] = i+1;
> keyword.length = strlen (keyword.key);
> return keyword;
> }
>
> And error summary:
> ==3242==
> ==3242== HEAP SUMMARY:
> ==3242== in use at exit: 0 bytes in 0 blocks
> ==3242== total heap usage: 25 allocs, 25 frees, 202 bytes allocated
> ==3242==
> ==3242== All heap blocks were freed -- no leaks are possible
> ==3242==
> ==3242== ERROR SUMMARY: 7 errors from 4 contexts (suppressed: 0 from 0)
> ==3242==
> ==3242== 1 errors in context 1 of 4:
> ==3242== Conditional jump or move depends on uninitialised value(s)
> ==3242== at 0x4092B8E: vfprintf (vfprintf.c:1624)
> ==3242== by 0x4097C2E: printf (printf.c:35)
> ==3242== by 0x804888B: main (main.c:161)
> ==3242== Uninitialised value was created by a heap allocation
> ==3242== at 0x402BE68: malloc (in
> /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
> ==3242== by 0x8049266: gen_key_struct (main.c:354)
> ==3242== by 0x80487C7: main (main.c:144)
> ==3242==
> ==3242==
> ==3242== 1 errors in context 2 of 4:
> ==3242== Conditional jump or move depends on uninitialised value(s)
> ==3242== at 0x804931E: gen_key_struct (main.c:363)
> ==3242== by 0x80487C7: main (main.c:144)
> ==3242== Uninitialised value was created by a heap allocation
> ==3242== at 0x402BE68: malloc (in
> /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
> ==3242== by 0x8049266: gen_key_struct (main.c:354)
> ==3242== by 0x80487C7: main (main.c:144)
> ==3242==
> ==3242==
> ==3242== 1 errors in context 3 of 4:
> ==3242== Conditional jump or move depends on uninitialised value(s)
> ==3242== at 0x80492AB: gen_key_struct (main.c:360)
> ==3242== by 0x80487C7: main (main.c:144)
> ==3242== Uninitialised value was created by a heap allocation
> ==3242== at 0x402BE68: malloc (in
> /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
> ==3242== by 0x8049266: gen_key_struct (main.c:354)
> ==3242== by 0x80487C7: main (main.c:144)
> ==3242==
> ==3242==
> ==3242== 4 errors in context 4 of 4:
> ==3242== Conditional jump or move depends on uninitialised value(s)
> ==3242== at 0x80492FB: gen_key_struct (main.c:361)
> ==3242== by 0x80487C7: main (main.c:144)
> ==3242== Uninitialised value was created by a heap allocation
> ==3242== at 0x402BE68: malloc (in
> /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
> ==3242== by 0x8049266: gen_key_struct (main.c:354)
> ==3242== by 0x80487C7: main (main.c:144)
> ==3242==
> ==3242== ERROR SUMMARY: 7 errors from 4 contexts (suppressed: 0 from 0)
>
> My system: Linux edgar-1015BX 3.2.0-29-generic-pae #46-Ubuntu SMP Fri Jul 27
> 17:25:43 UTC 2012 i686 athlon i386 GNU/Linux
>
> ------------------------------------------------------------------------------
> Live Security Virtual Conference
> Exclusive live event will cover all the ways today's security and
> threat landscape has changed and how IT managers can respond. Discussions
> will include endpoint security, mobile security and the latest in malware
> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
> _______________________________________________
> Valgrind-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/valgrind-users
>
>
--
David Chapman [email protected]
Chapman Consulting -- San Jose, CA
Software Development Done Right.
www.chapman-consulting-sj.com
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Valgrind-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/valgrind-users