|
From: Lucas B. <bra...@re...> - 2004-09-22 14:24:09
|
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 *));
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
|
|
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
|
|
From: Lucas B. <bra...@re...> - 2004-09-22 14:58:29
|
Hi Naveen: > 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 )); > Oooops... my mistake :) Thanks a lot. I'd also like to thanks everybody whom also answered (Andreas, Ashley and Jiafu). -- []'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 |
|
From: Tom H. <th...@cy...> - 2004-09-22 14:46:55
|
In message <415...@re...>
Lucas Brasilino <bra...@re...> wrote:
> LinkedList *newLinkedList(void)
> {
> LinkedList *s;
>
> /* memory allocation for LinkedList structure */
> s = (LinkedList *)malloc (sizeof (LinkedList *));
You are only allocating enough memory for a pointer to a LinkedList
here, not for a whole LinkedList structure.
Tom
--
Tom Hughes (th...@cy...)
Software Engineer, Cyberscience Corporation
http://www.cyberscience.com/
|
|
From: Ashley P. <as...@qu...> - 2004-09-22 14:51:22
|
On Wed, 2004-09-22 at 15:23, Lucas Brasilino wrote: > s = (LinkedList *)malloc (sizeof (LinkedList *)); You need to allocate more memory, sizeof(LinkedList) would be better. Ashley, |