POV-Ray : Newsgroups : povray.off-topic : C++ questions Server Time
7 Sep 2024 03:23:44 EDT (-0400)
  C++ questions (Message 11 to 20 of 123)  
<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>
From: Mueen Nawaz
Subject: Re: C++ questions
Date: 24 Sep 2008 16:53:32
Message: <48daa8cc$1@news.povray.org>
Invisible wrote:
> My understanding is that when you create variables, they start off 
> containing junk unless you initialise them (or their types have 
> constructors which initialise them to something specific). Is that correct?

	I may be wrong, but I believe all variables declared outside of a 
function have default values.


-- 
For Sale: Parachute. Only used once, never opened, small.


                     /\  /\               /\  /
                    /  \/  \ u e e n     /  \/  a w a z
                        >>>>>>mue### [at] nawazorg<<<<<<
                                    anl


Post a reply to this message

From: Darren New
Subject: Re: C++ questions
Date: 24 Sep 2008 17:37:03
Message: <48dab2ff@news.povray.org>
Mueen Nawaz wrote:
>     I may be wrong, but I believe all variables declared outside of a 
> function have default values.

More exactly, in C at least, any variables allocated statically start 
with a default value of zero appropriate for their type. That includes 
static variables allocated inside a function.

-- 
Darren New / San Diego, CA, USA (PST)


Post a reply to this message

From: Warp
Subject: Re: C++ questions
Date: 24 Sep 2008 17:51:27
Message: <48dab65f@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Mueen Nawaz wrote:
> >     I may be wrong, but I believe all variables declared outside of a 
> > function have default values.

> More exactly, in C at least, any variables allocated statically start 
> with a default value of zero appropriate for their type. That includes 
> static variables allocated inside a function.

  It would be interesting to see a quote from the C standard.

-- 
                                                          - Warp


Post a reply to this message

From: Fredrik Eriksson
Subject: Re: C++ questions
Date: 24 Sep 2008 18:17:10
Message: <op.uh0auvb47bxctx@e6600>
On Wed, 24 Sep 2008 23:51:27 +0200, Warp <war### [at] tagpovrayorg> wrote:
> Darren New <dne### [at] sanrrcom> wrote:
>> More exactly, in C at least, any variables allocated statically start
>> with a default value of zero appropriate for their type. That includes
>> static variables allocated inside a function.
>
>   It would be interesting to see a quote from the C standard.

How about the C++ standard?



"The storage for objects with static storage duration (3.7.1) shall be  
zero-initialized (8.5) before any other initialization takes place."



-- 
FE


Post a reply to this message

From: Darren New
Subject: Re: C++ questions
Date: 24 Sep 2008 19:27:19
Message: <48daccd7$1@news.povray.org>
Warp wrote:
> Darren New <dne### [at] sanrrcom> wrote:
>> Mueen Nawaz wrote:
>>>     I may be wrong, but I believe all variables declared outside of a 
>>> function have default values.
> 
>> More exactly, in C at least, any variables allocated statically start 
>> with a default value of zero appropriate for their type. That includes 
>> static variables allocated inside a function.
> 
>   It would be interesting to see a quote from the C standard.

http://atrey.karlin.mff.cuni.cz/projekty/vrr/doc/c99.pdf

Section 6.7.8, page 126 (or sheet 138). Check rule 10.

But come on, this has been the case since K&R First Edition. :-)

-- 
Darren New / San Diego, CA, USA (PST)


Post a reply to this message

From: Mueen Nawaz
Subject: Re: C++ questions
Date: 24 Sep 2008 23:46:48
Message: <48db09a8$1@news.povray.org>
Darren New wrote:
> Mueen Nawaz wrote:
>>     I may be wrong, but I believe all variables declared outside of a 
>> function have default values.
> 
> More exactly, in C at least, any variables allocated statically start 
> with a default value of zero appropriate for their type. That includes 
> static variables allocated inside a function.

	Yes, I seem to recall that it's true for static variables as well (in 
C++ - not sure about C).

-- 
Atheism is a non-prophet organization.


                     /\  /\               /\  /
                    /  \/  \ u e e n     /  \/  a w a z
                        >>>>>>mue### [at] nawazorg<<<<<<
                                    anl


Post a reply to this message

From: Darren New
Subject: Re: C++ questions
Date: 25 Sep 2008 00:21:55
Message: <48db11e3@news.povray.org>
Mueen Nawaz wrote:
>     Yes, I seem to recall that it's true for static variables as well 
> (in C++ - not sure about C).

I hedged my bets, not knowing whether statically allocated instances of 
a class with a constructor invoked the constructor. I'm pretty sure it 
does, but I didn't feel like trying to google it up. :-)  Maybe it 
initializes to zero everything before it invokes the constructor or 
something. (Which I would suspect, given that it's often the OS that 
does that initialization as it allocates the pages.)

-- 
Darren New / San Diego, CA, USA (PST)


Post a reply to this message

From: Slime
Subject: Re: C++ questions
Date: 25 Sep 2008 01:25:12
Message: <48db20b8@news.povray.org>
> More exactly, in C at least, any variables allocated statically start with 
> a default value of zero appropriate for their type. That includes static 
> variables allocated inside a function.


I was under the impression that global variables are initialized to zero, 
but you can't trust local variables because the stack has whatever garbage 
data was left on it from previously called functions.

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

From: Slime
Subject: Re: C++ questions
Date: 25 Sep 2008 01:28:19
Message: <48db2173@news.povray.org>
> Does C++ check whether a pointer you're deferencing is zero? Or will it 
> just segfault?

In practice it segfaults. Any time you think there's even a chance of this 
happening, do

assert( ptr );

to catch it ahead of time. (This will assert that it is not 0, or NULL). 
You'll save yourself some headaches.

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

From: Slime
Subject: Re: C++ questions
Date: 25 Sep 2008 01:28:56
Message: <48db2198$1@news.povray.org>
Man, I'm in the wrong time zone. You always get to answer the questions 
first! =)

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.