473,383 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

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.

May 25 '06 #1
14 6782

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.


May 25 '06 #2

"Snis Pilbor" <sn********@yahoo.com> 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
May 25 '06 #3
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/
May 25 '06 #4
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 '06 #5
ha*******@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.

--
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)
May 25 '06 #6

"Richard Heathfield"
ha*******@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.


Did the claim that printf managed to print a negative number of characters
figure into this diagnosis? joe
May 25 '06 #7
"Joe Smith" <gr**********@netzero.net> writes:
"Richard Heathfield"
ha*******@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.


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) ks***@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.
May 25 '06 #8
Joe Smith said:

"Richard Heathfield"
ha*******@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.


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)
May 25 '06 #9
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
May 25 '06 #10
"Harman Dhaliwal" <ha*******@gmail.com> 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
May 25 '06 #11
Joe Smith wrote:
"Richard Heathfield"
ha*******@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.


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/>
May 26 '06 #12

"CBFalconer" <cb********@yahoo.com> wrote in message
news:44***************@yahoo.com...
Joe Smith wrote:
"Richard Heathfield"
ha*******@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.
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" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org... "Joe Smith" <gr**********@netzero.net> writes:
"Richard Heathfield"
ha*******@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.


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
May 26 '06 #13

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

May 27 '06 #14

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?

May 28 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: gopal srinivasan | last post by:
Hi, I have a text like this - "This is a message containing tabs and white spaces" Now this text contains tabs and white spaces. I want remove the tabs and white...
8
by: aditya | last post by:
hi, Can anybody please tell me that how the following printf(...) statement works- main(){ int d=9; printf("%d",printf("%d")); return 0;
9
by: Eric Lilja | last post by:
Hello, I have two code snippets I want you to look at. My program compiles without warnings (warning level set to max, gcc 3.4.3) with either snippet but the latter one causes a segfault at...
18
by: biras | last post by:
hi #include<stdio.h> void main( ) { char *p; p="yes"; printf(p);
102
by: tom fredriksen | last post by:
Hi I was doing a simple test of the speed of a "maths" operation and when I tested it I found that removing the loop that initialises the data array for the operation caused the whole program to...
11
by: timmu | last post by:
Someone asked me a question about integer division and printf yesterday, I tell him he should do a casting to float/double before you do any interger division. But he doesn't think so, so I try...
19
by: RedDevilDan | last post by:
I am working on a Memory Footprint Reduction project. I came across an idea to disable all printf statements so that less memory is required. In addition, when there is no single printf statement,...
18
by: sam_cit | last post by:
Hi Everyone, int main() { printf("not included stdio.h"); } Yes, i haven't included stdio.h and my compiler would generate a warning and would assume that it would return a int, my question...
29
by: candy_init | last post by:
Hi all, I just came across the following program: #include <stdio.h> int main() { float a = 12.5; printf("%d\n", a); printf("%d\n", *(int *)&a); return 0;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.