Thread: c programs

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    1

    Unhappy c programs

    why some c programs are not run even if there is no error in compile time??????????

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    why some c programs are not run even if there is no error in compile time??????????
    Such as ?

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Usually because you made a run-time error and not a compile-time error.
    For more information, we need to see code!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by richi View Post
    why some c programs are not run even if there is no error in compile time??????????
    You mean like this one:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
    	char *overflowme;
    	strcpy(overflowme,"this will overwrite some memory");
    	printf("%s", overflowme);
    	return 0;
    }
    Because the compiler does not check stuff like this. Perhaps it should, and this is a performance trade-off in the compiler.

    On the other hand, programming need not be quite so easy as throwing darts blindfolded (which really is easy if you don't care what you hit). You have to know what you are doing. To know you have to learn, partially thru a process of trial and error. It's not nearly as painful as learning to ride a bike, and you already did that (hopefully).

    Lesson: Don't expect the compiler to fix everything for you, and be happy when it does.
    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

  5. #5
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    A compiler doesn't check if you're doing meaningful things. It only checks if you're doing correct things.
    I don't think many compilers will complain at this:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        printf("The mystery is solved: *NULL is %d\n", *(int *)NULL);
        return 0;
    }
    Even though it is clearly undefined behaviour.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Compilers can't check logic errors or care much whether there are even any output statements. That should be obvious.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    They can, and some do.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    The compiler just checks to make sure you use correct syntax, it doesn't check to make sure your code is coherent.

    The warbling of the instantiated was blue.

    Perfectly good syntax, with no meaning whatsoever.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, some do. 99% of the compilers do not, but some, like MSVC can check for problems like this. It's not good to dismiss out of hand. But it's certainly not good to always rely on such a feature.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    And if you do this:
    Code:
    int mostlypointless (int x) {
         int a = 12, i;
         for (i=0; i<10; i++) a = a*i+a;
         return x+1;
    }
    GCC -O3 will just ignore the statements in red as if they didn't exist, because they are meaningless in the context of the program.

    The only way to notice this (I guess) is if you use massive meaningless loops for profiling. Compiled gcc -O3, they won't happen.
    Last edited by MK27; 07-25-2009 at 04:34 AM.
    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

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Newer compilers for other languages are 'attempting' to do this and it comes off to me as 100% annoying. Maybe it's because I don't like my hand being held while I code.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's extremely helpful to offer such code analysis, because it can find problems in your code.
    Naturally, it is also slower, so there should always be an option to turn it off. Otherwise the compiler is flawed.
    Or perhaps you don't like the compiler removing certain code in optimizations?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    >> They can, and some do.
    To a very limited extent, but for the most part they can't.

    When it comes to incorrect manipulation of certain things, 'const' does a good job for protecting against this - if for some reason the programmer doesn't want to use it, that's his problem.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Const doesn't help very much at all. It's mostly for for convenience and efficiency.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by Elysia View Post
    It's extremely helpful to offer such code analysis, because it can find problems in your code.
    Naturally, it is also slower, so there should always be an option to turn it off. Otherwise the compiler is flawed.
    Hmm... I disagree. That's definitely not the task of a compiler. Abachler said it best.

    That type of code analysis can and should be done by specialized tools only. MSVC offers those tools in its Team System releases. Tools like Code Metrics, C/C++ Code Analysis Tools, Application Verifier, Line Level Sampling and even Code Profiler (and there's even more).

    Note that by "compiler", I mean the actual task of code compilation. In this context I don't agree such tasks should be implemented. In fact I suspect most of them couldn't, since better diagnostics can only be achieved after compilation and during program execution with access to debugging symbols, which is what most of the tools above do. On the other hand, if we consider the tasks of a compiler (lexical analysis, pre-processing, parsing and code generation with/without optimization) we can see there is really no place for such analysis as the excellent example provided by Mk27 demonstrates.

    Again, go back to abachler statement. There's an underlying truth to it that basically says it's not the task of the compiler to make sure your code works as you want it to.
    Last edited by Mario F.; 07-25-2009 at 01:16 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recommend upgrade path for C programs
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-22-2007, 07:32 AM
  2. Way to get the current open programs displayed?
    By CPPguy1111 in forum C++ Programming
    Replies: 6
    Last Post: 06-22-2005, 12:24 AM
  3. Communication between programs?
    By johny145 in forum Windows Programming
    Replies: 3
    Last Post: 06-01-2005, 10:14 PM
  4. POSIX/DOS programs?
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 02-28-2003, 05:42 AM
  5. executing c++ programs on the web
    By gulti01 in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2002, 03:12 AM