Computer Science Canada

Valgrind error.......need help by tomorrow or wednesday plz thanks

Author:  Rensoz [ Tue Mar 06, 2012 12:52 am ]
Post subject:  Valgrind error.......need help by tomorrow or wednesday plz thanks

Hi,
I am working on an assignment for my university and I have this error. Well everyone I know is having this error, if any can help me fix this that would be great thanks.
There was no memory leaks by the way. Very Happy
Everything works fine(does what needs to happen) , I just want to get off this stupid error.
Here is there error is get when I used


valgrind --leak-check=yes --track-origins=yes -v --error-limit=no ./a.out

code:


WARNING: new redirection conflicts with existing --ignoring it
       new : 0x04ea4b40 (__GI_strlen            ) R-> 0x04c257f0 strlen

ERROR SUMMARY: 1 errors from1 contexts (suppressed: 4 from 4)

1 errors in context 1 of 1:
Invalid read size 1
         at 0x4E6ED8A: vfprintf (vfprintf.c:1614)
         by 0x4E757C7: fprintf (fprintf.c:33)
         by 0x400BEC: main (assign5.c:58)

Address 0x518a362 is 0 bytes after a block of size 2 alloc'd
         at 0x4C244E8: malloc (vg_replace_malloc.c:236)
         by 0x400B64: main (assign5.c:55)

used_suppression:                2 dl-hack3-cond-1
used_suppression:                2 glibc-2.5.x-on-SUSE-10.2-(ppc)-2a



My code:
code:

typedef struct {
     char *name;
     float price;
     int quantity;
} address;

int main()
{
address *info =NULL;
info = malloc(sizeof(address));
int x = 0;
static int 1 = 0;

while (d)
{
x++;
info = realloc(info, sizeoff(address)*x);

/* LINE 54 */  int length = strnlen (temp)
/* LINE 55 */  info[i].name = malloc(sizeof(char)*length);
/* LINE 56 */  strncpy (info[i].name, temp, length);
/* LINE 58 */  fprintf(inventory, "%s \n",  info[i].name)

Author:  Rensoz [ Tue Mar 06, 2012 2:35 am ]
Post subject:  RE:Valgrind error.......need help by tomorrow or wednesday plz thanks

Couldn't put my entire code here. Plagarism problems sorry Sad

Author:  bl0ckeduser [ Tue Mar 06, 2012 10:09 am ]
Post subject:  Re: Valgrind error.......need help by tomorrow or wednesday plz thanks

You have to increase "length" by one because strlen/strnlen do not count the null terminator,
so there wasn't enough space allocated in the new array to write one.

From the manual:
Quote:

The strlen() function shall compute the number of bytes in the string to which s points, not including the terminating null byte.

http://pubs.opengroup.org/onlinepubs/009695399/functions/strlen.html

Hope this helps Very Happy

Author:  Rensoz [ Thu Mar 08, 2012 4:30 am ]
Post subject:  Re: Valgrind error.......need help by tomorrow or wednesday plz thanks

Heyy, thanks for the advice but there wasnt any problem with the length of string. It was something stupid i did which caused this error. forgot brackets around length in malloc. Thanks tho. Very Happy


: