sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
Chad's Avatar

structure defintion vs declaration question


Question posted by: Chad (Guest) on August 29th, 2008 02:25 AM
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?
8 Answers Posted
vippstar@gmail.com's Avatar
vippstar@gmail.com August 29th, 2008 02:35 AM
Guest - n/a Posts
#2: Re: structure defintion vs declaration question

On Aug 29, 4:24 am, Chad <cdal...@gmail.comwrote:
Quote:
Originally Posted by
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
Anand Hariharan's Avatar
Anand Hariharan August 29th, 2008 02:55 AM
Guest - n/a Posts
#3: Re: structure defintion vs declaration question

On Thu, 28 Aug 2008 18:24:53 -0700, Chad <cdalten@gmail.comwrote:
Quote:
Originally Posted by
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
Richard Heathfield's Avatar
Richard Heathfield August 29th, 2008 02:55 AM
Guest - n/a Posts
#4: Re: structure defintion vs declaration question

Chad said:
Quote:
Originally Posted by
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:
Originally Posted by
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
Chad's Avatar
Guest - n/a Posts
#5: Re: structure defintion vs declaration question

On Aug 28, 6:45 pm, Anand Hariharan <znvygb.nanaq.unevun...@tznvy.pbz>
wrote:
Quote:
Originally Posted by
On Thu, 28 Aug 2008 18:24:53 -0700, Chad <cdal...@gmail.comwrote:
Quote:
Originally Posted by
Given the following.....

>
Quote:
Originally Posted by
#include <stdlib.h>
#include <stdio.h>

>
Quote:
Originally Posted by
struct node {
int data;
struct node *next;
};

>
Quote:
Originally Posted by
int main(void)
{
struct node* head;

>
Quote:
Originally Posted by
head = malloc(sizeof(struct node));
head->data = 1;
head->next = NULL;

>
Quote:
Originally Posted by
head = head->next;

>
Quote:
Originally Posted by
free(head);

>
Quote:
Originally Posted by
return 0;
}

>
Quote:
Originally Posted by
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.

vippstar@gmail.com's Avatar
vippstar@gmail.com August 29th, 2008 03:25 AM
Guest - n/a Posts
#6: Re: structure defintion vs declaration question

On Aug 29, 4:59 am, Chad <cdal...@gmail.comwrote:
Quote:
Originally Posted by
On Aug 28, 6:45 pm, Anand Hariharan <znvygb.nanaq.unevun...@tznvy.pbz>
wrote:
>
>
>
Quote:
Originally Posted by
On Thu, 28 Aug 2008 18:24:53 -0700, Chad <cdal...@gmail.comwrote:
Quote:
Originally Posted by
Given the following.....

>
Quote:
Originally Posted by
Quote:
Originally Posted by
#include <stdlib.h>
#include <stdio.h>

>
Quote:
Originally Posted by
Quote:
Originally Posted by
struct node {
int data;
struct node *next;
};

>
Quote:
Originally Posted by
Quote:
Originally Posted by
int main(void)
{
struct node* head;

>
Quote:
Originally Posted by
Quote:
Originally Posted by
head = malloc(sizeof(struct node));
head->data = 1;
head->next = NULL;

>
Quote:
Originally Posted by
Quote:
Originally Posted by
head = head->next;

>
Quote:
Originally Posted by
Quote:
Originally Posted by
free(head);

>
Quote:
Originally Posted by
Quote:
Originally Posted by
return 0;
}

>
Quote:
Originally Posted by
Quote:
Originally Posted by
Is struct node *next defintion or declaration?

>
Quote:
Originally Posted by
After noting vippstar's response to your immediate question ...
... hope you are aware that your code is leaking memory.

>
Quote:
Originally Posted by
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.
Richard Heathfield's Avatar
Richard Heathfield August 29th, 2008 03:25 AM
Guest - n/a Posts
#7: Re: structure defintion vs declaration question

Chad said:
Quote:
Originally Posted by
On Aug 28, 6:45 pm, Anand Hariharan <znvygb.nanaq.unevun...@tznvy.pbz>
wrote:
Quote:
Originally Posted by
>On Thu, 28 Aug 2008 18:24:53 -0700, Chad <cdal...@gmail.comwrote:
Quote:
Originally Posted by
Given the following.....

>>
Quote:
Originally Posted by
#include <stdlib.h>
#include <stdio.h>

>>
Quote:
Originally Posted by
struct node {
int data;
struct node *next;
};

>>
Quote:
Originally Posted by
int main(void)
{
struct node* head;

>>
Quote:
Originally Posted by
head = malloc(sizeof(struct node));
head->data = 1;
head->next = NULL;

>>
Quote:
Originally Posted by
head = head->next;

>>
Quote:
Originally Posted by
free(head);

>>
Quote:
Originally Posted by
return 0;
}

>>
Quote:
Originally Posted by
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:
Originally Posted by
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
pete's Avatar
Guest - n/a Posts
#8: Re: structure defintion vs declaration question

Chad wrote:
Quote:
Originally Posted by
On Aug 28, 6:45 pm, Anand Hariharan <znvygb.nanaq.unevun...@tznvy.pbz>
wrote:
Quote:
Originally Posted by
>On Thu, 28 Aug 2008 18:24:53 -0700, Chad <cdal...@gmail.comwrote:
Quote:
Originally Posted by
>>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
Jack Klein's Avatar
Guest - n/a Posts
#9: 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:
Originally Posted by
On Aug 28, 6:45 pm, Anand Hariharan <znvygb.nanaq.unevun...@tznvy.pbz>
wrote:
Quote:
Originally Posted by
On Thu, 28 Aug 2008 18:24:53 -0700, Chad <cdal...@gmail.comwrote:
Quote:
Originally Posted by
Given the following.....

Quote:
Originally Posted by
#include <stdlib.h>
#include <stdio.h>

Quote:
Originally Posted by
struct node {
int data;
struct node *next;
};

Quote:
Originally Posted by
int main(void)
{
struct node* head;

Quote:
Originally Posted by
head = malloc(sizeof(struct node));


If the call to malloc() above succeeds, head now contains the address
of the allocated memory.
Quote:
Originally Posted by
Quote:
Originally Posted by
Quote:
Originally Posted by
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.
Quote:
Originally Posted by
Quote:
Originally Posted by
Quote:
Originally Posted by
head = head->next;


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.
Quote:
Originally Posted by
Quote:
Originally Posted by
Quote:
Originally Posted by
free(head);


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:
Originally Posted by
Quote:
Originally Posted by
Quote:
Originally Posted by
return 0;
}

Quote:
Originally Posted by
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
 
Not the answer you were looking for? Post your question . . .
197,018 members ready to help you find a solution.
Join Bytes.com

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 197,018 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors