473,569 Members | 2,806 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

No format string passed to variable argument list function

Hi,

I have a simple printf-like function:

int print(const char *format, ...)
{
char buffer[1024];
va_list argptr;
int i;

va_start(argptr , format);
i = vsnprintf(buffe r, sizeof(buffer), format, argptr);
va_end(argptr);

buffer[sizeof(buffer)-1] = '\0';

printf("%s\n",b uffer); /* this bit just for the sake of testing */

return i;
}

If I call the function using something like:
char message[50];
strcpy(message, "hi there");
print("%s",mess age);

everything works, but if I do:
print(message);

it doesn't (program crashes with an abort).

Is there something wrong with what I'm doing, or should I be looking
elsewhere to work out the cause of my crash?

Thanks a lot,
Adam
Oct 15 '08 #1
30 2701
Adam <ne**@snowstone .org.ukwrites:
Hi,

I have a simple printf-like function:

int print(const char *format, ...)
{
char buffer[1024];
va_list argptr;
int i;

va_start(argptr , format);
i = vsnprintf(buffe r, sizeof(buffer), format, argptr);
va_end(argptr);

buffer[sizeof(buffer)-1] = '\0';

printf("%s\n",b uffer); /* this bit just for the sake of testing */

return i;
}

If I call the function using something like:
char message[50];
strcpy(message, "hi there");
print("%s",mess age);

everything works, but if I do:
print(message);

it doesn't (program crashes with an abort).
I don't see a problem myself. I took your function and added a main function:

#include <stdarg.h>
#include <stdio.h>

/* your function */

int main(void) {
print("%s\n", "Hello world!");
print("Goodbye world!\n");
return 0;
}

and it compiles and runs without problems on three different systems.

There could be something that I'm missing, though, which doesn't show up
on those systems. Can you post a complete program that aborts, and tell
us the system and compiler you're using?

It certainly seems like v*printf should be able to handle a va_list from
a function with no extra arguments, though the standard doesn't
explicitly seem to say so. It's conceivable that there's a bug in your
standard library, but hard to say just yet.
Oct 15 '08 #2
Adam <ne**@snowstone .org.ukwrites:
I have a simple printf-like function:

int print(const char *format, ...)
{
char buffer[1024];
va_list argptr;
int i;

va_start(argptr , format);
i = vsnprintf(buffe r, sizeof(buffer), format, argptr);
va_end(argptr);

buffer[sizeof(buffer)-1] = '\0';
This isn't your problem, but what's the purpose of the above?
vsnprintf() should already write a '\0'-terminated string to buffer;
you're just setting character 1023 (which is likely far beyond the end
of the string) to '\0'.
printf("%s\n",b uffer); /* this bit just for the sake of testing */

return i;
}

If I call the function using something like:
char message[50];
strcpy(message, "hi there");
print("%s",mess age);

everything works, but if I do:
print(message);

it doesn't (program crashes with an abort).

Is there something wrong with what I'm doing, or should I be looking
elsewhere to work out the cause of my crash?
It would help if you could post a small, complete, compilable program
that demonstrates the problem. I don't see anything in what you
posted that explains the symptom you're seeing.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 16 '08 #3
Adam wrote:
Hi,

I have a simple printf-like function:
Did you include <stdarg.h>?

--
Ian Collins
Oct 16 '08 #4
Adam <n...@snowstone .org.ukwrote:
I have a simple printf-like function:

int print(const char *format, ...)
{
* char buffer[1024];
Maybe make this static. There's no upper or lower limit
on automatic storage, but I prefer not to allocate more
than 256 bytes in a single function.
* va_list argptr;
* int i;

* va_start(argptr , format);
* i = vsnprintf(buffe r, sizeof(buffer), format, argptr);
Note that [v]snprintf is new in C99. Although many C90
implementations have it as an extension, they may
differ in semantics and prototype.

Did you include the correct header or prototype?
Is print defined before or after use? If after,
did you prototype it correctly before use?
* va_end(argptr);

* buffer[sizeof(buffer)-1] = '\0';
This shouldn't be necessary.
* printf("%s\n",b uffer); /* this bit just for the sake of testing */
puts(buffer) is better since the result string could have
% characters in it that will be subject to conversion.
* return i;
}

