Thread: "The Devil function"

  1. #1
    Banned
    Join Date
    Mar 2008
    Posts
    78

    "The Devil function"

    I have a sentence to be read, that sentece will give me the values of X2 and Y2,
    The function is called readNext(arg), but it crashes when i call it, can't find out why. (no error message on windows).

    Code:
    int x1,x2,y1,y2;
    x1 = x2 = y1 = y2 = 0;
    
    readNext(x2);
    readNext(y2);
    
    ///End of code///
    
    //This function receives them:
    
        void readNext(int *arg1) {
             *arg1 = -1;
             printf("ok");
         while(*arg1 < 0 || isdigit(c)) {
             if(isdigit(c))
              *arg1 = (*arg1 < 0) ? (int)(c-'0') : *arg1 * 10 + (int)(c-'0');
           printf("3o3 arg1 = %i\n",*arg1);
           if(c==';') break;
           c = getchar();
          }
        }
    But in Windows it just crashes, and i can't test it on linux yet.
    Can you find out where's the error?

    Thanks!

  2. #2
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Ok i know the error now,
    its a "Segmentation fault (core dumped)" error as usual in critical moments...

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Run it in a debugger.

  4. #4
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Quote Originally Posted by rags_to_riches View Post
    Run it in a debugger.
    What's the best debugger to do it?

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by Milhas View Post
    I have a sentence to be read, that sentece will give me the values of X2 and Y2,
    The function is called readNext(arg), but it crashes when i call it, can't find out why. (no error message on windows).

    Code:
    int x1,x2,y1,y2;
    x1 = x2 = y1 = y2 = 0;
    
    readNext(x2);
    readNext(y2);
    
    ///End of code///
    
    //This function receives them:
    
        void readNext(int *arg1) {
             *arg1 = -1;
             printf("ok");
         while(*arg1 < 0 || isdigit(c)) {
             if(isdigit(c))
              *arg1 = (*arg1 < 0) ? (int)(c-'0') : *arg1 * 10 + (int)(c-'0');
           printf("3o3 arg1 = %i\n",*arg1);
           if(c==';') break;
           c = getchar();
          }
        }
    But in Windows it just crashes, and i can't test it on linux yet.
    Can you find out where's the error?

    Thanks!
    You are passing an integer to readNext(), but receiving it as a pointer to an integer.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Code:
    int x1,x2,y1,y2;
    ...
    readNext(x2);
    readNext(y2);
    ...
        void readNext(int *arg1) {
    Look at those 4 lines of code. Look at them HARD. And if you still don't see the problem, look at them HARDER!

    -- edit --
    Aw Todd. You ruined my fun!

  7. #7
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Thanks,
    so i'll have to make that function to return a result right? (not being void)

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Quote Originally Posted by Milhas View Post
    What's the best debugger to do it?
    Totally depends on the operating system and IDE. Visual Studio is great for Windows, gdb is great for Linux, Sun Workshop is great for Solaris (or was, it's been a while)...

    You'll find it hard to be a competent developer in compiled languages if you are unable to use a debugger proficiently.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Quote Originally Posted by Milhas View Post
    Thanks,
    so i'll have to make that function to return a result right? (not being void)
    I don't think you're looking at those 4 lines hard enough. Look at them HARDER:
    Code:
    int x1,x2,y1,y2;
    ...
    readNext(x2);
    readNext(y2);
    ...
        void readNext(int *arg1) {

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by arpsmack View Post
    I don't think you're looking at those 4 lines hard enough. Look at them HARDER:
    Too funny! (Or, just read the other posts in this thread. duh.)
    Mainframe assembler programmer by trade. C coder when I can.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Oh right! Look at those 4 lines HARDER... and listen to Todd. Or I suppose you could just pick one of those two.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    What wrong with simply using scanf? It looks like you're reimplementing the wheel!
    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"

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to start paying attention to compiler warnings and errors. Turn the warnings up to max, as well.
    You should never let implicit cast to a pointer from a non-pointer type compile.
    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.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    As Elysia says, turn up the warning level, and don't run the code until it clean compiles.

    It's no guarantee of success, but you certainly improve the odds in the long run.

    Think about the difference between
    readNext(x2);
    readNext(&x2);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Yes, that solves it, &x2 instead of x2.

    This tiny details still make me confusion sometimes..

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check out DEVIL Screensaver System!
    By hartwork in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 08-31-2005, 06:28 PM
  2. "The Messenger Service"
    By sean in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 07-08-2003, 10:18 PM
  3. "The Simpsons"
    By CompWiz84 in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 07-05-2002, 06:27 AM
  4. "The C++ Programming Language"
    By d00b in forum C++ Programming
    Replies: 6
    Last Post: 06-14-2002, 12:17 AM