473,508 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

for(;;) or while(1)?

Hi,

For portability, can an issue arise if we use while(1) for an infinite
loop in C? If so, should we then use for(;;)? Thanks,

Rick

Nov 13 '05
52 21853
goose wrote:
.... snip ...
if you need to *handle* the error at some point, then just
add in an integer to keep track of it

int error = 0;
x = y = z = NULL;
if (x = malloc (a)) {
...
if (y = malloc (b)) {
...
if (z = malloc (c)) {
...
} else error = 1;
} else error = 2;
} else error = 3;

if (x) free (x);
if (y) free (y);
if (z) free (z);

switch (error) {
case 1: printf ("no mem for z occurred\n"); break;
case 2: printf ("no mem for y occurred\n"); break;
case 3: printf ("no mem for x occurred\n"); break;
default: printf ("no error\n");
}


Complications not needed. The equivalent is:

....
x = y = z = NULL
if (!(x = malloc(a))) printf("...");
else if (!(y = malloc(b))) printf("...");
else if (!(z = malloc(c))) printf("...");
else {
....
}
free(z); free(y); free(x);

( If you don't like the "!(" replace with "NULL != (" )

and you can always factor "malloc(b)" etc. into a routine:

T *routine(b, others)
{
....
return malloc(b);
}

if you must have independent operations between the mallocs.
routine may even include the printf and be common to all three
cases, for net code reduction.

I think I said all this earlier. Maybe that was another thread.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #51
In article <3F*************@yahoo.com>, CBFalconer wrote:
Complications not needed. The equivalent is:
....
x = y = z = NULL
if (!(x = malloc(a))) printf("...");
else if (!(y = malloc(b))) printf("...");
else if (!(z = malloc(c))) printf("...");
else {
....
}
free(z); free(y); free(x);
and you can always factor "malloc(b)" etc. into a routine:
T *routine(b, others)
{
....
return malloc(b);
}
if you must have independent operations between the mallocs.


This is also an interesting place for a "," (comma) operator.
x = y = z = NULL
if ( !(x = malloc(a))) printf("...");
else if (setup1(x), !(y = malloc(b))) printf("...");
else if (setup2(x,y),!(z = malloc(c))) printf("...");
else {
....
}
where setup1() and setup2() can be either true functions,
or just some simple in-line code.

It doesn't use malloc(), but you can see a similar interaction
of the comma operator and a chain of else-if's in busybox's
date.c:date_conv_ftime() routine. While a bit unusual,
it doesn't take long to grasp, and the code path is greatly
simplified compared to other constructs. This is the author
speaking, so you can take this as a totally unbiased assessment.

- Larry
Nov 13 '05 #52
CBFalconer <cb********@yahoo.com> wrote in message news:<3F*************@yahoo.com>...
goose wrote:
... snip ...

if you need to *handle* the error at some point, then just
add in an integer to keep track of it

int error = 0;
x = y = z = NULL;
if (x = malloc (a)) {
...
if (y = malloc (b)) {
...
if (z = malloc (c)) {
...
} else error = 1;
} else error = 2;
} else error = 3;

if (x) free (x);
if (y) free (y);
if (z) free (z);

switch (error) {
case 1: printf ("no mem for z occurred\n"); break;
case 2: printf ("no mem for y occurred\n"); break;
case 3: printf ("no mem for x occurred\n"); break;
default: printf ("no error\n");
}


Complications not needed. The equivalent is:

....
x = y = z = NULL
if (!(x = malloc(a))) printf("...");
else if (!(y = malloc(b))) printf("...");
else if (!(z = malloc(c))) printf("...");
else {
....
}
free(z); free(y); free(x);


but what if my "error handler" needs to do more than
print a diagnostic ? changing it to the following:

x = y = z = NULL
if (!(x = malloc(a))) {
/* close files */
/* restore signals */
/* print a message */
}
else if (!(y = malloc(b))) {
/* close files */
/* restore signals */
/* print a message */
}
else if (!(z = malloc(c))) {
/* close files */
/* restore signals */
/* print a message */
}

else {
....
}
free(z); free(y); free(x);

and now /that/ does look complicated. with switch
statement, you can at least add tons of error handling
cpode without it getting in the way of readability of
the logic.

( If you don't like the "!(" replace with "NULL != (" )

and you can always factor "malloc(b)" etc. into a routine:

T *routine(b, others)
{
....
return malloc(b);
}
this is best, really.

if you must have independent operations between the mallocs.
routine may even include the printf and be common to all three
cases, for net code reduction.

I think I said all this earlier. Maybe that was another thread.


no, i came into the thread late, i think :-)

goose,
latecomer
Nov 13 '05 #53

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
2666
by: Andrew Koenig | last post by:
PEP 315 suggests that a statement such as do: x = foo() while x != 0: bar(x) be equivalent to while True:
36
2771
by: invni | last post by:
I have a nested while. How do I go from the inner while to the beginning of the outer while? Can this be done without using goto? while_1() { some codes here while_2() { if true go to the...
5
2305
by: Tom Kent | last post by:
I'm running into a weird circumstance with Visual Studio 7.1, where it will not properly indent a while loop nested inside a do-while loop. It compiles and runs fine, but the automatic indenting...
6
71933
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
12
1960
by: Howard | last post by:
Hello everyone (total VB.NET beginner here), I'm reading the "SAMS Teach Yourself VB.NET In 21 Days" book, and came across an exercise that I can't get to work. The exercise asks that you create...
7
3181
by: DaVinci | last post by:
I am writing a pong game.but met some problem. the ball function to control the scrolling ball, void ball(int starty,int startx) { int di ,i; int dj,j; di = 1; dj = 1; i = starty;
5
12219
by: é›· | last post by:
suggest add do while loop in later version
3
1827
by: libsfan01 | last post by:
hi all in my js code i have a while loop contained within a while loop executed on the basis of a conditional if statement, what i want to do is end the entire function on the last execution on...
10
2061
by: wd.jonsson | last post by:
Hmm, my while loop with "or" doesn't seem to work as I want it to... How do I tell the while loop to only accept "Y" or "y" or "N" or "n" input from the str(raw_input)? Thank's in advance! ...
11
2705
by: nembo kid | last post by:
If i>0 the while loop is executed; if i==0 not. Ok, but also if i<0 the while loop is executed. So, which are the rules? Which values must assume the "test condition" to be assumed like true o...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7120
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.