473,508 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Am I doing something wrong with printf() here?

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 run-time. I know it contains
non-standard constructs (using the MySql C api) but I wanted to know if it's
printf() I'm misuing. Here are the snippets:

/* m is of type MYSQL* */
unsigned int code = mysql_errno(m);
const char *description = mysql_error(m);

if(code && description)
{
printf("Failed to create chemistry recipe table.\n"
"Error code: %d\n"
"Description: %s\n", code, description);
}

This snippet doesn't segfault at runtime but the following one does:
printf("Failed to create chemistry recipe table.\n"
"Error code: %d\n"
"Description: %s\n", mysql_errno(m), mysql_error(m));

Both snippets produce the same output though. Is this mis-use of printf() in
the last snippet or a mysql c api problem (if so, I will ask this question
elsewhere)?

Thanks for any help

/ Eric
Nov 14 '05 #1
9 1355

"Eric Lilja" <er*************@yahoo.com> wrote in message
news:cr**********@news.island.liu.se...
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 run-time. I know it contains
non-standard constructs (using the MySql C api) but I wanted to know if it's printf() I'm misuing. Here are the snippets:

/* m is of type MYSQL* */
unsigned int code = mysql_errno(m);
const char *description = mysql_error(m);

if(code && description)
{
printf("Failed to create chemistry recipe table.\n"
"Error code: %d\n"
"Description: %s\n", code, description);
}

This snippet doesn't segfault at runtime but the following one does:
printf("Failed to create chemistry recipe table.\n"
"Error code: %d\n"
"Description: %s\n", mysql_errno(m), mysql_error(m));

Both snippets produce the same output though. Is this mis-use of printf() in the last snippet or a mysql c api problem (if so, I will ask this question
elsewhere)?


The proper 'printf()' specifier for type 'unsigned int'
is %u. For type 'char *' (or 'const char*') it's %s.
If your supplied arguments' types do not match the
specifiers, the behavior is undefined, in which case
*anything* could happen, from 'segfault' to 'seems to work'
or anything else.

-Mike
Nov 14 '05 #2

"Mike Wahler"wrote:

"Eric Lilja" <er*************@yahoo.com> wrote in message
news:cr**********@news.island.liu.se...
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 run-time. I know it contains
non-standard constructs (using the MySql C api) but I wanted to know if it's
printf() I'm misuing. Here are the snippets:

/* m is of type MYSQL* */
unsigned int code = mysql_errno(m);
const char *description = mysql_error(m);

if(code && description)
{
printf("Failed to create chemistry recipe table.\n"
"Error code: %d\n"
"Description: %s\n", code, description);
}

This snippet doesn't segfault at runtime but the following one does:
printf("Failed to create chemistry recipe table.\n"
"Error code: %d\n"
"Description: %s\n", mysql_errno(m), mysql_error(m));

Both snippets produce the same output though. Is this mis-use of printf()

in
the last snippet or a mysql c api problem (if so, I will ask this
question
elsewhere)?


The proper 'printf()' specifier for type 'unsigned int'
is %u. For type 'char *' (or 'const char*') it's %s.
If your supplied arguments' types do not match the
specifiers, the behavior is undefined, in which case
*anything* could happen, from 'segfault' to 'seems to work'
or anything else.


Thanks for catching that error, Mike. Unfortunately, my problem remains so I
can only assume I've encountered a problem with the mysql c api code. I will
look for an appropriate forum and ask there.
-Mike


/ Eric
Nov 14 '05 #3

"Eric Lilja" <er*************@yahoo.com> wrote in message
news:cr*********@news.island.liu.se...

"Mike Wahler"wrote:

"Eric Lilja" <er*************@yahoo.com> wrote in message
news:cr**********@news.island.liu.se...
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 run-time. I know it contains
non-standard constructs (using the MySql C api) but I wanted to know if it's
printf() I'm misuing. Here are the snippets:

/* m is of type MYSQL* */
unsigned int code = mysql_errno(m);
const char *description = mysql_error(m);

if(code && description)
{
printf("Failed to create chemistry recipe table.\n"
"Error code: %d\n"
"Description: %s\n", code, description);
}

This snippet doesn't segfault at runtime but the following one does:
printf("Failed to create chemistry recipe table.\n"
"Error code: %d\n"
"Description: %s\n", mysql_errno(m), mysql_error(m));

Both snippets produce the same output though. Is this mis-use of
printf() in
the last snippet or a mysql c api problem (if so, I will ask this
question
elsewhere)?


The proper 'printf()' specifier for type 'unsigned int'
is %u. For type 'char *' (or 'const char*') it's %s.
If your supplied arguments' types do not match the
specifiers, the behavior is undefined, in which case
*anything* could happen, from 'segfault' to 'seems to work'
or anything else.


Thanks for catching that error, Mike. Unfortunately, my problem remains so

I can only assume I've encountered a problem with the mysql c api code. I will look for an appropriate forum and ask there.


You can find out if the 'mysql' stuff is the problem by
removing it from your program and substituting 'dummy'
variables to represent e.g. mysql function return values,
and give those to 'printf()'. Also, note that often a
bug can easily wait to manifest itself until execution
reaches some part of the code other than where the bug
actually is. I.e. 'divide and conquer'. Do your program
in small pieces. Perform unit tests.

-Mike
Nov 14 '05 #4
In article <cr**********@news.island.liu.se>,
"Eric Lilja" <er*************@yahoo.com> wrote:
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 run-time. I know it contains
non-standard constructs (using the MySql C api) but I wanted to know if it's
printf() I'm misuing. Here are the snippets:

/* m is of type MYSQL* */
unsigned int code = mysql_errno(m);
const char *description = mysql_error(m);

if(code && description)
{
printf("Failed to create chemistry recipe table.\n"
"Error code: %d\n"
"Description: %s\n", code, description);
}

This snippet doesn't segfault at runtime but the following one does:
printf("Failed to create chemistry recipe table.\n"
"Error code: %d\n"
"Description: %s\n", mysql_errno(m), mysql_error(m));

Both snippets produce the same output though. Is this mis-use of printf() in
the last snippet or a mysql c api problem (if so, I will ask this question
elsewhere)?
1. In the second code it is not defined in which order mysql_errno and
mysql_error are called. There might be a dependency; maybe mysql_error
returns NULL until after a call to mysql_errno which returns a non-zero
value. Try what happens if you start with
const char *description = mysql_error(m);
unsigned int code = mysql_errno(m);


that is in reverse order.

2. Obviously if mysql_errno returns zero then the output will be
different. Quite possible that mysql_error will then return NULL, which
means the first printf isn't called at all while the second one crashes.

3. Or do you mean you called mysql_errno and mysql_error once before the
printf and then again within the printf? Maybe mysql_errno reports an
error only once (any errors since the previous call to mysql_errno? ),
so the printf will crash.
Nov 14 '05 #5

"Christian Bau" wrote:
In article <cr**********@news.island.liu.se>,
"Eric Lilja" <er*************@yahoo.com> wrote:
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 run-time. I know it contains
non-standard constructs (using the MySql C api) but I wanted to know if
it's
printf() I'm misuing. Here are the snippets:

/* m is of type MYSQL* */
unsigned int code = mysql_errno(m);
const char *description = mysql_error(m);

if(code && description)
{
printf("Failed to create chemistry recipe table.\n"
"Error code: %d\n"
"Description: %s\n", code, description);
}

This snippet doesn't segfault at runtime but the following one does:
printf("Failed to create chemistry recipe table.\n"
"Error code: %d\n"
"Description: %s\n", mysql_errno(m), mysql_error(m));

Both snippets produce the same output though. Is this mis-use of printf()
in
the last snippet or a mysql c api problem (if so, I will ask this
question
elsewhere)?
1. In the second code it is not defined in which order mysql_errno and
mysql_error are called. There might be a dependency; maybe mysql_error
returns NULL until after a call to mysql_errno which returns a non-zero
value. Try what happens if you start with
const char *description = mysql_error(m);
unsigned int code = mysql_errno(m);


