what is wrong in the code?

Discussion in 'C Programming' started by Sean Zhang, Apr 20, 2006.

  1. Sean Zhang

    Sean Zhang Guest

    The following is the code:
    struct S {
    int i;
    int * p;
    };
    void main()
    {
    Struct S s;
    int * p = &s.i;
    p[0] = 4;
    p[1] = 3;
    s.p = p;
    s.p[1] = 1;
    s.p[0] = 2;
    }
    I don't why cause the problem occurred in the last sentence?
    Thanks a lot!
     
    Sean Zhang, Apr 20, 2006
    #1
    1. Advertising

  2. Sean Zhang

    Marc Boyer Guest

    Le 20-04-2006, Sean Zhang <> a écrit :
    > The following is the code:
    > struct S {
    > int i;
    > int * p;
    > };
    > void main()
    > {
    > Struct S s;
    > int * p = &s.i;
    > p[0] = 4;


    OK: p[0] == *p == s.i
    You code is equivalent to
    s.i= 4

    > p[1] = 3;


    Undefined behavior. p[1] does not exists !

    > s.p = p;


    s.p = &s.i;

    > s.p[1] = 1;


    UB

    > s.p[0] = 2;


    OK
    > }


    Marc Boyer
     
    Marc Boyer, Apr 20, 2006
    #2
    1. Advertising

  3. Sean Zhang

    Sean Zhang Guest

    Acctually, The error is just happedn in the last sentence! No undefined
    behavior error!

    Sean Zhang
    Marc Boyer 写é“:

    > Le 20-04-2006, Sean Zhang <> a écrit :
    > > The following is the code:
    > > struct S {
    > > int i;
    > > int * p;
    > > };
    > > void main()
    > > {
    > > Struct S s;
    > > int * p = &s.i;
    > > p[0] = 4;

    >
    > OK: p[0] == *p == s.i
    > You code is equivalent to
    > s.i= 4
    >
    > > p[1] = 3;

    >
    > Undefined behavior. p[1] does not exists !
    >
    > > s.p = p;

    >
    > s.p = &s.i;
    >
    > > s.p[1] = 1;

    >
    > UB
    >
    > > s.p[0] = 2;

    >
    > OK
    > > }

    >
    > Marc Boyer
     
    Sean Zhang, Apr 20, 2006
    #3
  4. Sean Zhang

    Marc Boyer Guest

    Le 20-04-2006, Sean Zhang <> a écrit :
    > Acctually, The error is just happedn in the last sentence! No undefined
    > behavior error!


    Newby on Usenet ? Please, answer after the message your are replying
    to.

    On your problem, did you know what means 'undefined behavior' ?
    It means that 'from now, everything can happen', and yes often,
    whith UB, the program 'seems to work' some time, and then
    crash after the problem itself.

    With some experience on C and on how typical compiler
    works, one can often guess why it crashes at a given line and
    not at the UB itself.
    But to get a correct behavior, avoid UB.

    Marc Boyer
     
    Marc Boyer, Apr 20, 2006
    #4
  5. Sean Zhang said:

    > The following is the code:
    > struct S {
    > int i;
    > int * p;
    > };
    > void main()


    Here is your first problem. The function called 'main' is called by the
    system, and so the interface specification is carefully defined by the
    language. It isn't your decision. There are two core forms that are
    portable across all hosted implementations, and the one you need is:

    int main(void)


    > {
    > Struct S s;


    This is okay. It creates storage for a struct S.

    > int * p = &s.i;


    This is fine. It takes and stores the address of s.i.

    > p[0] = 4;


    This is fine too. It stores the value 4 in s.i.

    > p[1] = 3;


    This is not fine. You are trying to write the value 3 into an array element
    that does not exist. The result is undefined behaviour.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Apr 20, 2006
    #5
  6. Sean Zhang said:

    > Acctually, The error is just happedn in the last sentence! No undefined
    > behavior


    Wrong. The behaviour is undefined from the very first call to main.

    > error!


    Yes, error. Just because you don't get a message saying it's wrong, that
    doesn't mean it's right.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Apr 20, 2006
    #6
  7. Sean Zhang

    Sean Zhang Guest

    Yeah, you are right in the syntax perspective! But I want to know why
    the last sentence produce the runtime error when I execute the binary
    code after I compile it on VC6!

    Sean
    Richard Heathfield 写é“:

    > Sean Zhang said:
    >
    > > Acctually, The error is just happedn in the last sentence! No undefined
    > > behavior

    >
    > Wrong. The behaviour is undefined from the very first call to main.
    >
    > > error!

    >
    > Yes, error. Just because you don't get a message saying it's wrong, that
    > doesn't mean it's right.
    >
    > --
    > Richard Heathfield
    > "Usenet is a strange place" - dmr 29/7/1999
    > http://www.cpax.org.uk
    > email: rjh at above domain (but drop the www, obviously)
     
    Sean Zhang, Apr 20, 2006
    #7
  8. Sean Zhang said:

    > Yeah, you are right in the syntax perspective!


    I didn't say anything about syntax. I said a couple of things about
    undefined behaviour.

    > But I want to know why
    > the last sentence produce the runtime error when I execute the binary
    > code after I compile it on VC6!


    It is because your program has at least two instances of undefined
    behaviour. One is your misdefinition of main's interface, and the other is
    that you are writing to an object that doesn't exist.

    Either of these can be sufficient to cause a runtime error.

    The fix is to remove instances of undefined behaviour from your program.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Apr 20, 2006
    #8
  9. Sean Zhang

    Ian Collins Guest

    Sean Zhang wrote:
    > Yeah, you are right in the syntax perspective! But I want to know why
    > the last sentence produce the runtime error when I execute the binary
    > code after I compile it on VC6!
    >

    Please don't top post!

    Who knows? You have indirectly set s.p to 1, so all bets are off.

    --
    Ian Collins.
     
    Ian Collins, Apr 20, 2006
    #9
  10. Sean Zhang

    Sean Zhang Guest

    Thanks to Marc Boyer and you!
    I have tested it in this way:
    If I reverse the last two sentence, then compile and run it, it is
    normal, no runtime error under windows o.s. So, It is weird to me!

    Sean
    Richard Heathfield 写é“:

    > Sean Zhang said:
    >
    > > Yeah, you are right in the syntax perspective!

    >
    > I didn't say anything about syntax. I said a couple of things about
    > undefined behaviour.
    >
    > > But I want to know why
    > > the last sentence produce the runtime error when I execute the binary
    > > code after I compile it on VC6!

    >
    > It is because your program has at least two instances of undefined
    > behaviour. One is your misdefinition of main's interface, and the other is
    > that you are writing to an object that doesn't exist.
    >
    > Either of these can be sufficient to cause a runtime error.
    >
    > The fix is to remove instances of undefined behaviour from your program.
    >
    > --
    > Richard Heathfield
    > "Usenet is a strange place" - dmr 29/7/1999
    > http://www.cpax.org.uk
    > email: rjh at above domain (but drop the www, obviously)
     
    Sean Zhang, Apr 20, 2006
    #10
  11. Sean Zhang

    Ian Collins Guest

    Sean Zhang wrote:
    > Thanks to Marc Boyer and you!
    > I have tested it in this way:
    > If I reverse the last two sentence, then compile and run it, it is
    > normal, no runtime error under windows o.s. So, It is weird to me!
    >

    Please don't top post!

    If you play with fire, or undefined behaviour, you will get burnt.

    --
    Ian Collins.
     
    Ian Collins, Apr 20, 2006
    #11
  12. Sean Zhang said:

    > Thanks to Marc Boyer and you!
    > I have tested it in this way:
    > If I reverse the last two sentence, then compile and run it, it is
    > normal, no runtime error under windows o.s.


    It's still wrong, though.

    > So, It is weird to me!


    Making random changes to the code will not help you to understand it.

    There are at least two problems with your code. I pointed these out to you
    earlier on. When they are both fixed, your program will have two fewer
    bugs, and therefore its behaviour will be easier for you to understand.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Apr 20, 2006
    #12
  13. Richard Heathfield <> wrote:
    >Sean Zhang said:
    >> The following is the code:

    <...>

    In addition to what others pointed out,

    >> Struct S s;


    'Struct' should be 'struct'
     
    Roberto Waltman, Apr 20, 2006
    #13
  14. On 20 Apr 2006 02:41:56 -0700, in comp.lang.c , "Sean Zhang"
    <> wrote:

    >I have tested it in this way:
    >If I reverse the last two sentence, then compile and run it, it is
    >normal, no runtime error under windows o.s. So, It is weird to me!


    It only worked by accident. Its still broken code.

    I saw a good analogy for this recently:

    Say you get into your car and put on a blindfold. You start driving
    blind. You don't necessarily crash into anything immediately. You may
    never crash, if you're lucky. You still made a serious error right at
    the start. The fact that you didn't have a crash doesn't change the
    fact that you did something bad....

    Mark McIntyre
    --
    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Apr 20, 2006
    #14
  15. Sean Zhang

    balasam Guest

    Sean Zhang wrote:

    > The following is the code:
    > struct S {
    > int i;
    > int * p;
    > };
    > void main()
    > {
    > Struct S s;
    > int * p = &s.i;
    > p[0] = 4;
    > p[1] = 3;
    > s.p = p;
    > s.p[1] = 1;
    > s.p[0] = 2;
    > }
    > I don't why cause the problem occurred in the last sentence?
    > Thanks a lot!


    Dear friend,
    Your are not allocating memory for the "s.p" .Only you assign
    the address of p.One thing you note that you are accessing the memory
    in an illegal way.
     
    balasam, Apr 20, 2006
    #15
  16. balasam said:

    >
    > Sean Zhang wrote:
    >
    >> The following is the code:
    >> struct S {
    >> int i;
    >> int * p;
    >> };
    >> void main()
    >> {
    >> Struct S s;
    >> int * p = &s.i;
    >> p[0] = 4;
    >> p[1] = 3;
    >> s.p = p;
    >> s.p[1] = 1;
    >> s.p[0] = 2;
    >> }
    >> I don't why cause the problem occurred in the last sentence?
    >> Thanks a lot!

    >
    > Dear friend,
    > Your are not allocating memory for the "s.p" .Only you assign
    > the address of p.


    He gives p a value, anyway.

    Not allocating the memory is not (exactly) the problem. He does, after all,
    point s.p at a legal object (s.i, in other words).

    > One thing you note that you are accessing the memory
    > in an illegal way.


    That is certainly true.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Apr 20, 2006
    #16
  17. Mark McIntyre <> writes:
    > On 20 Apr 2006 02:41:56 -0700, in comp.lang.c , "Sean Zhang"
    > <> wrote:
    >
    >>I have tested it in this way:
    >>If I reverse the last two sentence, then compile and run it, it is
    >>normal, no runtime error under windows o.s. So, It is weird to me!

    >
    > It only worked by accident. Its still broken code.
    >
    > I saw a good analogy for this recently:
    >
    > Say you get into your car and put on a blindfold. You start driving
    > blind. You don't necessarily crash into anything immediately. You may
    > never crash, if you're lucky. You still made a serious error right at
    > the start. The fact that you didn't have a crash doesn't change the
    > fact that you did something bad....


    Suppose the first time you try this, you run into something. You
    stop, take off the blindfold, reverse it, and put it back on again.
    This time you don't run into anything, and you arrive at your
    destination. Reversing the blindfold didn't correct the problem; you
    just got lucky.

    Reversing the last two statements of your program (note: C has
    declarations and statements, not "sentences") didn't fix anything, it
    just randomly changed the (already wrong) behavior of your program.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Apr 20, 2006
    #17
  18. "Sean Zhang" <> writes:
    > The following is the code:
    > struct S {
    > int i;
    > int * p;
    > };
    > void main()
    > {
    > Struct S s;
    > int * p = &s.i;
    > p[0] = 4;
    > p[1] = 3;
    > s.p = p;
    > s.p[1] = 1;
    > s.p[0] = 2;
    > }
    > I don't why cause the problem occurred in the last sentence?


    No, that isn't the actual code. If it were, it wouldn't have
    compiled, since you misspelled "struct" as "Struct". (Thanks to
    Roberto Waltman for noticing this; I completely missed it myself.)

    If you want to ask us about your code, you need to show us the *exact*
    code. Don't re-type it; copy-and-paste it. Don't make us waste our
    time guessing which errors are in your original code and which you
    introduced by posting an approximation to it.

    And if you're having a problem, tell us what the problem is. From
    your description, we can't even tell whether the problem occurs at
    compile time or run time. Show us the error message (and again,
    copy-and-paste the *exact* message, don't re-type it).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Apr 20, 2006
    #18
  19. Groovy hepcat Sean Zhang was jivin' on 20 Apr 2006 02:13:06 -0700 in
    comp.lang.c.
    what is wrong in the code?'s a cool scene! Dig it!

    >The following is the code:
    >struct S {
    > int i;
    > int * p;
    >};
    >void main()


    main() should return int, not void.

    >{
    > Struct S s;


    There's no such thing as Struct. Remember, C is a case sensitive
    language. "Struct" (with a capitol "S") is not the same as "struct".

    > int * p = &s.i;


    Fine.

    > p[0] = 4;


    Fine.

    > p[1] = 3;


    BZZZZT! Undefined behaviour. p[1] doesn't exist.

    > s.p = p;


    Fine.

    > s.p[1] = 1;


    BZZZZT! Undefined behaviour. s.p[1] doesn't exist.

    > s.p[0] = 2;


    Fine.
    You really need to return a value (after changing main()'s return
    type). You can portably return 0, EXIT_SUCCESS or EXIT_FAILURE (the
    latter two being macros defined in stdlib.h).

    >}
    >I don't why cause the problem occurred in the last sentence?


    I realise English is probably not your first language, but I don't
    know what that's supposed to mean. There are problems throughout this
    code. This could not even have compiled. It is, no doubt, not the code
    you actually tried to compile. Don't retype code here. Copy and paste,
    so we are sure to see the real code.

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
     
    Peter Shaggy Haywood, Apr 22, 2006
    #19
    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. walala
    Replies:
    3
    Views:
    2,272
    Ralf Hildebrandt
    Sep 10, 2003
  2. willem oosthuizen

    What is wrong with the following code?

    willem oosthuizen, Oct 10, 2003, in forum: VHDL
    Replies:
    9
    Views:
    1,361
  3. Matthew
    Replies:
    7
    Views:
    878
    Priscilla Walmsley
    Jan 7, 2005
  4. David. E. Goble
    Replies:
    9
    Views:
    510
    David. E. Goble
    Feb 2, 2005
  5. kiran
    Replies:
    12
    Views:
    1,227
    Scott Sauyet
    Dec 7, 2011
Loading...

Share This Page