Thread: using fprintf crashed my program

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    using fprintf crashed my program

    Hi so Im wondering when would there be a case when fprintf would crash a program. I didn't create the program but I'm trying to debug it. I've been able to use fprintf on other files in the program, but for some reason this one file where im just trying to fprintf helloworld crashes the program. And its able to recongize the

    extern FILE* fpvar;

    so its able to see the pointer to the where i originally created fpvar. but once i put a fprintf it crashes the program

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You may be observing the effects of undefined behaviour. By the way, it is usually better to use a debugger to debug, rather than scatter debug print statements all over the place.
    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

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    can you post the code ?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Without any code, I'd say the most likely reason is that fpvar doesn't point to a valid file stream that is open for writing. This could be because
    1. fpvar was never initialized or set to the result of an fopen call
    2. fpvar was opened at some point, but then closed, thus it points to bogus memory that does not describe an open file stream
    3. You have some other memory issue, e.g. buffer overflow, that is scrambling the contents of the fpvar pointer and making it point to memory that doesn't describe an open file stream

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    Well I cant use a debugger that i know of cuz im using jni functions and although this isnt a jni function where im getting the problem the main code is base in java and when i call the jni thats when it crashes.
    I cant really post the code because its companys code im trying to debug but i mean the basic thing is i have

    FILE* fpvar in my main code in C where JAVA does a jni call

    then the rest of my files have
    extern FILE* fpvar;
    and it works for other files but this one where im just diong
    fprintf(fpvar, "helloworld\n");

    it crashes once i open the java program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to execute other program within c program?
    By KoYoungSuk in forum C Programming
    Replies: 7
    Last Post: 06-07-2010, 05:08 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. Auto shutdown of crashed program...
    By phil_drew in forum Windows Programming
    Replies: 1
    Last Post: 04-24-2004, 12:48 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM