 |
 |
|
 |
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
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] nawaz org<<<<<<
anl
Post a reply to this message
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
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
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
Darren New <dne### [at] san rr com> 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
|
 |
|  |
|  |
|
 |
|
 |
|  |
|  |
|
 |
On Wed, 24 Sep 2008 23:51:27 +0200, Warp <war### [at] tag povray org> wrote:
> Darren New <dne### [at] san rr com> 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] san rr com> 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] nawaz org<<<<<<
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
|
 |
|  |
|  |
|
 |
|
 |
|  |