f



The difference between new node() and new node?

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;
0
remlostime (30)
9/5/2008 10:52:18 AM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

4 Replies
579 Views

Similar Articles

[PageSpeed] 0

On Sep 5, 6:52=A0pm, remlostime <remlost...@gmail.com> wrote:
> now, I define the struct following:
> struct node
> {
> =A0 =A0 =A0 =A0 bool match;
> =A0 =A0 =A0 =A0 node* child[27];
>
> };
>
> What's the difference between
> 1) node *Trie=3Dnew node(); and 2) node *Trie =3D new node;

no difference.
0
9/5/2008 11:38:30 AM
On Sep 5, 12:52 pm, remlostime <remlost...@gmail.com> wrote:
> now, I define the struct following:
> struct node
> {
>         bool match;
>         node* child[27];
> };

> What's the difference between
> 1) node *Trie=3Dnew node(); and 2) node *Trie =3D 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.

--
James Kanze (GABI Software)             email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34

0
james.kanze (9769)
9/5/2008 12:34:16 PM
James Kanze wrote:
> 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?
0
nospam270 (2948)
9/5/2008 7:33:56 PM
Juha Nieminen schrieb:
> James Kanze wrote:
>> 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?

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);

-- 
Thomas
0
9/5/2008 8:16:24 PM
Reply: