Re: newbie: why does this work

Discussion in 'C Programming' started by Brett Frankenberger, Aug 31, 2003.

  1. In article <bird6o$6ee$>, Jay <> wrote:
    >
    >should i not get an error because string is
    >only supposed to hold 3 characters?
    >
    >char string[3];
    >strcpy(string,"hellotesting");


    Writing beyond the end of an array is undefined behavior. Under the
    standard, the implementation is free to do anyting it wants, so you
    can't really say that your program should do anything at all.

    Few, if any, implementations will give an error message, because C
    implementations traditionally don't do array bounds checking. (It's
    very difficult to do it while still allowing all the allowable things
    that C programmers do with pointers, and many C programmers would
    philosophically be of the opinion that it's not the compilers job to
    add runtime overhead to check for programmer errors.)

    In this case, you got lucky. You wrote beyond the end of an array, and
    it wasn't into an area that caused a fault, and there wasn't anything
    else of great importance there.

    -- Brett
     
    Brett Frankenberger, Aug 31, 2003
    #1
    1. Advertising

  2. Brett Frankenberger

    Randy Howard Guest

    In article <birc9q$58e$>, says...
    > In this case, you got lucky.


    Possibly. We don't know for sure.

    > You wrote beyond the end of an array, and it wasn't into an area
    > that caused a fault, and there wasn't anything else of great
    > importance there.


    No obvious fault occurred. The system could have easily deleted
    some critical file on the hard drive that won't be noticed for
    another month, reported his illegal alien nanny to the INS,
    transferred 1,000.00 out of his checking account to the Bill Gates
    Foundation, or any number of interesting side-effects without presenting
    an error message on his console.

    :)

    --
    Randy Howard _o
    2reply remove FOOBAR \<,
    ______________________()/ ()______________________________________________
    SCO Spam-magnet:
     
    Randy Howard, Aug 31, 2003
    #2
    1. Advertising

  3. Brett Frankenberger

    Malcolm Guest

    "Randy Howard" <> wrote in message
    >
    > > In this case, you got lucky.

    >
    > Possibly. We don't know for sure.
    >

    Getting lucky with UB means that the system reports "segmentation fault" -
    i.e. it produces output that is as correct as it can be given that the
    program is incorrect.
    Supressing an error to produce a seemingly correct program isn't lucky,
    because it means that the bug is likely to ship, or start causing problems
    later on in development, where it is far more expensive to fix.
     
    Malcolm, Aug 31, 2003
    #3
    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. jblazi
    Replies:
    5
    Views:
    435
    jblazi
    Aug 16, 2004
  2. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    461
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    907
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,984
    Smokey Grindel
    Dec 2, 2006
  5. Tarun
    Replies:
    5
    Views:
    394
    Tarun
    Jul 14, 2005
Loading...

Share This Page