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