By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,938 Members | 2,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,938 IT Pros & Developers. It's quick & easy.

structure defintion vs declaration question

P: n/a
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?
Aug 29 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Aug 29, 4:24 am, Chad <cdal...@gmail.comwrote:
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
Aug 29 '08 #2

P: n/a
On Thu, 28 Aug 2008 18:24:53 -0700, Chad <cd*****@gmail.comwrote:
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
Aug 29 '08 #3

P: n/a
Chad said:
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.
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
Aug 29 '08 #4

P: n/a
On Aug 28, 6:45 pm, Anand Hariharan <znvygb.nanaq.unevun...@tznvy.pbz>
wrote:
On Thu, 28 Aug 2008 18:24:53 -0700, Chad <cdal...@gmail.comwrote:
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.

Aug 29 '08 #5

P: n/a
On Aug 29, 4:59 am, Chad <cdal...@gmail.comwrote:
On Aug 28, 6:45 pm, Anand Hariharan <znvygb.nanaq.unevun...@tznvy.pbz>
wrote:
On Thu, 28 Aug 2008 18:24:53 -0700, Chad <cdal...@gmail.comwrote:
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.

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.
Aug 29 '08 #6

P: n/a
Chad said:
On Aug 28, 6:45 pm, Anand Hariharan <znvygb.nanaq.unevun...@tznvy.pbz>
wrote:
>On Thu, 28 Aug 2008 18:24:53 -0700, Chad <cdal...@gmail.comwrote:
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.
<snip>
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
Aug 29 '08 #7

P: n/a
Chad wrote:
On Aug 28, 6:45 pm, Anand Hariharan <znvygb.nanaq.unevun...@tznvy.pbz>
wrote:
>On Thu, 28 Aug 2008 18:24:53 -0700, Chad <cdal...@gmail.comwrote:
>>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
Aug 29 '08 #8

P: n/a
On Thu, 28 Aug 2008 18:59:59 -0700 (PDT), Chad <cd*****@gmail.com>
wrote in comp.lang.c:
On Aug 28, 6:45 pm, Anand Hariharan <znvygb.nanaq.unevun...@tznvy.pbz>
wrote:
On Thu, 28 Aug 2008 18:24:53 -0700, Chad <cdal...@gmail.comwrote:
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));
If the call to malloc() above succeeds, head now contains the address
of the allocated memory.
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.
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.
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.
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.
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
Aug 29 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.