POV-Ray : Newsgroups : povray.off-topic : C++ questions Server Time
7 Sep 2024 23:27:31 EDT (-0400)
  C++ questions (Message 14 to 23 of 123)  
<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>
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

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

Which timezone is that then?


Post a reply to this message

From: John VanSickle
Subject: Re: C++ questions
Date: 25 Sep 2008 07:32:43
Message: <48db76db@news.povray.org>
Invisible wrote:
> (You knew there were going to be a few at some point...)
> 
> I just want to make sure I've got this absolutely straight in my head. 
> So... a reference is the same as a pointer, except that it has nicer 
> syntax, and you cannot change where it points to?
> 
> If I'm understanding this correctly, a "union" is like several structs 
> with the same base address, and you can treat it was one struct or the 
> other struct, and it's up to you to remember which which struct you're 
> currently using it is. (I.e., the language itself provides no way to 
> distinguish.)
> 
> Here's a perverse question: can a union have member functions?

Yes.

> 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?

Yes.

> Does C++ have a concept of a "null pointer" - i.e., a pointer that 
> doesn't point to anything valid, and can be detected as such?

Yes.

> How does memory allocation work in C++? If a program fills the heap, do 
> you have to explicitly *do* something to enlarge the heap, or does it 
> grow automatically? Does it shrink back again after you release things, 
> or do you have to request that manually?

Depends on the implementation.

Regards,
John


Post a reply to this message

From: Darren New
Subject: Re: C++ questions
Date: 25 Sep 2008 17:46:38
Message: <48dc06be@news.povray.org>
Slime 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.
> 
> 
> 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.

Correct. I was also pointing out that they get initialized to the type's 
version of zero, whatever that might be. (I.e., pointers may not have 
all zero bits in them; they get initialized to null instead.) Rarely can 
you see this difference. :-)

And variables that are static but not global also get initialized.

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


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.