that is in reverse order.


Very interesting. I tried switching the order and it did indeed alter the
programs behaviour. It didn't cause a crash, but different output.

unsigned int code = mysql_errno(m);
const char *description = mysql_error(m);

if(code && description)
{
printf("Failed to create chemistry recipe table.\n"
"Error code: %u\n"
"Description: %s\n", code, description);
}

yields the output:
Failed to create chemistry recipe table.
Error code: 1050
Description: Table 'chemistry_recipes' already exists

and with the initilizations switched:
const char *description = mysql_error(m);
unsigned int code = mysql_errno(m);

if(code && description)
{
printf("Failed to create chemistry recipe table.\n"
"Error code: %u\n"
"Description: %s\n", code, description);
}
yields this output:
Failed to create chemistry recipe table.
Error code: 10373019
Description: Table 'chemistry_recipes' already exists
(note the different error code)

2. Obviously if mysql_errno returns zero then the output will be
different. Quite possible that mysql_error will then return NULL, which
means the first printf isn't called at all while the second one crashes.

3. Or do you mean you called mysql_errno and mysql_error once before the
printf and then again within the printf? Maybe mysql_errno reports an
error only once (any errors since the previous call to mysql_errno? ),
so the printf will crash.


/ Eric
Nov 14 '05 #6
Eric Lilja wrote:
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 run-time. I know it contains
non-standard constructs (using the MySql C api) but I wanted to know if it's
printf() I'm misuing. Here are the snippets:

/* m is of type MYSQL* */
unsigned int code = mysql_errno(m);
const char *description = mysql_error(m);

if(code && description)
{
printf("Failed to create chemistry recipe table.\n"
"Error code: %d\n"
"Description: %s\n", code, description);
}

This snippet doesn't segfault at runtime but the following one does:
printf("Failed to create chemistry recipe table.\n"
"Error code: %d\n"
"Description: %s\n", mysql_errno(m), mysql_error(m));

Both snippets produce the same output though.
That I don't understand. If they produce the same output, then the
segfault is probably occurring *after* this line. It is very unlikely
that a line of code will both cause a segfault and be executed with
otherwise expected results.
Is this mis-use of printf() in
the last snippet or a mysql c api problem (if so, I will ask this question
elsewhere)?

The first snippet checks to see if description is NULL; the second does
not check that mysql_error(m) is NULL. There are, then, two probable
scenarios
1) Most likely: The segfault does not occur where you think it does.
2) Less likely: mysql_error(m) is not 0 in the first snippet at the
point that its value is assigned to description, but is 0 in the second
snippet at the point that it is returned to printf.

Nov 14 '05 #7
"Eric Lilja" <er*************@yahoo.com> writes:
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 run-time. I know it contains
non-standard constructs (using the MySql C api) but I wanted to know if it's
printf() I'm misuing. Here are the snippets:

/* m is of type MYSQL* */
unsigned int code = mysql_errno(m);
const char *description = mysql_error(m);

if(code && description)
{
printf("Failed to create chemistry recipe table.\n"
"Error code: %d\n"
"Description: %s\n", code, description);
}

This snippet doesn't segfault at runtime but the following one does:
printf("Failed to create chemistry recipe table.\n"
"Error code: %d\n"
"Description: %s\n", mysql_errno(m), mysql_error(m));

Both snippets produce the same output though. Is this mis-use of printf() in
the last snippet or a mysql c api problem (if so, I will ask this question
elsewhere)?


Use "%u", not "%d" to print an unsigned int (but this isn't going to
cause a seg fault).

What are the declared return types of mysql_errno and mysql_error?
Did you include the header that declares them?

And as someone else mentioned, there may be an ordering dependency on
the calls to mysql_errno() and mysql_error() in your second printf().

--
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.
Nov 14 '05 #8
Keith Thompson wrote:
"Eric Lilja" <er*************@yahoo.com> writes:
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
run-time. I know it contains non-standard constructs (using the
MySql C api) but I wanted to know if it's printf() I'm misuing.
Here are the snippets:

