Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > [OT] Some bad code I wrote the other day

Reply
Thread Tools

[OT] Some bad code I wrote the other day

 
 
Old Wolf
Guest
Posts: n/a
 
      10-11-2006
char *out = dest;
char const *in = src;

while ( out != dest + dest_len && in != src + src_len )
*dest++ = *src++;

Also on the same day: icc_check is a function that returns a
status value, and also 'returns' the previous status value by writing
to the pointer you pass to it.

if ( icc_check(&state) != state )
{
/* note that state has changed */
}

 
Reply With Quote
 
 
 
 
Cong Wang
Guest
Posts: n/a
 
      10-11-2006


On Oct 11, 1:18 pm, "Old Wolf" <oldw...@inspire.net.nz> wrote:
> char *out = dest;
> char const *in = src;
>
> while ( out != dest + dest_len && in != src + src_len )
> *dest++ = *src++;
>
> Also on the same day: icc_check is a function that returns a
> status value, and also 'returns' the previous status value by writing
> to the pointer you pass to it.
>
> if ( icc_check(&state) != state )
> {
> /* note that state has changed */
> }


But what is your question?

 
Reply With Quote
 
 
 
 
Mudit Khetan
Guest
Posts: n/a
 
      10-11-2006

Old Wolf wrote:
> char *out = dest;
> char const *in = src;
>
> while ( out != dest + dest_len && in != src + src_len )
> *dest++ = *src++;
>
> Also on the same day: icc_check is a function that returns a
> status value, and also 'returns' the previous status value by writing
> to the pointer you pass to it.
>
> if ( icc_check(&state) != state )
> {
> /* note that state has changed */
> }


1) The loop given above will never come to an end, "infinite loop".
because u do not change (you can't) the in pointer.
2) The condition will always be true, as function calls are resolved
first then the result (return) of this call will be compared against
the "state" var that has been changed by the function.

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      10-11-2006
Mudit Khetan wrote:

> Old Wolf wrote:
>
>>char *out = dest;
>> char const *in = src;
>>
>> while ( out != dest + dest_len && in != src + src_len )
>> *dest++ = *src++;
>>
>>Also on the same day: icc_check is a function that returns a
>>status value, and also 'returns' the previous status value by writing
>>to the pointer you pass to it.
>>
>> if ( icc_check(&state) != state )
>> {
>> /* note that state has changed */
>> }

>
> 1) The loop given above will never come to an end, "infinite loop".
> because u do not change (you can't) the in pointer.


The `in' pointer can be changed (although the code shown
does not change it). The `const' means that `in' cannot be
used to change the characters that it points at.

And a nit-pick: The loop does indeed end if either dest_len
or src_len is zero.

> 2) The condition will always be true, as function calls are resolved
> first then the result (return) of this call will be compared against
> the "state" var that has been changed by the function.


C makes no promise that function calls are "resolved first"
in an expression. The outcome of the test is implementation-
dependent.

--
Eric Sosman
lid
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      10-11-2006
Old Wolf <> wrote:

> char *out = dest;
> char const *in = src;


> while ( out != dest + dest_len && in != src + src_len )
> *dest++ = *src++;


> if ( icc_check(&state) != state )
> {
> /* note that state has changed */
> }


Perhaps you can be switched over to HR so you can write your company's
interview questions

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      10-11-2006
Mudit Khetan wrote:
>
> Old Wolf wrote:

[...]
> > Also on the same day: icc_check is a function that returns a
> > status value, and also 'returns' the previous status value by writing
> > to the pointer you pass to it.
> >
> > if ( icc_check(&state) != state )
> > {
> > /* note that state has changed */
> > }

[...]
> 2) The condition will always be true, as function calls are resolved
> first then the result (return) of this call will be compared against
> the "state" var that has been changed by the function.


Untrue. There is no guarantee that the value of the right-hand side
of the "!=" condition isn't evaluated first, as in this pseudo-asm
code:

load R10,state
push &state
call icc_check ; (return value is stored in R0)
compare R0,R10

(ObNitPickerAvoidance: the compiler knows enough to preserve any
registers R10-R31 that it uses, so R10 won't change even if it is
used by icc_check.)

As I recall, the function call is a sequence point, but there is
no guarantee as to the order in which the statement is evaluated.

However, I'm not sure if this example invokes UB or is simply
"implementation defined".

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <private.php?do=newpm&u=>


 
Reply With Quote
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      10-11-2006
Kenneth Brody wrote:
> > Old Wolf wrote:
> > > if ( icc_check(&state) != state )
> > > {
> > > /* note that state has changed */
> > > }

> [...]
> As I recall, the function call is a sequence point, but there is
> no guarantee as to the order in which the statement is evaluated.
>
> However, I'm not sure if this example invokes UB or is simply
> "implementation defined".


Neither, actually. It's unspecified, which means the implementation
need not document anything, and need not even be consistent if the code
is executed multiple times, but the result of the function call must be
compared with either the value of state before the function call, or
the value afterwards.

 
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
Help me, a friend of mine wrote a program in C# and I wrote the sameprogram in C..His is faster than mine on the same machine...How Come ? c C Programming 43 12-15-2007 01:01 PM
I wrote a C++ code generator in Python, would anyone please help me to review the code? :) Kevin Wan Python 5 01-17-2007 09:33 AM
About a class I wrote to filter bad html input Owen Wong ASP .Net 1 09-26-2006 07:44 AM
ActiveX apologetic Larry Seltzer... "Sun paid for malicious ActiveX code, and Firefox is bad, bad bad baad. please use ActiveX, it's secure and nice!" (ok, the last part is irony on my part) fernando.cassia@gmail.com Java 0 04-16-2005 10:05 PM



Advertisments