structure defintion vs declaration question 
August 29th, 2008, 01:25 AM
| | | structure defintion vs declaration question
Given the following.....
#include <stdlib.h>
#include <stdio.h>
struct node {
int data;
struct node *next;
};
int main(void)
{
struct node* head;
head = malloc(sizeof(struct node));
head->data = 1;
head->next = NULL;
head = head->next;
free(head);
return 0;
}
Is struct node *next defintion or declaration? | 
August 29th, 2008, 01:35 AM
| | | Re: structure defintion vs declaration question
On Aug 29, 4:24 am, Chad <cdal...@gmail.comwrote: Quote:
Given the following.....
>
#include <stdlib.h>
#include <stdio.h>
>
struct node {
int data;
struct node *next;
>
};
>
int main(void)
{
struct node* head;
>
head = malloc(sizeof(struct node));
head->data = 1;
head->next = NULL;
>
head = head->next;
>
free(head);
>
return 0;
>
}
>
Is struct node *next defintion or declaration?
| It's a declaration.
See 6.7.2.1 | 
August 29th, 2008, 01:55 AM
| | | Re: structure defintion vs declaration question
On Thu, 28 Aug 2008 18:24:53 -0700, Chad <cdalten@gmail.comwrote: Quote:
Given the following.....
>
#include <stdlib.h>
#include <stdio.h>
>
struct node {
int data;
struct node *next;
};
>
int main(void)
{
struct node* head;
>
head = malloc(sizeof(struct node));
head->data = 1;
head->next = NULL;
>
head = head->next;
>
free(head);
>
return 0;
}
>
>
Is struct node *next defintion or declaration?
| After noting vippstar's response to your immediate question ...
.... hope you are aware that your code is leaking memory.
Also, I'd suggest you review the currently active thread 'Pointer
dereference rather than sizeof' and see if you might reconsider how you
wrote your malloc statement.
- Anand | 
August 29th, 2008, 01:55 AM
| | | Re: structure defintion vs declaration question
Chad said: Quote:
Given the following.....
>
#include <stdlib.h>
#include <stdio.h>
>
struct node {
int data;
struct node *next;
};
>
int main(void)
{
struct node* head;
>
head = malloc(sizeof(struct node));
| Better: head = malloc(sizeof *head);
Don't assume malloc succeeded - *check* that it succeeded, and take
appropriate action if it failed. Quote:
head->data = 1;
head->next = NULL;
>
head = head->next;
>
free(head);
>
return 0;
}
>
>
Is struct node *next defintion or declaration?
| It's part of a type definition. The grammar calls it a declarator:
struct-or-union-specifier:
struct-or-union identifier<opt{
struct-declaration-list }
struct-or-union identifier
struct-or-union:
struct
union
struct-declaration-list:
struct-declaration
struct-declaration-list struct-declaration
struct-declaration:
specifier-qualifier-list struct-declarator-list ;
specifier-qualifier-list:
type-specifier specifier-qualifier-list<opt>
type-qualifier specifier-qualifier-list<opt>
struct-declarator-list:
struct-declarator
struct-declarator-list , struct-declarator
struct-declarator:
declarator
declarator<opt: constant-expression
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999 | 
August 29th, 2008, 02:05 AM
| | | Re: structure defintion vs declaration question
On Aug 28, 6:45 pm, Anand Hariharan <znvygb.nanaq.unevun...@tznvy.pbz>
wrote: Quote:
On Thu, 28 Aug 2008 18:24:53 -0700, Chad <cdal...@gmail.comwrote:> Quote:
#include <stdlib.h>
#include <stdio.h>
| > Quote:
struct node {
int data;
struct node *next;
};
| > Quote:
int main(void)
{
struct node* head;
| > Quote:
head = malloc(sizeof(struct node));
head->data = 1;
head->next = NULL;
| >>>> Quote: |
Is struct node *next defintion or declaration?
| >
After noting vippstar's response to your immediate question ...
... hope you are aware that your code is leaking memory.
>
Also, I'd suggest you review the currently active thread 'Pointer
dereference rather than sizeof' and see if you might reconsider how you
wrote your malloc statement.
>
| Okay, how is the code leaking memory? I should see it, but for some
reason, I don't. | 
August 29th, 2008, 02:25 AM
| | | Re: structure defintion vs declaration question
On Aug 29, 4:59 am, Chad <cdal...@gmail.comwrote: Quote:
On Aug 28, 6:45 pm, Anand Hariharan <znvygb.nanaq.unevun...@tznvy.pbz>
wrote:
>
>
> Quote: |
On Thu, 28 Aug 2008 18:24:53 -0700, Chad <cdal...@gmail.comwrote: | > Quote: Quote:
#include <stdlib.h>
#include <stdio.h>
| | > Quote: Quote:
struct node {
int data;
struct node *next;
};
| | > Quote: Quote:
int main(void)
{
struct node* head;
| | > Quote: Quote:
head = malloc(sizeof(struct node));
head->data = 1;
head->next = NULL;
| | >>>> Quote: Quote: |
Is struct node *next defintion or declaration?
| | > Quote:
After noting vippstar's response to your immediate question ...
... hope you are aware that your code is leaking memory.
| > Quote:
Also, I'd suggest you review the currently active thread 'Pointer
dereference rather than sizeof' and see if you might reconsider how you
wrote your malloc statement.
| >
Okay, how is the code leaking memory? I should see it, but for some
reason, I don't.
|
I did not review your code, just answered your question.
However, your code practically does this:
p = malloc();
/* ... */
p = NULL;
free(p);
(note: free(NULL) is defined to do nothing)
Not only you leak memory, but you fail to check the return value of
malloc, so you are also possibly dereferencing a null pointer. | 
August 29th, 2008, 02:25 AM
| | | Re: structure defintion vs declaration question
Chad said: Quote:
On Aug 28, 6:45 pm, Anand Hariharan <znvygb.nanaq.unevun...@tznvy.pbz>
wrote: Quote:
>On Thu, 28 Aug 2008 18:24:53 -0700, Chad <cdal...@gmail.comwrote:>> Quote:
#include <stdlib.h>
#include <stdio.h>
| >> Quote:
struct node {
int data;
struct node *next;
};
| >> Quote:
int main(void)
{
struct node* head;
| >> Quote:
head = malloc(sizeof(struct node));
head->data = 1;
head->next = NULL;
| >>>>>>>> Quote: |
Is struct node *next defintion or declaration?
| >>
>After noting vippstar's response to your immediate question ...
>... hope you are aware that your code is leaking memory.
| | <snip> Quote:
Okay, how is the code leaking memory? I should see it, but for some
reason, I don't.
| head->next = NULL; /* fine, nothing wrong with this. */
head = head->next; /* since head->next is NULL, head is now NULL too,
and you no longer have a record of where your
allocated block is stored. That's a memory leak. */
free(head); /* equivalent to free(NULL) because head is NULL -
this is legal, but does nothing useful. */
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999 | 
August 29th, 2008, 02:25 AM
| | | Re: structure defintion vs declaration question
Chad wrote: Quote:
On Aug 28, 6:45 pm, Anand Hariharan <znvygb.nanaq.unevun...@tznvy.pbz>
wrote: Quote:
>On Thu, 28 Aug 2008 18:24:53 -0700, Chad <cdal...@gmail.comwrote: Quote:
>>Given the following.....
>>#include <stdlib.h>
>>#include <stdio.h>
>>struct node {
>> int data;
>> struct node *next;
>>};
>>int main(void)
>>{
>> struct node* head;
>> head = malloc(sizeof(struct node));
>> head->data = 1;
>> head->next = NULL;
>> head = head->next;
>> free(head);
>> return 0;
>>}
>>Is struct node *next defintion or declaration?
| >After noting vippstar's response to your immediate question ...
>... hope you are aware that your code is leaking memory.
>>
>Also, I'd suggest you review the currently active thread 'Pointer
>dereference rather than sizeof' and see if you might reconsider how you
>wrote your malloc statement.
>>
| >
Okay, how is the code leaking memory? I should see it, but for some
reason, I don't.
| head = head->next;
free(head);
--
pete | 
August 29th, 2008, 02:25 AM
| | | Re: structure defintion vs declaration question
On Thu, 28 Aug 2008 18:59:59 -0700 (PDT), Chad <cdalten@gmail.com>
wrote in comp.lang.c: Quote:
On Aug 28, 6:45 pm, Anand Hariharan <znvygb.nanaq.unevun...@tznvy.pbz>
wrote: Quote:
On Thu, 28 Aug 2008 18:24:53 -0700, Chad <cdal...@gmail.comwrote: Quote:
#include <stdlib.h>
#include <stdio.h>
| Quote:
struct node {
int data;
struct node *next;
};
| Quote:
int main(void)
{
struct node* head;
| Quote: |
head = malloc(sizeof(struct node));
| | | If the call to malloc() above succeeds, head now contains the address
of the allocated memory. Quote: Quote: Quote:
head->data = 1;
head->next = NULL;
| | | The two statements above store data into the allocated memory that
head points to. This is just fine if the malloc() call succeeded,
which, as Richard pointed out, you did not check for. The statement above changes the contents of head. You overwrite the
address of the allocated memory with a null pointer. Since you have
no other copy of the address returned by malloc(), and you just over
wrote the one copy that you did have, you have lost the address. It's
gone, and there's no way y0u can get it back. The statement above calls free() with a null pointer. That's
perfectly valid and does absolutely nothing. But you haven't free'd
the memory you allocated, and you can't because you lost the pointer
to it. Quote: Quote: Quote: |
Is struct node *next defintion or declaration?
| After noting vippstar's response to your immediate question ...
... hope you are aware that your code is leaking memory.
Also, I'd suggest you review the currently active thread 'Pointer
dereference rather than sizeof' and see if you might reconsider how you
wrote your malloc statement.
| >
Okay, how is the code leaking memory? I should see it, but for some
reason, I don't.
| See detailed explanation above.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,989 network members.
|