Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Harman Dhaliwal
Guest
 
Posts: n/a
#1: May 25 '06
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
Guest
 
Posts: n/a
#2: May 25 '06

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



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:[color=blue]
> 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.[/color]

Joe Smith
Guest
 
Posts: n/a
#3: May 25 '06

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



"Snis Pilbor" <snispilbor@yahoo.com> wrote in message[color=blue]
> Hi Harman,[/color]
[color=blue]
> 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:[color=green]
>> 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.[/color][/color]

Mr. Pilbor, please don't top post. Have you (OP) been keeping an eye of the
return value on printf? joe


Marc Thrun
Guest
 
Posts: n/a
#4: May 25 '06

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


Harman Dhaliwal schrieb:[color=blue]
> 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.
>[/color]

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/
harmanpal@gmail.com
Guest
 
Posts: n/a
#5: May 25 '06

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


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

Richard Heathfield
Guest
 
Posts: n/a
#6: May 25 '06

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


harmanpal@gmail.com said:
[color=blue]
> I am relatively new to C and am
> approaching from a high level language (java) background[/color]

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)
Joe Smith
Guest
 
Posts: n/a
#7: May 25 '06

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



"Richard Heathfield"[color=blue]
> harmanpal@gmail.com said:
>[color=green]
>> I am relatively new to C and am
>> approaching from a high level language (java) background[/color]
>
> The cause of your C problem is an error in the way you are using pointers.[/color]

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


Keith Thompson
Guest
 
Posts: n/a
#8: May 25 '06

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


"Joe Smith" <grumpy196884@netzero.net> writes:[color=blue]
> "Richard Heathfield"[color=green]
>> harmanpal@gmail.com said:
>>[color=darkred]
>>> I am relatively new to C and am
>>> approaching from a high level language (java) background[/color]
>>
>> The cause of your C problem is an error in the way you are using pointers.[/color]
>
> Did the claim that printf managed to print a negative number of characters
> figure into this diagnosis? joe[/color]

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) kst-u@mib.org <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.
Richard Heathfield
Guest
 
Posts: n/a
#9: May 25 '06

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


Joe Smith said:
[color=blue]
>
> "Richard Heathfield"[color=green]
>> harmanpal@gmail.com said:
>>[color=darkred]
>>> I am relatively new to C and am
>>> approaching from a high level language (java) background[/color]
>>
>> The cause of your C problem is an error in the way you are using
>> pointers.[/color]
>
> Did the claim that printf managed to print a negative number of characters
> figure into this diagnosis?[/color]

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)
Default User
Guest
 
Posts: n/a
#10: May 26 '06

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


Harman Dhaliwal wrote:
[color=blue]
> 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.[/color]


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
Espen Myrland
Guest
 
Posts: n/a
#11: May 26 '06

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


"Harman Dhaliwal" <harmanpal@gmail.com> writes:
[color=blue]
> 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.[/color]



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


--
espen
CBFalconer
Guest
 
Posts: n/a
#12: May 26 '06

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


Joe Smith wrote:[color=blue]
> "Richard Heathfield"[color=green]
>> harmanpal@gmail.com said:
>>[color=darkred]
>>> I am relatively new to C and am
>>> approaching from a high level language (java) background[/color]
>>
>> The cause of your C problem is an error in the way you are using
>> pointers.[/color]
>
> Did the claim that printf managed to print a negative number of
> characters figure into this diagnosis? joe[/color]

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/>


Joe Smith
Guest
 
Posts: n/a
#13: May 26 '06

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



"CBFalconer" <cbfalconer@yahoo.com> wrote in message
news:4476379F.797F77E0@yahoo.com...[color=blue]
> Joe Smith wrote:[color=green]
>> "Richard Heathfield"[color=darkred]
>>> harmanpal@gmail.com 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.[/color]
>>
>> Did the claim that printf managed to print a negative number of
>> characters figure into this diagnosis? joe[/color]
>
> 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/>
>
>[/color]
/* begin reply */
/* paste */

"Keith Thompson" <kst-u@mib.org> wrote in message
news:lnlkspbve8.fsf@nuthaus.mib.org...[color=blue]
> "Joe Smith" <grumpy196884@netzero.net> writes:[color=green]
>> "Richard Heathfield"[color=darkred]
>>> harmanpal@gmail.com 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.[/color]
>>
>> Did the claim that printf managed to print a negative number of
>> characters
>> figure into this diagnosis? joe[/color]
>
> 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.[/color]

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


James Dow Allen
Guest
 
Posts: n/a
#14: May 27 '06

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



Harman Dhaliwal wrote:[color=blue]
> as soon as I remove the printf statements the program crashes with a
> pointer error (usually tried reading x00000000 error).[/color]

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

ballpointpenthief
Guest
 
Posts: n/a
#15: May 28 '06

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



Harman Dhaliwal wrote:[color=blue]
> 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).[/color]

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?

Closed Thread