[OT] Some bad code I wrote the other day

Discussion in 'C Programming' started by Old Wolf, Oct 11, 2006.

  1. Old Wolf

    Old Wolf Guest

    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 */
    }
     
    Old Wolf, Oct 11, 2006
    #1
    1. Advertising

  2. Old Wolf

    Cong Wang Guest

    Re: Some bad code I wrote the other day

    On Oct 11, 1:18 pm, "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 */
    > }


    But what is your question?
     
    Cong Wang, Oct 11, 2006
    #2
    1. Advertising

  3. Old Wolf

    Mudit Khetan Guest

    Re: Some bad code I wrote the other day

    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.
     
    Mudit Khetan, Oct 11, 2006
    #3
  4. Old Wolf

    Eric Sosman Guest

    Re: Some bad code I wrote the other day

    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
     
    Eric Sosman, Oct 11, 2006
    #4
  5. 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.
     
    Christopher Benson-Manica, Oct 11, 2006
    #5
  6. Re: Some bad code I wrote the other day

    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: <mailto:>
     
    Kenneth Brody, Oct 11, 2006
    #6
  7. Re: Some bad code I wrote the other day

    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.
     
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Oct 11, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Owen Wong
    Replies:
    1
    Views:
    357
    Owen Wong
    Sep 26, 2006
  2. Kevin Wan
    Replies:
    5
    Views:
    760
    Kevin Wan
    Jan 17, 2007
  3. c
    Replies:
    43
    Views:
    1,579
    Richard
    Dec 15, 2007
  4. Andy
    Replies:
    1
    Views:
    758
  5. rantingrick
    Replies:
    44
    Views:
    1,298
    Peter Pearson
    Jul 13, 2010
Loading...

Share This Page