473,799 Members | 2,723 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(s truct 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 1573
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(s truct 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(s truct 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(s truct 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.u nevun...@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(s truct 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.u nevun...@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(s truct 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.u nevun...@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(s truct 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.u nevun...@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(s truct 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.u nevun...@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(s truct 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.l earn.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
34378
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 the actual function call. I will post the code below of the structure, the prototype and and function call and if someone can explain this I would be very appreciative: struct data { float amount; string fname;
2
2466
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
5559
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 , which same structure but different name?
8
3326
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. However, the DEBUG_EVENT structure is defined as a union, and the size and contents vary depending on the event code contained in the header. typedef struct _DEBUG_EVENT { DWORD dwDebugEventCode; DWORD dwProcessId;
6
5019
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 class MyClass {
4
4759
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++, there is no complain about the defintion. But
3
4106
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 dont have any issues. but according to some norms in the review I am not supposed to use this file direclty in the header file .. but its fine if i use that inside the corresponding file. To avoid compiler error, i forward declared the structure...
5
1708
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 -I/opt/systinet/server_cpp65/include/ -I../.. +DD64 /opt/systinet/server_cpp65/lib/libwasp.sl /opt/systinet/server_cpp65/lib/libwasp_stl.sl -ldcekt -lpthread
9
2523
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 FlyCaptureCameraModel { blabla, blabla2 };
0
9543
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10029
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7567
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6808
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4144
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.