473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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?
Aug 29 '08 #1
8 1544
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
2
by: kimimaro | last post by:
hi I wonder if array can be work along with structure? Below are the declaration of my structure struct employee{ char ID; char Name; char Department;
6
by: yezi | last post by:
Hi: How to code for operation between 2 structures? The structure's defination is : struct sockaddr_in my_addr; I need call a subfunction and pass the value in my_addr to the subfuncition...
8
by: Charles Law | last post by:
Can anyone suggest how I would marshal a variable length structure back from an API call. Specifically, I am looking at the WaitForDebugEvent function, which returns a DEBUG_EVENT structure. ...
6
by: Laurent | last post by:
Hello, This is probably a dumb question, but I just would like to understand how the C# compiler computes the size of the managed structure or classes. I'm working on this class: public...
4
by: huguogang | last post by:
Just curious, any one know what the 3 part parameter "class CString filename" would mean. The code: int TestFunc(class CString filename) { fopen(filename, "w"); } Compile using Visaul C++,...
3
by: DhaneshNair | last post by:
Hi all, I hav a file which is actually linkage file (used as an reference interface between c and c++ files). And this file has got two structures in it .. When i include this file directly i...
5
by: Amit_Basnak | last post by:
Dear Friends I am giving the following command to build the server executableon HP-UX /opt/aCC/bin/aCC -o SOAPServer SOAPServer.o WFWuListService.o WFWuListImpl.o WFWuListStructs.o WFWuList.o...
9
by: A n g l e r | last post by:
Hi all. I've got the following code as a part of managed C++ library that is loaded by a project in C#: public ref class FlyCaptureEnumsAndStructsManaged { public: typedef enum class...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.