If I call the function using something like:
char message[50];
strcpy(message, "hi there");
char message[50] = "hi there";
print("%s",mess age);

everything works, but if I do:
print(message);

it doesn't (program crashes with an abort).
Post a compilable program that exhibits the problem.

Also try print("%s\n", message);
Is there something wrong with what I'm doing,
Nothing obvious.
or should I be looking elsewhere to work out the
cause of my crash?
If you're using a C90 extension, you should check
the manual on what vsnprintf does.

--
Peter
Oct 16 '08 #5
Ian Collins <ia******@hotma il.comwrites:
Adam wrote:
>I have a simple printf-like function:
Did you include <stdarg.h>?
He must have; otherwise the identifier "va_arg" wouldn't be visible.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 16 '08 #6
Keith Thompson wrote:
Ian Collins <ia******@hotma il.comwrites:
>Adam wrote:
>>I have a simple printf-like function:
Did you include <stdarg.h>?

He must have; otherwise the identifier "va_arg" wouldn't be visible.
Or va_list. Yes it was a silly question!

--
Ian Collins
Oct 16 '08 #7
Ian Collins <ian-n...@hotmail.co mwrote:
Keith Thompson wrote:
Ian Collins <ian-n...@hotmail.co mwrites:
Adam wrote:
I have a simple printf-like function:
>
Did you include <stdarg.h>?
He must have; otherwise the identifier "va_arg"
wouldn't be visible.

Or va_list. *Yes it was a silly question!
Not necessarily. How do you know Adam isn't using
<varargs.h>?

--
Peter
Oct 16 '08 #8
Peter Nilsson <ai***@acay.com .auwrites:
Adam <n...@snowstone .org.ukwrote:
<snip>
>Â* printf("%s\n",b uffer); /* this bit just for the sake of testing */

puts(buffer) is better since the result string could have
% characters in it that will be subject to conversion.
Did you misread the printf? It looks fine to me (though puts probably
has the edge).

--
Ben.
Oct 16 '08 #9
Ian Collins <ia******@hotma il.comwrites:
Keith Thompson wrote:
>Ian Collins <ia******@hotma il.comwrites:
>>Adam wrote:
I have a simple printf-like function:

Did you include <stdarg.h>?

He must have; otherwise the identifier "va_arg" wouldn't be visible.
Or va_list. Yes it was a silly question!
Yes, I meant va_list.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 16 '08 #10

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

Similar topics

4
13530
by: Andrew Chalk | last post by:
I am a raw beginner to Python. I just read in "Learning Python" that assigning to a string argument inside a function does not change the string in the caller. I want an assignment in the function to alter the passed string in the caller. Is there any way to do this? For example def SafeAdd(self, Variable, Value): if self.form.has_key(...
3
4489
by: Lyn | last post by:
Hi, I have a Search input form which collects from the user a person's name. I am using LIKE with a "%" suffix in the SQL so that the user does not have to type in the full name. When they hit the Search button, a query is run to search the Person table for a match. This produces a recordset (I am using ADO). If the RecordCount is zero,...
6
2826
by: Stuart McGraw | last post by:
I am looking for a VBA "format" or "template" function, that is, a function that takes a format string and a varying number of arguments, and substitutes the argument values into the format string as specified in the format string. For example fmt("this is $1 format string. arg2 is $2", "short", 3) would return the string "this is short...
7
4349
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
23
3559
by: Nascimento | last post by:
Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
35
2429
by: michael.casey | last post by:
The purpose of this post is to obtain the communities opinion of the usefulness, efficiency, and most importantly the correctness of this small piece of code. I thank everyone in advance for your time in considering this post, and for your comments. I wrote this function to simplify the task of combining strings using mixed sources. I often...
6
1483
by: AT | last post by:
Can someone please explain me the following: quote from documentation "Unlike other intrinsic data types, String is a reference type. When a variable of reference type is passed as an argument to a function or subroutine, a reference to the memory address where the data is stored is passed instead of the actual value of the string. " Code
232
13119
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an...
21
55590
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } function getName()
0
7620
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7931
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7985
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6298
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3666
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3659
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2120
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 we have to send another system
0
959
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.