Date: Sun, 9 Jun 2002 19:39:18 +0300 From: Giorgos Keramidas <keramida@ceid.upatras.gr> To: "Christopher J. Umina" <FJU@Fritzilldo.com> Cc: FreeBSD Questions <questions@FreeBSD.ORG> Subject: Re: Load Averages with C Message-ID: <20020609163918.GB1595@hades.hell.gr> In-Reply-To: <001901c20fe2$9988cc60$0301a8c0@uminafamily.com> References: <001201c20f54$46ba9e20$0301a8c0@uminafamily.com> <20020608222753.GA38586@hades.hell.gr> <001901c20fe2$9988cc60$0301a8c0@uminafamily.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On 2002-06-09 11:22 -0700, Christopher J. Umina wrote: > Why when I use getloadavg(3) do I come out with wierd numbers? > > This is my code. > > #include <stdio.h> > #include <stdlib.h> > > int main(void) { > int i; > double loadavg[3]; > int getloadavg(double loadavg[], int nelem); This is a prototype for getloadavg(). Since you included stdlib.h you don't need this. > for (i = 0; i < 3; ++i) { > printf ("%d\n", loadavg[i]); You are using the values of the loadavg[] array without having first called getloadavg(). Your printfs will output whatever random garbage happens to be on the stack (where loadavg[] is allocated, since it's a local variable of the main() function). Try this: #include <stdio.h> #include <stdlib.h> int main(void) { int i; double loadavg[3]; getloadavg(loadavg, 3); for (i = 0; i < 3; ++i) { printf (" %5.3lf", loadavg[i]); } printf("\n"); return (0); } Which seems to work nicely here: 19:36 [charon@hades /tmp]$ cc -Wall -W orig.foo.c orig.foo.c: In function `main': orig.foo.c:10: warning: int format, double arg (arg 2) orig.foo.c:12: warning: control reaches end of non-void function 19:36 [charon@hades /tmp]$ cc -Wall -W foo.c 19:36 [charon@hades /tmp]$ ./a.out 1.663 1.466 0.970 19:36 [charon@hades /tmp]$ ./a.out 1.663 1.466 0.970 19:36 [charon@hades /tmp]$ ./a.out 1.663 1.466 0.970 - Giorgos To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020609163918.GB1595>