Re: strange behaviour

Discussion in 'C Programming' started by vippstar@gmail.com, Jan 13, 2009.

  1. Guest

    On Jan 13, 2:56 pm, tfelb <> wrote:
    > This function creates a puzzle for me which I don't understand.
    >
    > Through the process of s2, the pointer increments beyond the limit of
    > the given pointer array in main.
    > I have another function where I used a pointer array as an argument
    > and a function parameter as a double pointer and this works!?
    >
    > s2 must find the NULL after the second element of that pointer array
    > but it doesn't.
    >
    > Sorry it's maybe a fool question.
    >
    > Thanks for any help!
    >
    > Tom F.
    >
    > /*
    >  *    TEST Function
    >  */
    >
    > void test(char *buffer, char *s1, char **s2)
    > {
    >
    >      while(*s2)
    >          {
    >
    >            s2++; /* Steps beyond into garbage */
    >          }
    >
    > }


    The while loop is equal to s2 = NULL. s2 is local to that function,
    which means the function has no side effects since the only operation
    is the assignment. Since the function doesn't return a value, calling
    test is a nop.

    > /* in main */
    >
    >         char ja[] = "This";


    Your first problem is in this line. Let's assume this line was before
    it:

    int main(void) {

    >         char jo[] = "That";
    >                 char je[] = "Nothing";
    >
    >                 char *text[2];


    The indentation is horrible. Do something about it...
    In the last line, text is an array of 2 elements of type char *.

    >         text[0] = ja;
    >         text[1] = jo;
    >         text[2] = je;


    text is an array of 2 elements. You're attempting to access a 3rd
    element. That's undefined behavior, and anything could happend.

    >         join(erg,"<",text);


    what is erg?

    I'll also pretend after this were the following two lines:

    return 0;
    }
     
    , Jan 13, 2009
    #1
    1. Advertising

  2. Richard Guest

    writes:

    > On Jan 13, 2:56 pm, tfelb <> wrote:
    >> This function creates a puzzle for me which I don't understand.
    >>
    >> Through the process of s2, the pointer increments beyond the limit of
    >> the given pointer array in main.
    >> I have another function where I used a pointer array as an argument
    >> and a function parameter as a double pointer and this works!?
    >>
    >> s2 must find the NULL after the second element of that pointer array
    >> but it doesn't.
    >>
    >> Sorry it's maybe a fool question.
    >>
    >> Thanks for any help!
    >>
    >> Tom F.
    >>
    >> /*
    >>  *    TEST Function
    >>  */
    >>
    >> void test(char *buffer, char *s1, char **s2)
    >> {
    >>
    >>      while(*s2)
    >>          {
    >>
    >>            s2++; /* Steps beyond into garbage */
    >>          }
    >>
    >> }

    >
    > The while loop is equal to s2 = NULL. s2 is local to that function,


    No it isn't.

    It's functionally equivalent to *s2 == NULL. Where "s2" is the
    parameter passed in.

    while(*s2++);

    Would have been nicer regardless and is very much C'ism.

    If s2 was = NULL as you claim there would probably have been a crash.
     
    Richard, Jan 13, 2009
    #2
    1. Advertising

  3. writes:

    > On Jan 13, 2:56 pm, tfelb <> wrote:
    >> This function creates a puzzle for me which I don't understand.
    >>
    >> Through the process of s2, the pointer increments beyond the limit of
    >> the given pointer array in main.
    >> I have another function where I used a pointer array as an argument
    >> and a function parameter as a double pointer and this works!?
    >>
    >> s2 must find the NULL after the second element of that pointer array
    >> but it doesn't.

    <snip>
    >> void test(char *buffer, char *s1, char **s2)
    >> {
    >>      while(*s2)
    >>          {
    >>            s2++; /* Steps beyond into garbage */
    >>          }
    >> }

    >
    > The while loop is equal to s2 = NULL.


    Not always, surely. In the case in question, there are three char *s
    in the array pointer to by s2 and none of the these are == NULL. The
    call therefore invokes undefined behaviour where as a direct setting
    of s2 to NULL does not.

    > s2 is local to that function,
    > which means the function has no side effects since the only operation
    > is the assignment. Since the function doesn't return a value, calling
    > test is a nop.


    A possible seg. fault is hardly a no-op! I suspect the loop is sketch
    and the real body does something else.

    <snip>

    --
    Ben.
     
    Ben Bacarisse, Jan 13, 2009
    #3
  4. tfelb <> writes:
    > On 13 Jan., 14:06, wrote:
    >> On Jan 13, 2:56 pm, tfelb <> wrote:

    [...]
    >> >      while(*s2)
    >> >          {

    >>
    >> >            s2++; /* Steps beyond into garbage */
    >> >          }

    >>
    >> > }

    >>
    >> The while loop is equal to s2 = NULL. s2 is local to that function,
    >> which means the function has no side effects since the only operation
    >> is the assignment. Since the function doesn't return a value, calling
    >> test is a nop.

    [...]
    > while(*s2) is the same as s2 = NULL? Huh? I thought its s2 != NULL ?


    vippstar meant that the loop is equivalent to
    s1 = NULL;
    i.e., to an assignment. He retracted this in a followup.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 13, 2009
    #4
  5. Richard Guest

    Keith Thompson <> writes:

    > tfelb <> writes:
    >> On 13 Jan., 14:06, wrote:
    >>> On Jan 13, 2:56 pm, tfelb <> wrote:

    > [...]
    >>> >      while(*s2)
    >>> >          {
    >>>
    >>> >            s2++; /* Steps beyond into garbage */
    >>> >          }
    >>>
    >>> > }
    >>>
    >>> The while loop is equal to s2 = NULL. s2 is local to that function,
    >>> which means the function has no side effects since the only operation
    >>> is the assignment. Since the function doesn't return a value, calling
    >>> test is a nop.

    > [...]
    >> while(*s2) is the same as s2 = NULL? Huh? I thought its s2 != NULL ?

    >
    > vippstar meant that the loop is equivalent to
    > s1 = NULL;
    > i.e., to an assignment. He retracted this in a followup.


    He did not mean that. He corrected himself later. He does not need you
    second guessing him. Thank you.

    --
    I'm not a person who particularly had heros when growing up.
    - What Dennis Ritchie could potentially say when asked about the hero worship coming from c.l.c.
     
    Richard, Jan 13, 2009
    #5
    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. Antonio

    Strange encoding behaviour

    Antonio, Dec 29, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    441
    Antonio
    Dec 29, 2004
  2. Jan
    Replies:
    2
    Views:
    1,482
    Mike Treseler
    Dec 16, 2004
  3. David Cantin

    Strange behaviour with perl and apache

    David Cantin, Nov 3, 2003, in forum: Perl
    Replies:
    1
    Views:
    466
    Jim Gibson
    Nov 3, 2003
  4. Dennis Johansson
    Replies:
    1
    Views:
    513
    Dennis Johansson
    Aug 21, 2003
  5. Andy Chambers
    Replies:
    1
    Views:
    411
    Daniel Dyer
    May 14, 2007
Loading...

Share This Page