Why does this work?

This is a discussion on Why does this work? within the C Programming forums, part of the General Programming Boards category; Just out of curiosity this seems to work for me: Code: #include <stdio.h> #define COUNT_TO 52 int main() { int ...

  1. #1
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Why does this work?

    Just out of curiosity this seems to work for me:
    Code:
    #include <stdio.h>
    #define COUNT_TO 52
    
    int main()
    {
        int i, a[10];
        for(i=1; i<COUNT_TO; i++)
        {
            a[i]=i;
            printf("%i\n", a[i]);
        }
        getchar();
    }
    If I change COUNT_TO to 53 then the prog quits before getting input. Both should be errors, so why dosent my compiler complain about it?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Accessing any values you haven't allocated storage for is undefined behavior. This means anything can happen, including appearing to work normally.

    Edit: In general the compiler can't determine whether indexes will be valid at runtime, so it isn't trying, even though in your case it's possible to determine that.
    Last edited by robatino; 07-13-2007 at 06:23 PM.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Oh, I guess the number it could count to before crashing then would be dependant on whats used where in the memory. Thanks.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Depending on what operating system you are using, you can get software which will detect that sort of thing (buffer overruns). For example, I think Valgrind would do that (for Linux only).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I don't think valgrind will be able to detect that according to wikipedia
    In addition to the performance penalty an important limitation of valgrind is its inability to detect errors in stack usage, even trivial ones like the below access of an array outside its bounds. Please note the below code example does not apply/work if compiler optimisation is enabled.
    Code:
    int main(void)
    {
    
        /* Declare and define a variable to always be 0 */
        const int retval = 0;
    
        /* Declare an array with one element */
        int one[1];
    
        one[1] = 1; /* Write outside its bounds */
    
        /* The return value has been changed from 0 to 1 */
        return retval;
    }

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    BoundsChecker will catch this kind of errors...
    PCLint - probably will if the array declaration and out-of bounds access is in the same function...
    (both are commercial products)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Mmm, I thought that Valgrind might only work for dynamically allocated memory. It would be able to detect
    Code:
    int *x = malloc(sizeof(*x));
    x[1] = 1;
    But not for automatic variables.

    I think you can use some functions built in to MSVC to detect buffer overruns. I forget the function name -- do a search for it. I don't think Dev-C++ or any other compilers support this particular function.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    At work we turn on page-heap allocation and get a crash dump file at the exact line the overrun occurred. This only works for heap allocations though, not stack. Works nicely when using the debugger too, of course.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 08:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 11:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 11:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 05:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21