|
From: Naveen K. <g_n...@ya...> - 2004-09-22 14:42:51
|
--- Lucas Brasilino <bra...@re...>
wrote:
> Hi!
>
> I think I'm messing out something, but can't figure
> out
> some invalid writes valgrind pointing me out.
> I'm implementing a linked list for a server I'm
> writing that
> will store user configuration from a file. So I did:
>
> struct _LinkedList
> {
> char *key;
> char *data;
> struct _LinkedList *next;
> struct _LinkedList *head;
> };
> typedef struct _LinkedList LinkedList;
>
> int main(void)
> {
>
> LinkedList *usersettings;
>
> usersettings = newLinkedList();
> }
>
> LinkedList *newLinkedList(void)
> {
> LinkedList *s;
>
> /* memory allocation for LinkedList structure */
> s = (LinkedList *)malloc (sizeof (LinkedList *));
You are allocating enough memory here only for one
pointer(i.e LinkedList*) and not for the entire
structure. Thats why the write to s->key (which is of
type char * and occupies size = pointer size) is ok
but all other writes are invalid.
Needs to be changed to
s = (LinkedList *)malloc (sizeof( LinkedList ));
> if (!s)
> return s;
>
> /* storing list's head */
> s->head = s; /* invalid write ???? */
> s->next = NULL; /* invalid write ???? */
>
> s->key = NULL; /* Here valgrind says it's ok (no
> invalid write) */
> s->data = NULL; /* invalid write ???? */
>
> return s;
> }
>
> Any tip or help?
>
> thanks a lot in advance:
>
> --
>
> []'s
> Lucas Brasilino
> bra...@re...
> http://www.recife.pe.gov.br
> Emprel - Empresa Municipal de Informatica (pt_BR)
> Municipal Computing Enterprise (en_US)
> Recife - Pernambuco - Brasil
> Fone: +55-81-34167078
>
>
>
>
-------------------------------------------------------
> This SF.Net email is sponsored by: YOU BE THE JUDGE.
> Be one of 170
> Project Admins to receive an Apple iPod Mini FREE
> for your judgement on
> who ports your project to Linux PPC the best.
> Sponsored by IBM.
> Deadline: Sept. 24. Go here:
> http://sf.net/ppc_contest.php
> _______________________________________________
> Valgrind-users mailing list
> Val...@li...
>
https://lists.sourceforge.net/lists/listinfo/valgrind-users
>
_______________________________
Do you Yahoo!?
Express yourself with Y! Messenger! Free. Download now.
http://messenger.yahoo.com
|