CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2003
    Posts
    4

    Question When to use "new" with "struct"

    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.

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    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.

  3. #3
    Join Date
    Mar 2003
    Posts
    4
    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;
    }

  4. #4
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    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.

    Code:
    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;
    }

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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....

  6. #6
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    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.

    Code:
    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.
    Code:
    { int data; node* next; } ;
    (2) If you are writing a C program, then to declare mynode you need to write
    Code:
    struct node mynode;
    or
    Code:
    typedef struct node
    { int data; node* next; } node;
    
    // some main program
    void main()
    {
        ...
        node mynode;
        ...
    }

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: When to use "new" with "struct"

    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

  8. #8
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Re: When to use "new" with "struct"

    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.

  9. #9
    Join Date
    Mar 2003
    Posts
    4
    thanks all it's now cleared up.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured