|
-
March 10th, 2003, 08:12 PM
#3
Thanks!
Thus a declaration of a struct type automatically allocates the storage because of default constructor is built in --> no need to use new operator.
In the following code, i'd like to verify that,
(1) it is ok to declare (which allocates) a node struct to be used in the main program.
(2) it is WRONG to declare (which allocates) a node in the CreateNodeWithData() function because the newnode is allocated on the stack and the caller never gets it when the function returns.
Thanks very much for advice!
ming
======================
struct node
{ int data; node* next; }
// some main program
void main()
{
...
node mynode;
mynode.data = 1000;
mynode.next = null;
}
// This function creates a linked list of nodes and return the list.
node* CreateNodeWithData( int data )
{
node* pNode;
node newnode;
newnode.data = data;
newnode.next = null;
return pNode = &newnode;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|