Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > programming style: while(TRUE), for(;;), ...?

Reply
Thread Tools

programming style: while(TRUE), for(;;), ...?

 
 
Felix Kater
Guest
Posts: n/a
 
      07-05-2005
Hi,

in some cases like dynamic memory allocation I find it convenient to
(ab-)use for() or while() like this:


/* (allocate memory) */

for(;{ /* alternatively: while(TRUE){ */

/* (other code) */

break;
}

/* (free memory) */


The reason to do that is to remind the programmer who might not be me
not to forget things (like freeing memory) when inserting code later,
keep the order of commands (don't free too early) etc.


Two questions:

Are there generally better ways to do that?

Are there reasons to prefer one of the two ways (for vs, while) ?


Felix
 
Reply With Quote
 
 
 
 
Suman
Guest
Posts: n/a
 
      07-05-2005
Felix Kater wrote:
> Hi,
>
> in some cases like dynamic memory allocation I find it convenient to
> (ab-)use for() or while() like this:
>
>
> /* (allocate memory) */
>
> for(;{ /* alternatively: while(TRUE){ */
>
> /* (other code) */
>
> break;
> }
>
> /* (free memory) */
>
>
> The reason to do that is to remind the programmer who might not be me
> not to forget things (like freeing memory) when inserting code later,
> keep the order of commands (don't free too early) etc.

If you put an infinite loop in between, then that piece of code
will never ever come to free memory part.Right? So prevention has
caused
a disease here.
>
> Two questions:
>
> Are there generally better ways to do that?
>
> Are there reasons to prefer one of the two ways (for vs, while) ?

Do you mean to say whether I'd prefer murder to assasination?

 
Reply With Quote
 
 
 
 
Suman
Guest
Posts: n/a
 
      07-05-2005
Lawrence Kirby wrote:
> On Tue, 05 Jul 2005 04:30:45 -0700, Suman wrote:
>
> > Felix Kater wrote:
> >> Hi,
> >>
> >> in some cases like dynamic memory allocation I find it convenient to
> >> (ab-)use for() or while() like this:
> >>
> >>
> >> /* (allocate memory) */
> >>
> >> for(;{ /* alternatively: while(TRUE){ */
> >>
> >> /* (other code) */
> >>
> >> break;
> >> }
> >>
> >> /* (free memory) */
> >>
> >>
> >> The reason to do that is to remind the programmer who might not be me
> >> not to forget things (like freeing memory) when inserting code later,
> >> keep the order of commands (don't free too early) etc.

>
> I'm not clear how the for() really helps here. Does it potentially use
> break in multiple places to go to the cleanup code?
>
> > If you put an infinite loop in between, then that piece of code
> > will never ever come to free memory part.Right? So prevention has
> > caused
> > a disease here.

>
> The break statement ensures it isn't infinite, unless the loop body
> contains continue statements.

Concur.
> >>
> >> Two questions:
> >>
> >> Are there generally better ways to do that?

do {
/*stuff*/
} while ( 0 );

Or wrap the code in for/while in a function, and call it with proper
arguments. That should be easier, if the constraints under which one
works, allows that.

 
Reply With Quote
 
Felix Kater
Guest
Posts: n/a
 
      07-05-2005
Lawrence Kirby <(E-Mail Removed)> wrote:

> >> The reason to do that is to remind the programmer who might not be me
> >> not to forget things (like freeing memory) when inserting code later,
> >> keep the order of commands (don't free too early) etc.

>
> I'm not clear how the for() really helps here. Does it potentially use
> break in multiple places to go to the cleanup code?


Yes, the multiple break points/exits lead to the same cleanup code.

Second: The opened brace (often together with indentation or folding in
the editor) reminds you that your are programming on something like a
sub level which will end at some point and execute other (important)
code on the main level.

> >> Are there generally better ways to do that?

>
> There's always goto.


Third: I also think of nested situations where gotos are of course hard
to control.

Fourth: I beleve that it is not always a good practise to source out
pieces of code into new functions when these functions are used only
once. Using the suggested practise (together with a editor which can do
folding) could be a good substitution for that.


> >> Are there reasons to prefer one of the two ways (for vs, while) ?

>
> for (; and while (1) are equivalent.


Ok. I'll ask in a compiler's news group to check wheater there are
differences.


Felix
 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      07-05-2005
On Tue, 05 Jul 2005 04:30:45 -0700, Suman wrote:

> Felix Kater wrote:
>> Hi,
>>
>> in some cases like dynamic memory allocation I find it convenient to
>> (ab-)use for() or while() like this:
>>
>>
>> /* (allocate memory) */
>>
>> for(;{ /* alternatively: while(TRUE){ */
>>
>> /* (other code) */
>>
>> break;
>> }
>>
>> /* (free memory) */
>>
>>
>> The reason to do that is to remind the programmer who might not be me
>> not to forget things (like freeing memory) when inserting code later,
>> keep the order of commands (don't free too early) etc.


I'm not clear how the for() really helps here. Does it potentially use
break in multiple places to go to the cleanup code?

> If you put an infinite loop in between, then that piece of code
> will never ever come to free memory part.Right? So prevention has
> caused
> a disease here.


The break statement ensures it isn't infinite, unless the loop body
contains continue statements.

>>
>> Two questions:
>>
>> Are there generally better ways to do that?


There's always goto.

>> Are there reasons to prefer one of the two ways (for vs, while) ?


for (; and while (1) are equivalent.

> Do you mean to say whether I'd prefer murder to assasination?


Hopefully it won't come to either.

Lawrence
 
Reply With Quote
 
Alan J. McFarlane
Guest
Posts: n/a
 
      07-05-2005
In article news:(E-Mail Removed), Felix Kater
wrote:
> Lawrence Kirby <(E-Mail Removed)> wrote:

[...]
>>>> Are there reasons to prefer one of the two ways (for vs, while) ?

>>
>> for (; and while (1) are equivalent.

>
> Ok. I'll ask in a compiler's news group to check wheater there are
> differences.
>

Well one difference is that many compilers will warn about "while(1)",
with
e.g.
warning C4127: conditional expression is constant
Whereas, they don't warn about "for (;" (presumably because it's much
more obvious to the reader (and to the writer of the code too)).

I haven't found a 'quiet' equivalent though for "do{ ... }while(0)" (as
recommended in http://www.eskimo.com/~scs/C-faq/q10.4.html).
--
Alan J. McFarlane
http://www.alanjmcf.me.uk/
Please follow-up in the newsgroup for the benefit of all.

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      07-05-2005

On Tue, 05 Jul 2005 13:09:39 +0200, Felix Kater wrote:

> in some cases like dynamic memory allocation I find it convenient to
> (ab-)use for() or while() like this:

<examples snipped>
> Two questions:
>
> Are there generally better ways to do that?


I would not say "better" but (if I understand what you are doing) there
are alternatives such as:

switch (0 /* anything */) {
default:
<stuff with breaks>
}

which reads as if it is doing "default stuff" with no danger of
actually looping.

--
Ben.

 
Reply With Quote
 
Giorgos Keramidas
Guest
Posts: n/a
 
      07-05-2005
Ben Bacarisse <(E-Mail Removed)> writes:
>On Tue, 05 Jul 2005 13:09:39 +0200, Felix Kater wrote:
>> in some cases like dynamic memory allocation I find it convenient to
>> (ab-)use for() or while() like this:

><examples snipped>
>> Two questions:
>>
>> Are there generally better ways to do that?

>
> I would not say "better" but (if I understand what you are doing) there
> are alternatives such as:
>
> switch (0 /* anything */) {
> default:
> <stuff with breaks>
> }
>
> which reads as if it is doing "default stuff" with no danger of
> actually looping.


Why would this be better than using <stuff with breaks> without the
funny switch() construct though?

 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      07-05-2005
On Tue, 05 Jul 2005 17:54:41 +0300, Giorgos Keramidas wrote:

....

>> I would not say "better" but (if I understand what you are doing) there
>> are alternatives such as:
>>
>> switch (0 /* anything */) {
>> default:
>> <stuff with breaks>
>> }
>>
>> which reads as if it is doing "default stuff" with no danger of
>> actually looping.

>
> Why would this be better than using <stuff with breaks> without the
> funny switch() construct though?


Without the switch() construct break wouldn't work. Presumably the idea is
to create a common jump-out point for the enclosed code.

Lawrence


 
Reply With Quote
 
Giorgos Keramidas
Guest
Posts: n/a
 
      07-05-2005
Lawrence Kirby <(E-Mail Removed)> writes:
>On Tue, 05 Jul 2005 17:54:41 +0300, Giorgos Keramidas wrote:
>>> I would not say "better" but (if I understand what you are doing) there
>>> are alternatives such as:
>>>
>>> switch (0 /* anything */) {
>>> default:
>>> <stuff with breaks>
>>> }
>>>
>>> which reads as if it is doing "default stuff" with no danger of
>>> actually looping.

>>
>> Why would this be better than using <stuff with breaks> without the
>> funny switch() construct though?

>
> Without the switch() construct break wouldn't work. Presumably the idea is
> to create a common jump-out point for the enclosed code.


Ah. I still don't see why a simple "goto err;" would ugly, but that's I
guess dangerously close to the place where religious flamefests live.

Anyway, thanks for the clarification

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C (functional programming) VS C++ (object oriented programming) Joe Mayo C Programming 168 10-22-2007 01:00 AM
Can Your Programming Language Do This? Joel on functional programming and briefly on anonymous functions! Casey Hawthorne Python 4 08-04-2006 05:23 AM
Wireless PEAP/MSCHAPV2 client programming question Jim Howard Wireless Networking 6 07-02-2005 11:53 AM
systems programming versus application programming Matt Java 35 07-22-2004 08:10 AM
XSLT programming cameron Firefox 0 01-04-2004 10:51 PM



Advertisments