Thread: Most Common problems in C

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Bayint Naung View Post
    @ MK27, I forgot that! How to avoid Off by One error?! Count carefully?
    I guess. I would say that for allocation, too much is better than too little, but that is not true for iterative reads, so not very good advice. Seems to me that once I read the analogy (you need 11, not 10 fenceposts for a 100' fence with posts every 10 feet) it was easier to conceive of and notice.
    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

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by MK27 View Post
    I guess. I would say that for allocation, too much is better than too little, but that is not true for iterative reads, so not very good advice. Seems to me that once I read the analogy (you need 11, not 10 fenceposts for a 100' fence with posts every 10 feet) it was easier to conceive of and notice.
    I find your analogy off for your own example. While this can be, indeed, the cause for off-by-one errors, it's not for the copying of strings. That is not caused by forgetting to add one after subtraction, but rather by forgetting to count \0 as a character that has to be stored as well. Still an off by one error, but different analogy.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by EVOEx View Post
    I find your analogy off for your own example.
    Technically, I wasn't using that analogy with that example, they were in two separate posts.

    You're right, tho, understanding the fencepost error may or may not help with the strlen one in the strict sense of the fencepost analogy being about inclusion in a series (0-10 is eleven numbers).

    On the other hand, it is still conceptually similar -- when you get to the tenth pole, you'll notice there's still a unit of fence left unaccounted for, altho in this case it has to do with a C specific issue (what strlen counts and what a C string is) as opposed to a plain old arithmetic error.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to handle multiple cases which use common code?
    By tmaxx in forum C Programming
    Replies: 3
    Last Post: 10-03-2008, 07:42 AM
  2. common functions
    By FoodDude in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2005, 02:13 PM
  3. Common Problems
    By WolfPack in forum C Programming
    Replies: 4
    Last Post: 01-28-2003, 06:38 PM
  4. Lowest Common Factor
    By PJYelton in forum C++ Programming
    Replies: 9
    Last Post: 12-23-2002, 09:30 AM
  5. Coding Problems
    By RpiMatty in forum C++ Programming
    Replies: 12
    Last Post: 01-06-2002, 02:47 AM