* stdarg's unpredictable
@ 1994-01-14 22:40 anh
1994-01-15 19:58 ` Daniel S. Riley
1994-01-17 22:19 ` Gerald Feldman USG
0 siblings, 2 replies; 3+ messages in thread
From: anh @ 1994-01-14 22:40 UTC (permalink / raw)
Hello everybody,
I compiled the below program on a Dec 3000/500 AXP ALPHA running OSF/1.
To compile and to run: cc test.c then a.out :-).
Anyway, the point is, if I uncomment the commented printf line, I will
get a different results. No, not test: Hello World showing up and not
showing up.:-). I used to get segmented fault without the printf and
strange output with printf. After changing the string in the test() call,
A space more or less, I don't remember. I no longer get segmented fault,
but the program is still not functioning correctly. The output and the
behavior is unpredictable. Can someone shed some light?
Change test(" .... ...") to include more parameters, a space more or less.
Comment and uncomment the printf in test(), and you'll see what I mean.
I put this test program together because we experience some strange behavior
in one of our other monitoring program. I tracked the problem to the functions
using arbitrary number of arguments. Thanks for all help.
Anh
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdarg.h>
void test(format)
char *format;
{
va_list vlist;
char msg[500];
va_start(vlist,format);
vsprintf(msg,format,vlist);
va_end(vlist);
/* printf("\n\ntest: %s\n",msg);*/
}
main()
{
unsigned int a=4;
unsigned long b=4;
char name[]="Hello World";
printf("sizeof(size_t)=%d sizeof(u int)=%d, sizeof(int)=%d, sizeof(u_long)=%d, sizeof(long)=%d\n",
sizeof(size_t),sizeof(unsigned int),sizeof(int),sizeof(u_long),sizeof(long));
printf("a=%d, b=%d\n",a,b);
printf("ntohl(a)=%u, htonl(a)=%u, ntohl(htonl(a))=%u\n",ntohl(a),htonl(a),
ntohl(htonl(a)));
printf("ntohl(b)=%lu, htonl(b)=%lu, ntohl(hotnl(b))=%lu\n",ntohl(b),htonl(b),
ntohl(htonl(b)));
printf("\n%s\n",name);
test("ntohl(b)=%d, htonl(b)=%d, %s\n",ntohl(b),htonl(b),name);
}
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: stdarg's unpredictable
1994-01-14 22:40 stdarg's unpredictable anh
@ 1994-01-15 19:58 ` Daniel S. Riley
1994-01-17 22:19 ` Gerald Feldman USG
1 sibling, 0 replies; 3+ messages in thread
From: Daniel S. Riley @ 1994-01-15 19:58 UTC (permalink / raw)
In article <1994Jan14.164003.57216@kuhub.cc.ukans.edu>,
anh@kuhub.cc.ukans.edu writes:
[...]
anh> #include <stdarg.h>
[...]
anh> void test(format)
anh> char *format;
anh> {
anh> va_list vlist;
[...]
Your function declaration is incorrect--it *must* be
void test(char *format, ...)
See the ANSI C standard, section 6.7.1, "Semantics":
"If a function that accepts a variable number of arguments is
defined without a parameter type list that ends with the ellipsis
notation, the result is undefined."
Fix that, and you should find that it works fine.
--
-Dan Riley Internet: dsr@lns598.tn.cornell.edu
-Wilson Lab, Cornell University HEPNET/SPAN: lns598::dsr (44630::dsr)
"Distance means nothing/To me." -Kate Bush
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: stdarg's unpredictable
1994-01-14 22:40 stdarg's unpredictable anh
1994-01-15 19:58 ` Daniel S. Riley
@ 1994-01-17 22:19 ` Gerald Feldman USG
1 sibling, 0 replies; 3+ messages in thread
From: Gerald Feldman USG @ 1994-01-17 22:19 UTC (permalink / raw)
In article <1994Jan14.164003.57216@kuhub.cc.ukans.edu>, anh@kuhub.cc.ukans.edu writes:
< much code removed>
unsigned int a=4;
unsigned long b=4;
char name[]="Hello World";
printf("a=%d, b=%d\n",a,b);
--
Also, as a kind of minor nit, The %d specification takes an int (eg. 32 bit
type) as its argument, and element b is a long (eg. 64 bits). You should
use %ld for longs.
Jerry Feldman
--
+-------------------------------------------------------------+
Jerry Feldman USG
Mailstop: ZKO3-3/Y25 DTN:381-2970
Digital Equipment Corp. EXT:(603)881-2970
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~1994-01-17 22:19 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1994-01-14 22:40 stdarg's unpredictable anh
1994-01-15 19:58 ` Daniel S. Riley
1994-01-17 22:19 ` Gerald Feldman USG
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox