removing a printf statement from my program causes it to crash!?

Discussion in 'C Programming' started by Harman Dhaliwal, May 25, 2006.

  1. Hi,
    I am programming an in memory representation of a database and am at
    the tail end, hopefully, of implementation. But the vexing point is
    that my program runs fine with all my diagnostic printf statements, but
    as soon as I remove the printf statements the program crashes with a
    pointer error (usually tried reading x00000000 error).

    I have tried everything from flushing the buffer, tracking done the
    routine, etc... can anyone offer me some suggestions to something I
    might try? Has anyone had a similiar error? What is going on with
    printf that would cause the removal of it to prvent program execution?
    Does printf empty a buffer that is overflowing? Please excuse my
    lamentations but I've hit my head against this for almost 2 days.
    Thank you in advance for any response.
     
    Harman Dhaliwal, May 25, 2006
    #1
    1. Advertising

  2. Harman Dhaliwal

    Snis Pilbor Guest

    Hi Harman,

    I'm not the world's best expert, but in my experience whenever you
    have anomalies like this it suggests that dynamically allocated memory
    is corrupt. Look closely to make sure everything that needs to be
    allocated is allocated before use and that once anything is free'd, it
    is never "used" again.

    Harman Dhaliwal wrote:
    > Hi,
    > I am programming an in memory representation of a database and am at
    > the tail end, hopefully, of implementation. But the vexing point is
    > that my program runs fine with all my diagnostic printf statements, but
    > as soon as I remove the printf statements the program crashes with a
    > pointer error (usually tried reading x00000000 error).
    >
    > I have tried everything from flushing the buffer, tracking done the
    > routine, etc... can anyone offer me some suggestions to something I
    > might try? Has anyone had a similiar error? What is going on with
    > printf that would cause the removal of it to prvent program execution?
    > Does printf empty a buffer that is overflowing? Please excuse my
    > lamentations but I've hit my head against this for almost 2 days.
    > Thank you in advance for any response.
     
    Snis Pilbor, May 25, 2006
    #2
    1. Advertising

  3. Harman Dhaliwal

    Joe Smith Guest

    "Snis Pilbor" <> wrote in message
    > Hi Harman,


    > I'm not the world's best expert, but in my experience whenever you
    > have anomalies like this it suggests that dynamically allocated memory
    > is corrupt. Look closely to make sure everything that needs to be
    > allocated is allocated before use and that once anything is free'd, it
    > is never "used" again.
    >
    > Harman Dhaliwal wrote:
    >> Hi,
    >> I am programming an in memory representation of a database and am at
    >> the tail end, hopefully, of implementation. But the vexing point is
    >> that my program runs fine with all my diagnostic printf statements, but
    >> as soon as I remove the printf statements the program crashes with a
    >> pointer error (usually tried reading x00000000 error).
    >>
    >> I have tried everything from flushing the buffer, tracking done the
    >> routine, etc... can anyone offer me some suggestions to something I
    >> might try? Has anyone had a similiar error? What is going on with
    >> printf that would cause the removal of it to prvent program execution?
    >> Does printf empty a buffer that is overflowing? Please excuse my
    >> lamentations but I've hit my head against this for almost 2 days.
    >> Thank you in advance for any response.


    Mr. Pilbor, please don't top post. Have you (OP) been keeping an eye of the
    return value on printf? joe
     
    Joe Smith, May 25, 2006
    #3
  4. Harman Dhaliwal

    Marc Thrun Guest

    Harman Dhaliwal schrieb:
    > Hi,
    > I am programming an in memory representation of a database and am at
    > the tail end, hopefully, of implementation. But the vexing point is
    > that my program runs fine with all my diagnostic printf statements, but
    > as soon as I remove the printf statements the program crashes with a
    > pointer error (usually tried reading x00000000 error).
    >
    > I have tried everything from flushing the buffer, tracking done the
    > routine, etc... can anyone offer me some suggestions to something I
    > might try? Has anyone had a similiar error? What is going on with
    > printf that would cause the removal of it to prvent program execution?
    > Does printf empty a buffer that is overflowing? Please excuse my
    > lamentations but I've hit my head against this for almost 2 days.
    > Thank you in advance for any response.
    >


    This is one downside of undefined behaviour - it might even seem to
    work, until you remove this call to printf(). So your real mistake
    happened somewhere before the printf() was called while your program was
    executing.

    --
    Marc Thrun
    http://www.tekwarrior.de/
     
    Marc Thrun, May 25, 2006
    #4
  5. Harman Dhaliwal

    Guest

    I haven't been keeping track, but if a negative number is returned, how
    can i use this to track my error? I am relatively new to C and am
    approaching from a high level language (java) background
     
    , May 25, 2006
    #5
  6. said:

    > I am relatively new to C and am
    > approaching from a high level language (java) background


    The cause of your C problem is an error in the way you are using pointers.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, May 25, 2006
    #6
  7. Harman Dhaliwal

    Joe Smith Guest

    "Richard Heathfield"
    > said:
    >
    >> I am relatively new to C and am
    >> approaching from a high level language (java) background

    >
    > The cause of your C problem is an error in the way you are using pointers.


    Did the claim that printf managed to print a negative number of characters
    figure into this diagnosis? joe
     
    Joe Smith, May 25, 2006
    #7
  8. Re: removing a printf statement from my program causes it tocrash!?

    "Joe Smith" <> writes:
    > "Richard Heathfield"
    >> said:
    >>
    >>> I am relatively new to C and am
    >>> approaching from a high level language (java) background

    >>
    >> The cause of your C problem is an error in the way you are using pointers.

    >
    > Did the claim that printf managed to print a negative number of characters
    > figure into this diagnosis? joe


    I don't recall any such claim.

    printf() returns the number of characters printed, or a negative value
    if an error occurs. Even if it were possible for it to print a
    negative number of characters, there would be no way for it to
    indicate that it had done so.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, May 25, 2006
    #8
  9. Harman Dhaliwal

    Default User Guest

    Harman Dhaliwal wrote:

    > Hi,
    > I am programming an in memory representation of a database and am at
    > the tail end, hopefully, of implementation. But the vexing point is
    > that my program runs fine with all my diagnostic printf statements,
    > but as soon as I remove the printf statements the program crashes
    > with a pointer error (usually tried reading x00000000 error).
    >
    > I have tried everything from flushing the buffer, tracking done the
    > routine, etc... can anyone offer me some suggestions to something I
    > might try? Has anyone had a similiar error? What is going on with
    > printf that would cause the removal of it to prvent program execution?
    > Does printf empty a buffer that is overflowing? Please excuse my
    > lamentations but I've hit my head against this for almost 2 days.
    > Thank you in advance for any response.



    How can we debug code you don't show? Seriously.

    When something like that happens, you are probably overwriting some
    automatic data or something like that.

    Reduce it to a minimal, compilable program that demonstrates the
    problem. Often you'll find it yourself when you do that. Otherwise,
    post the code.




    Brian
     
    Default User, May 25, 2006
    #9
  10. Joe Smith said:

    >
    > "Richard Heathfield"
    >> said:
    >>
    >>> I am relatively new to C and am
    >>> approaching from a high level language (java) background

    >>
    >> The cause of your C problem is an error in the way you are using
    >> pointers.

    >
    > Did the claim that printf managed to print a negative number of characters
    > figure into this diagnosis?


    No, my diagnosis was based on the fact that the OP has a Java background.


    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, May 25, 2006
    #10
  11. Harman Dhaliwal

    CBFalconer Guest

    Joe Smith wrote:
    > "Richard Heathfield"
    >> said:
    >>
    >>> I am relatively new to C and am
    >>> approaching from a high level language (java) background

    >>
    >> The cause of your C problem is an error in the way you are using
    >> pointers.

    >
    > Did the claim that printf managed to print a negative number of
    > characters figure into this diagnosis? joe


    Look up the specification of printf. A negative return value
    indicates error. This probably has nothing to do with the OPs
    problem. He probably has some sort of undefined behaviour
    somewhere, such as a buffer overrun or a bad pointer.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
     
    CBFalconer, May 26, 2006
    #11
  12. "Harman Dhaliwal" <> writes:

    > Hi,
    > I am programming an in memory representation of a database and am at
    > the tail end, hopefully, of implementation. But the vexing point is
    > that my program runs fine with all my diagnostic printf statements, but
    > as soon as I remove the printf statements the program crashes with a
    > pointer error (usually tried reading x00000000 error).
    >
    > I have tried everything from flushing the buffer, tracking done the
    > routine, etc... can anyone offer me some suggestions to something I
    > might try? Has anyone had a similiar error? What is going on with
    > printf that would cause the removal of it to prvent program execution?
    > Does printf empty a buffer that is overflowing? Please excuse my
    > lamentations but I've hit my head against this for almost 2 days.
    > Thank you in advance for any response.




    You probably have some undefined behavour elsewhere in the code.
    You seldom need some debugger.


    --
    espen
     
    Espen Myrland, May 26, 2006
    #12
  13. Harman Dhaliwal

    Joe Smith Guest

    "CBFalconer" <> wrote in message
    news:...
    > Joe Smith wrote:
    >> "Richard Heathfield"
    >>> said:
    >>>
    >>>> I am relatively new to C and am
    >>>> approaching from a high level language (java) background
    >>>
    >>> The cause of your C problem is an error in the way you are using
    >>> pointers.

    >>
    >> Did the claim that printf managed to print a negative number of
    >> characters figure into this diagnosis? joe

    >
    > Look up the specification of printf. A negative return value
    > indicates error. This probably has nothing to do with the OPs
    > problem. He probably has some sort of undefined behaviour
    > somewhere, such as a buffer overrun or a bad pointer.
    >
    > --
    > "If you want to post a followup via groups.google.com, don't use
    > the broken "Reply" link at the bottom of the article. Click on
    > "show options" at the top of the article, then click on the
    > "Reply" at the bottom of the article headers." - Keith Thompson
    > More details at: <http://cfaj.freeshell.org/google/>
    > Also see <http://www.safalra.com/special/googlegroupsreply/>
    >
    >

    /* begin reply */
    /* paste */

    "Keith Thompson" <> wrote in message
    news:...
    > "Joe Smith" <> writes:
    >> "Richard Heathfield"
    >>> said:
    >>>
    >>>> I am relatively new to C and am
    >>>> approaching from a high level language (java) background
    >>>
    >>> The cause of your C problem is an error in the way you are using
    >>> pointers.

    >>
    >> Did the claim that printf managed to print a negative number of
    >> characters
    >> figure into this diagnosis? joe

    >
    > I don't recall any such claim.
    >
    > printf() returns the number of characters printed, or a negative value
    > if an error occurs. Even if it were possible for it to print a
    > negative number of characters, there would be no way for it to
    > indicate that it had done so.


    You are correct. The negative return was only premised. My claim is that
    what is premised might have something to do with his output. joe

    /* end paste */

    My newsreader or server has definitely gone Indian on me. joe
     
    Joe Smith, May 26, 2006
    #13
  14. Harman Dhaliwal wrote:
    > as soon as I remove the printf statements the program crashes with a
    > pointer error (usually tried reading x00000000 error).


    Everyone is just guessing; let me guess too.

    Either you've used an uninitialized automatic variable, or have
    coded just "return;" where "return x;" was needed. The
    printf() is irrelevant per se, but happens to change the value of
    undefined register or memory.

    Just a guess based on my own debugging experience. If
    someone's making book I'll want at least 9-to-2 odds before
    betting on this horse.

    James
     
    James Dow Allen, May 27, 2006
    #14
  15. Harman Dhaliwal wrote:
    > Hi,
    > I am programming an in memory representation of a database and am at
    > the tail end, hopefully, of implementation. But the vexing point is
    > that my program runs fine with all my diagnostic printf statements, but
    > as soon as I remove the printf statements the program crashes with a
    > pointer error (usually tried reading x00000000 error).


    I find this interesting. I had the same problem when I did something
    like this:

    int a =3;
    int arr[a];

    instead of using malloc()

    I wonder what is special about printf() which caused it to cover up our
    problem. Perhaps someone could shed some light on it, even though it
    would be off-topic?
     
    ballpointpenthief, May 28, 2006
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward
    Replies:
    1
    Views:
    563
    =?Utf-8?B?U2NvdHQgU2ltb25z?=
    Oct 14, 2004
  2. =?Utf-8?B?QkQgSm9uZXM=?=

    Hourglass ('wait') cursor causes IE to crash

    =?Utf-8?B?QkQgSm9uZXM=?=, Oct 26, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    592
    =?Utf-8?B?QkQgSm9uZXM=?=
    Oct 26, 2004
  3. Bytz
    Replies:
    9
    Views:
    564
    Jeff Dillon
    Apr 13, 2006
  4. Marcel Raap

    Xvfb causes iPlanet to crash

    Marcel Raap, Jul 13, 2004, in forum: Java
    Replies:
    0
    Views:
    360
    Marcel Raap
    Jul 13, 2004
  5. Dusty Lefevre
    Replies:
    0
    Views:
    819
    Dusty Lefevre
    Mar 27, 2005
Loading...

Share This Page