Topic: bool()


Author: "Radoslav Getov" <nospam@mai.com>
Date: Mon, 6 Aug 2001 17:26:55 GMT
Raw View
Thanks for the replies. Now I am wondering wchih (if any) of

   int* i = new int[10];

or

   int* j = new int;

or
   int* k = new int();

would initialize to 0?

Tnaks in advance.
Radoslav Getov



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Ron Natalie <ron@sensor.com>
Date: Mon, 6 Aug 2001 18:35:09 GMT
Raw View

Radoslav Getov wrote:

>
>    int* i = new int[10];

Leaves values indeterminate.
>
> or
>
>    int* j = new int;

Leaves value indenterminate.

>
> or
>    int* k = new int();
>

initializes to zero.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Gabriel Dos_Reis <gdosreis@sophia.inria.fr>
Date: Mon, 6 Aug 2001 19:53:02 GMT
Raw View
"Radoslav Getov" <nospam@mai.com> writes:

| Thanks for the replies. Now I am wondering wchih (if any) of
|
|    int* i = new int[10];

No intialization.

| or
|
|    int* j = new int;

Detto.

| or
|    int* k = new int();

Initialize to 0.

Note however that std::vector<int>(10) is guaranteed to initlize all
its ten ints to zero.

--
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Radoslav Getov" <nospam@mai.com>
Date: Mon, 30 Jul 2001 20:42:32 GMT
Raw View
For some strange reason I used to think that 'int j' does not initialize
'j', while e.g int j= int() initializes it to 0 (and similarly for the other
built-in types).

I am not sure about it anymore (cause it's safer), and could not find
anything about it in the standard.

Does anybody know better?

Thanks,

Radoslav Getov



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Roger Orr" <rogero@howzatt.demon.co.uk>
Date: Tue, 31 Jul 2001 11:12:51 GMT
Raw View
Radoslav Getov wrote in message ...
>For some strange reason I used to think that 'int j' does not initialize
>'j', while e.g int j= int() initializes it to 0 (and similarly for the
other
>built-in types).
>
>I am not sure about it anymore (cause it's safer), and could not find
>anything about it in the standard.
>
You are right.  See 5.2.3 Explicit type conversion (functional notation)

"2 The expression T(), where T is a simple   type   specifier (7.1.5.2) for a
non   array complete object type or
the (possibly cv   qualified) void type, creates an rvalue of the specified
type, whose value is determined by
default   initialization..."

and 8.5 explains that this means zero-initializing.

To quote Stroustrup the C++ programming language (3rd ed p131)
"This use of the constructor notation for built-in types is particularly
important when writing templates."

That is, when you _know_ you've got an int you can always write:-

    int i = 0;

but in a template you don't know what a sensible default value is, so for
example:-

    template<typename T>
    T some_function( T t )
    {
        T initial_value = T();
        ...

Hope this helps,
Roger Orr
--
MVP in C++ at www.brainbench.com




---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Gabriel Dos_Reis <gdosreis@sophia.inria.fr>
Date: Tue, 31 Jul 2001 18:38:05 GMT
Raw View
"Radoslav Getov" <nospam@mai.com> writes:

| I am not sure about it anymore (cause it's safer), and could not find
| anything about it in the standard.

5.2.3/2
   The expression T(), where T is a simple-type-specifier (7.1.5.2)
   for a non-array complete object type or the (possibly cv-qualified)
   void type, creates an rvalue of the specified type, whose value is
   determined by default-initialization (8.5; no initialization is
   done for the void() case).

8.5/5
   To zero-initialize storage for an object of type T means:
   --- if T is a scalar type (3.9), the storage is set to the value of
       0 (zero) converted to T;
   [...]

   To default-initialize an object of type T means:
   --- if T is a non-POD class type (clause 9), the default
       constructor for T is called (and the initialization is
       ill-formed if T has no accessible default constructor);

   --- if T is an array type, each element is default-initialized;

   --- otherwise, the storage for the object is zero-initialized.

--
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Anthony Williams" <anthwil@nortelnetworks.com>
Date: Tue, 31 Jul 2001 18:38:18 GMT
Raw View
"Radoslav Getov" <nospam@mai.com> wrote in message
news:tmbe9tk7lq6442@corp.supernews.com...
> For some strange reason I used to think that 'int j' does not initialize
> 'j', while e.g int j= int() initializes it to 0 (and similarly for the
other
> built-in types).
>
> I am not sure about it anymore (cause it's safer), and could not find
> anything about it in the standard.

The "strange reason" can only be that you were right :)

5.2.3p2 says

"The expression T(), ..., creates an rvalue of the specified type, whose
value is determined by default-initialization (8.5)"

8.5p5 says

"...To default-initialize an object of type T means:
- if T is a non-POD class type (clause 9), the default constructor for T is
called (and the initialization is ill-formed if T has no accessible default
constructor);
- if T is an array type, each element is default-initialized;
- otherwise, the storage for the object is zero-initialized."

So int() is an rvalue of type int, with the value zero. Thus

int j=int();

initializes j from an rvalue of type int with value zero - i.e. j==0.

Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optoelectronics
The opinions expressed in this message are not necessarily those of my
employer



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]