Click to See Complete Forum and Search --> : When to use "new" with "struct"


mingfai_siuming
March 10th, 2003, 06:16 PM
I'm reading a book on C, it doesn't say anything about constructor or default constructor for structs. But i've heard others talk about using "new" keyword to instantiate structs.

Is it messed up? Is this something only in C++?

Help much appreciated.

ming

p.s. best to email me or cc my email address.

Kheun
March 10th, 2003, 06:57 PM
Constructor, destructor and new are only available in C++, not in C. In C++, struct is equivalent to the class keyword except that the default access to member variable is always public.

mingfai_siuming
March 10th, 2003, 07:12 PM
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;
}

Kheun
March 10th, 2003, 07:33 PM
Yes, you are right! Default constructor and destructor are generated if you did not declare.

When you just declare a node object (mynode), it is created on the stack. So when the function ends, the node object goes out-of-scope, thus it is destroyed. Therefore, the return value from CreateNodeWithData() is a pointer to a destroyed object (invalid pointer).

You can modify CreateNodeWithData() to create the node object from free-store using new keyword. In this case, you would have to use delete to destroy the object manually when not in use.

node* CreateNodeWithData( int data )
{
node* pNode = new node;
newnode->data = data;
newnode->next = null;
return pNode;
}

int main(void)
{
node* pNode = CreateNodeWithData(10);
delete pNode;
return 0;
}

Andreas Masur
March 11th, 2003, 02:48 AM
Originally posted by Kheun
Constructor, destructor and new are only available in C++, not in C. In C++, struct is equivalent to the class keyword except that the default access to member variable is always public.
...and that the default inheritence is public for structs instead of private for classes while you derive from it....

KevinHall
March 11th, 2003, 01:52 PM
I'm reading a book on C, it doesn't say anything about constructor or default constructor for structs. But i've heard others talk about using "new" keyword to instantiate structs.


struct node
{ int data; node* next; }

// some main program
void main()
{
...
node mynode;
mynode.data = 1000;
mynode.next = null;
}



A couple of things I noticed:

(1) you need a semi-colon after the definition of node.

{ int data; node* next; } ;


(2) If you are writing a C program, then to declare mynode you need to write
struct node mynode;

or


typedef struct node
{ int data; node* next; } node;

// some main program
void main()
{
...
node mynode;
...
}

Paul McKenzie
March 11th, 2003, 05:57 PM
Originally posted by mingfai_siuming
I'm reading a book on C, it doesn't say anything about constructor or default constructor for structs.Is your goal to learn C++ or 'C'? If it is to learn C++, put away the 'C' book and get a C++ book. You'll only get more confused and you'll have to unlearn a lot of coding habits if you learn 'C' when your goal is to learn C++.

p.s. best to email me or cc my email address. You asked your question here, expect to get a response here.

Regards,

Paul McKenzie

Kheun
March 11th, 2003, 07:12 PM
Originally posted by Paul McKenzie
Is your goal to learn C++ or 'C'? If it is to learn C++, put away the 'C' book and get a C++ book. You'll only get more confused and you'll have to unlearn a lot of coding habits if you learn 'C' when your goal is to learn C++.

I agree with Paul. Personally, I have been using C 5 years before learning C++. At that time, C++ isn't popular yet. Making the paradigm shift to C++ is very painful and confusing. I have to unlearn many things I picked up from C so that I can utilize the C++ language properly.

mingfai_siuming
March 11th, 2003, 09:00 PM
thanks all it's now cleared up.