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

H

Harman Dhaliwal

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.
 
S

Snis Pilbor

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.
 
J

Joe Smith

Snis Pilbor said:
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.

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

Marc Thrun

Harman said:
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.
 
H

harmanpal

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
 
R

Richard Heathfield

(e-mail address removed) 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.
 
J

Joe Smith

"Richard Heathfield"
(e-mail address removed) said:


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
 
K

Keith Thompson

Joe Smith said:
"Richard Heathfield"

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.
 
D

Default User

Harman said:
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
 
R

Richard Heathfield

Joe Smith said:
"Richard Heathfield"

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.
 
C

CBFalconer

Joe said:
"Richard Heathfield"

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

Espen Myrland

Harman Dhaliwal said:
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.
 
J

Joe Smith

CBFalconer said:
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 said:
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
 
J

James Dow Allen

Harman said:
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
 
B

ballpointpenthief

Harman said:
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top