/* m is of type MYSQL* */
unsigned int code = mysql_errno(m);
const char *description = mysql_error(m);

if(code && description)
{
printf("Failed to create chemistry recipe table.\n"
"Error code: %d\n"
"Description: %s\n", code, description);
}

This snippet doesn't segfault at runtime but the following one
does:

printf("Failed to create chemistry recipe table.\n"
"Error code: %d\n"
"Description: %s\n", mysql_errno(m), mysql_error(m));

Both snippets produce the same output though. Is this mis-use of
printf() in the last snippet or a mysql c api problem (if so, I
will ask this question elsewhere)?


Use "%u", not "%d" to print an unsigned int (but this isn't going
to cause a seg fault).

What are the declared return types of mysql_errno and mysql_error?
Did you include the header that declares them?

And as someone else mentioned, there may be an ordering dependency on
the calls to mysql_errno() and mysql_error() in your second printf().


The first is guarded against 0/NULL values for code/description,
while the second has no such guard to the corresponding printf
arguments. I suspect printf itself is aborting on finding a NULL
to satisfy a %s specification.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #9
Eric Lilja wrote:
[...]
/* m is of type MYSQL* */
unsigned int code = mysql_errno(m);
const char *description = mysql_error(m);

if(code && description)
{
printf("Failed to create chemistry recipe table.\n"
"Error code: %d\n"
"Description: %s\n", code, description);
}

This snippet doesn't segfault at runtime but the following one does:
printf("Failed to create chemistry recipe table.\n"
"Error code: %d\n"
"Description: %s\n", mysql_errno(m), mysql_error(m));

Both snippets produce the same output though. Is this mis-use of printf() in
the last snippet or a mysql c api problem (if so, I will ask this question
elsewhere)?

[...]

I would suggest against using the latter approach. The calling
conventions being used determine the order in which the arguments are
passed. I would generally avoid calling multiple functions or using
prefix/postfix expressions within argument lists.

Regards,
Jonathan.
Nov 14 '05 #10

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

Similar topics

5
2663
by: Jepsensen | last post by:
Dear Everybody. I got a problem with my cpp code. I'm trying to calculate a very simple Discrete Cosine Transform (DCT), but my c++ code seams to calculate a wrong result. I have just started...
2
1195
by: Robert Smith | last post by:
Why doesnt this code work???? it just stops at the socket() and does not print HI. why is this? #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h>...
60
3269
by: Dominique Léger | last post by:
Hello guys, I'm kinda new to C, and I'm having a hard time with strings. What I'm trying to do is a simple function that trims spaces & tabs at the beginning of a given string. For example, I...
15
8987
by: robert maas, see http://tinyurl.com/uh3t | last post by:
Here's the source: #include <stdio.h> #include <errno.h> main () { char* str = "9999999999"; long long int llin; char* endptr; /* Set by strtoll */ int nch; errno = 0; llin = strtoll(str,...
16
1755
by: chutsu | last post by:
Ok Here is a problem, I got a imaginary database program that I need to code, to add a patient I have function inser_patient. but when I try to input the details it doesn't quite work the way I...
4
1268
by: Benoit Lefebvre | last post by:
Weird subject you will say.. I didn't know what to put in there.. Long story short, I'm changing the value of a variable from a function.. Nothing unusual there.. How come the value of the...
34
1730
by: raphfrk | last post by:
This program should copy one file onto the other. It works if I compile it with gcc to a cygwin program. However, if I compile it with the -mno-cygwin option, it doesn't work (this targets native...
4
243
by: Cromulent | last post by:
On 2008-07-06 03:16:23 +0100, Ben Bacarisse <ben.usenet@bsb.me.uksaid: Ah, I see thanks. Ah I meant it to be '\n' not ' and \n if that makes sense. They were meant to be enclosing the...
3
2941
by: Blue sky | last post by:
Hi,I am a new C++ learner.The follow prgram produces a wrong result,but I can't find the wrong.Can you help me?Thank you! #include<stdio.h> long factorial( long number); int main() { int...
0
7328
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7049
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4709
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.