Groups | Search | Server Info | Keyboard shortcuts | Login | Register
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: why do some writers declare and define variables separately |
| Date | 2013-09-26 11:17 +1200 |
| Message-ID | <bah98eFkubpU1@mid.individual.net> (permalink) |
| References | (3 earlier) <l1ulhq$2lp$1@dont-email.me> <bagu1qFkslhU3@mid.individual.net> <524349BA.1090100@verizon.net> <bah27qFkslhU4@mid.individual.net> <52436899.9000501@verizon.net> |
James Kuyper wrote:
> On 09/25/2013 05:17 PM, Ian Collins wrote:
>> James Kuyper wrote:
> ....
>>> However, not "having thought much" about a variable is not the same as
>>> "not knowing how it would be used". In general, the very first thought I
>>> have about a variable is "how it would be used". How it should be
>>> initialized is often a much later thought.
>>
>> At the point of first use.
>>
>> That way you always know how it is going to be used and what the correct
>> initial value is. No uninitialised variables to worry or warn about.
>
> It's not uncommon for the point of first use to be a location where you
> can't define the variable because doing so would give it the wrong
> scope. For instance, first use might occur inside a block, even though
> the variable must have a scope that extends outside that block. Such a
> variable's definition would need to be in an outer scope, and would need
> to occur before entry into that block - which might means that the
> definition will be reached before the program can determine what value
> it should have at the point of first use. Therefore, your criterion does
> not solve the problem of whether, and how, such variables should be
> initialized.
Yes and no. Such cases tend to require a means to determine whether the
value was correctly assigned, so a known default initial values is
required. I'm thinking of this style:
int result = FAIL;
if (someCondition)
{
// do something to get the result;
}
else
{
// do something else to get the result;
}
return result;
In most cases, this situation is a hint to extract the code in the block
into a separate function which will either remove the need for the
variable, or allow it to be initialised with the result of the function.
>> Well, OK, there are output parameter values which don't have a "correct"
>> initialisation value. These leave you with a choice of a compiler
>> warning or a dummy initial value.
>
> When I first read that message, I didn't notice your restriction of that
> comment to "output parameter values", and therefore misinterpreted that
> statement as meaning something quite different - I've cancelled the
> message that I wrote reflecting that misunderstanding, but cancellation
> requests are only occasionally honored, and I see that you've already
> responded to it.
>
> Now that I've noticed that restriction, I'm not quite sure what you're
> talking about. An "output" parameter value suggests to me that you're
> talking about a pointer parameter used to write the output to the
> pointed-at location. The parameter itself is initialized by copying from
> the corresponding function argument. The compiler has no way of knowing
> whether or not the memory it points is uninitialized, and therefore
> can't warn you about the possibility of using uninitialized memory.
> Therefore, you don't need to provide a dummy initial value to shut up
> that warning, which is good, because you're also not allowed to provide one.
>
> Therefore, I've almost certainly misunderstood the situation you're
> describing. Could you expand on your comment a little? A concrete
> example might be helpful.
I was referring to a parameter used for an additional return value.
Something like
int doSomething( T* someStuff, int* someSize );
where someSize is filled in by the function.
--
Ian Collins
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar
why do some writers declare and define variables separately yoodavid <odimegwudavid@yahoo.fr> - 2013-09-18 16:42 -0500
Re: why do some writers declare and define variables separately Fred K <fred.l.kleinschmidt@gmail.com> - 2013-09-18 14:52 -0700
Re: why do some writers declare and define variables separately Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-09-18 18:08 -0400
Re: why do some writers declare and define variables separately T0xicCode <T0xic@co.de> - 2013-09-18 18:10 -0400
Re: why do some writers declare and define variables separately Ken Brody <kenbrody@spamcop.net> - 2013-09-19 11:16 -0400
Re: why do some writers declare and define variables separately T0xicCode <T0xic@co.de> - 2013-09-19 11:45 -0400
Re: why do some writers declare and define variables separately Keith Thompson <kst-u@mib.org> - 2013-09-18 15:45 -0700
Re: why do some writers declare and define variables separately Jens Schmidt <Jens.Schmidt-HH@gmx.de> - 2013-09-19 01:19 +0200
Re: why do some writers declare and define variables separately Francis Glassborow <francis.glassborow@btinternet.com> - 2013-09-19 02:13 +0100
Re: why do some writers declare and define variables separately Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-09-19 03:59 +0100
Re: why do some writers declare and define variables separately Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2013-09-27 00:49 +0300
Re: why do some writers declare and define variables separately Anand Hariharan <mailto.anand.hariharan@gmail.com> - 2013-10-03 15:41 -0700
Re: why do some writers declare and define variables separately Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-10-04 00:42 +0100
Re: why do some writers declare and define variables separately Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-09-19 00:01 -0400
Re: why do some writers declare and define variables separately David Brown <david@westcontrol.removethisbit.com> - 2013-09-19 10:17 +0200
Re: why do some writers declare and define variables separately "osmium" <r124c4u102@comcast.net> - 2013-09-19 06:44 -0500
Re: why do some writers declare and define variables separately Les Cargill <lcargill99@comcast.com> - 2013-09-19 08:00 -0500
Re: why do some writers declare and define variables separately Noob <root@127.0.0.1> - 2013-09-19 15:12 +0200
Re: why do some writers declare and define variables separately Keith Thompson <kst-u@mib.org> - 2013-09-19 08:31 -0700
Re: why do some writers declare and define variables separately Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-09-19 11:35 -0400
Re: why do some writers declare and define variables separately Keith Thompson <kst-u@mib.org> - 2013-09-19 12:16 -0700
Re: why do some writers declare and define variables separately Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-09-19 15:32 -0400
Re: why do some writers declare and define variables separately Tim Rentsch <txr@alumni.caltech.edu> - 2013-09-22 12:36 -0700
Re: why do some writers declare and define variables separately Tim Rentsch <txr@alumni.caltech.edu> - 2013-09-22 11:28 -0700
Re: why do some writers declare and define variables separately Öö Tiib <ootiib@hot.ee> - 2013-09-23 03:26 -0700
Re: why do some writers declare and define variables separately Tim Rentsch <txr@alumni.caltech.edu> - 2013-09-24 20:33 -0700
Re: why do some writers declare and define variables separately Öö Tiib <ootiib@hot.ee> - 2013-09-25 04:37 -0700
Re: why do some writers declare and define variables separately Keith Thompson <kst-u@mib.org> - 2013-09-25 08:24 -0700
Re: why do some writers declare and define variables separately glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-09-25 19:08 +0000
Re: why do some writers declare and define variables separately Les Cargill <lcargill99@comcast.com> - 2013-09-25 17:37 -0500
Re: why do some writers declare and define variables separately Tim Rentsch <txr@alumni.caltech.edu> - 2013-09-22 11:34 -0700
Re: why do some writers declare and define variables separately gordonb.t5wsh@burditt.org (Gordon Burditt) - 2013-09-25 05:29 -0500
Re: why do some writers declare and define variables separately Ian Collins <ian-news@hotmail.com> - 2013-09-25 22:35 +1200
Re: why do some writers declare and define variables separately David Brown <david@westcontrol.removethisbit.com> - 2013-09-25 13:02 +0200
Re: why do some writers declare and define variables separately James Kuyper <jameskuyper@verizon.net> - 2013-09-25 08:39 -0400
Re: why do some writers declare and define variables separately Keith Thompson <kst-u@mib.org> - 2013-09-25 08:23 -0700
Re: why do some writers declare and define variables separately Ian Collins <ian-news@hotmail.com> - 2013-09-26 08:06 +1200
Re: why do some writers declare and define variables separately James Kuyper <jameskuyper@verizon.net> - 2013-09-25 16:38 -0400
Re: why do some writers declare and define variables separately Ian Collins <ian-news@hotmail.com> - 2013-09-26 09:17 +1200
Re: why do some writers declare and define variables separately Öö Tiib <ootiib@hot.ee> - 2013-09-25 14:32 -0700
Re: why do some writers declare and define variables separately Ian Collins <ian-news@hotmail.com> - 2013-09-26 09:50 +1200
Re: why do some writers declare and define variables separately Ian Collins <ian-news@hotmail.com> - 2013-09-26 09:55 +1200
Re: why do some writers declare and define variables separately James Kuyper <jameskuyper@verizon.net> - 2013-09-25 18:50 -0400
Re: why do some writers declare and define variables separately Ian Collins <ian-news@hotmail.com> - 2013-09-26 11:17 +1200
Re: why do some writers declare and define variables separately James Kuyper <jameskuyper@verizon.net> - 2013-09-25 21:26 -0400
Re: why do some writers declare and define variables separately Ian Collins <ian-news@hotmail.com> - 2013-09-26 13:50 +1200
Re: why do some writers declare and define variables separately Noob <root@127.0.0.1> - 2013-09-25 15:55 +0200
Re: why do some writers declare and define variables separately James Kuyper <jameskuyper@verizon.net> - 2013-09-25 10:03 -0400
Re: why do some writers declare and define variables separately Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2013-09-27 01:29 +0300
Re: why do some writers declare and define variables separately Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2013-09-27 01:20 +0300
Re: why do some writers declare and define variables separately David Brown <david@westcontrol.removethisbit.com> - 2013-09-25 13:05 +0200
Re: why do some writers declare and define variables separately Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-09-25 23:01 +0100
Re: why do some writers declare and define variables separately James Kuyper <jameskuyper@verizon.net> - 2013-09-25 18:56 -0400
Re: why do some writers declare and define variables separately David Brown <david@westcontrol.removethisbit.com> - 2013-09-26 09:10 +0200
Re: why do some writers declare and define variables separately James Kuyper <jameskuyper@verizon.net> - 2013-09-26 07:31 -0400
Re: why do some writers declare and define variables separately David Brown <david@westcontrol.removethisbit.com> - 2013-09-26 13:58 +0200
Re: why do some writers declare and define variables separately James Kuyper <jameskuyper@verizon.net> - 2013-09-26 09:12 -0400
Re: why do some writers declare and define variables separately Michael Lenz <unrest@nullvector.org> - 2013-10-03 19:06 +0000
Re: why do some writers declare and define variables separately James Kuyper <jameskuyper@verizon.net> - 2013-10-03 15:26 -0400
Re: why do some writers declare and define variables separately Dag-Erling Smørgrav <des@des.no> - 2013-10-03 21:15 +0200
csiph-web