Thread: Why so..

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by joybanerjee39
    it works fine with my pelles c !
    Fascinating. A difference in version, perhaps?

    Quote Originally Posted by CommonTater
    However... the code itself is probably what the picky police here would call "undefined behaviour" since you are messing with the root pointer to a string...
    I think the behaviour is well defined. b is merely a pointer to a char, like any other. Incrementing it then decrementing it is not a problem. Undefined behaviour would come if we attempted to modify what b pointed to, but that does not happen here. Futhermore, there are three separate statements, so there is no problem with doing too much within consecutive sequence points.

    EDIT:
    Quote Originally Posted by juice
    Thanks... Its working now.. I wonder what was wrong.
    Maybe you have discovered an optimisation bug in your version of Pelles C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by joybanerjee39 View Post
    it works fine with my pelles c !
    Project -> Project Options -> Compiler -> Optimizations = "Maximize Speed" (Or anything but "None")

    ... will demonstrate the problem.

    If you check the error messages in the panel at the bottom of the screen you should have gotten...

    Building main.obj.
    D:\Programming\C_Code\Experiments\junk\main.c(13): warning #2238: Array index for 'char [6]' is out-of-bounds.
    Building junk.exe.
    Writing debug information
    Compacting CodeView information
    Done.

  3. #18
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    Fascinating. A difference in version, perhaps?


    I think the behaviour is well defined. b is merely a pointer to a char, like any other. Incrementing it then decrementing it is not a problem. Undefined behaviour would come if we attempted to modify what b pointed to, but that does not happen here. Futhermore, there are three separate statements, so there is no problem with doing too much within consecutive sequence points.

    EDIT:

    Maybe you have discovered an optimisation bug in your version of Pelles C.
    But out...

    Your speculation isn't helping....
    Let me work the problem and sort it out so I reproduce it, then I will make a bug report on the Pelles C forums.

  4. #19
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by CommonTater View Post
    Project -> Project Options -> Compiler -> Optimizations = "Maximize Speed" (Or anything but "None")

    ... will demonstrate the problem.

    If you check the error messages in the panel at the bottom of the screen you should have gotten...
    He's right...

  5. #20
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by CommonTater View Post
    However... the code itself is probably what the picky police here would call "undefined behaviour" since you are messing with the root pointer to a string...
    Aside from the lack of return (which I think is allowed in C99), there's nothing undefined here. Rightly, laserlight's example is the most idiomatic, with it's use of return 0 and const.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #21
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    @joybenergie and juice ... what version of Pelles are you using and is it 32 or 64 bit?

  7. #22
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    whats wrong with "Max. speed"

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by juice
    whats wrong with "Max. speed"
    It could be - as CommonTater noted, I am just speculating - that with optimisations enabled, Pelles C makes some assumptions about a pointer to the first character of a string literal that do not hold if the pointer is modified, under certain circumstances. This would be consistent with that warning #2238 since a negative index is out of bounds. It would also be consistent with the output that you saw since "junk" memory often contains zeroes, so you could be printing a "null character" that is just before the character at index 0 of the string literal.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Quote Originally Posted by CommonTater View Post
    Let me work the problem and sort it out so I reproduce it, then I will make a bug report on the Pelles C forums.
    There's already one, not that you'd ever know anybody who could fix it has seen it.

  10. #25
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    I am just speculating
    Please don't do that.... you aren't helping!

  11. #26
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by adeyblue View Post
    There's already one, not that you'd ever know anybody who could fix it has seen it.
    Oh for crying out loud...

    Exactly how STUPID are you people?

    This is not an opportunity to trash on Pelles C... so kindly go slither back under whatever rock you call home.

  12. #27
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by juice View Post
    whats wrong with "Max. speed"
    You appear to have stumbled into an optimization bug in Pelles C.
    I am trying to write code to fully demonstrate the problem and will
    forward a report directly to Pelle about it.

    That is, if the other fools will PLEASE stop using this opportunity to behave stupidly.

    (You and joybenergie need to make sure you can accept PMs... we'll finish this there.)

  13. #28
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by CommonTater View Post
    However... the code itself is probably what the picky police here would call "undefined behaviour" since you are messing with the root pointer to a string...
    I don't think the standard has any conception of "root pointer", so this should not be UB. Trying to move this:

    Code:
    char b[]="asdf";
    would an error tho, since b is not a true lvalue.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    Please don't do that.... you aren't helping!
    If by speculating so that we are aware of the possibility that we are looking at a certain variety of compiler bug is not helping, then I am afraid that none of us are helping

    Quote Originally Posted by CommonTater
    This is not an opportunity to trash on Pelles C
    Err... compilers are programs written by humans. Humans err, thus bugs are to be expected, so no, if it is genuine, and thus far it looks like it, identifying a bug is not tantamount to trashing Pelles C.

    Yes, I know that you are a fan of Pelles C, but notice that I am taking this cautiously too because I expect bugs in a reputable compiler to be rare, hence I am calling it speculation whereas I could take the current evidence at face value and declare with certainty that there is a compiler bug.

    EDIT:
    Quote Originally Posted by CommonTater
    You appear to have stumbled into an optimization bug in Pelles C.
    I am trying to write code to fully demonstrate the problem and will
    forward a report directly to Pelle about it.
    You are speculating too! Refer to my code in post #9 for a program that will "fully demonstrate the problem", if juice's observation is correct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    IMO, Saying that the compiler makes incorrect assumptions about pointers to the beginning of a string is speculation. Saying that the compiler has a bug is not. Neither are trashing the compiler.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed