The difference between new node() and new node?

R

remlostime

now, I define the struct following:
struct node
{
bool match;
node* child[27];
};

What's the difference between
1) node *Trie=new node(); and 2) node *Trie = new node;
 
L

Linlin Yan

now, I define the struct following:
struct node
{
        bool match;
        node* child[27];

};

What's the difference between
1) node *Trie=new node(); and 2) node *Trie = new node;

no difference.
 
J

James Kanze

now, I define the struct following:
struct node
{
bool match;
node* child[27];
};
What's the difference between
1) node *Trie=new node(); and 2) node *Trie = new node;

new Node() zero initializes the data, i.e. match will be false,
and all of the pointers will be NULL. new Node doesn't; the
contents are undefined, and reading them (before having set them
otherwise) is undefined behavior.
 
J

Juha Nieminen

James said:
new Node() zero initializes the data, i.e. match will be false,
and all of the pointers will be NULL. new Node doesn't

Btw, is there anything equivalent for allocating arrays?
 
T

Thomas J. Gritzan

Juha said:
Btw, is there anything equivalent for allocating arrays?

Sure.

// allocates uninitialized ints
int* arry1 = new int[100];

// allocates ints initialized to 0 on recent (=conformant) compilers
int* arry2 = new int[100]();

// allocates ints initialized to 0 on all compilers
std::vector<int> arry3(100);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top