From owner-freebsd-questions Sun Jun 9 9:39:41 2002 Delivered-To: freebsd-questions@freebsd.org Received: from mailsrv.otenet.gr (mailsrv.otenet.gr [195.170.0.5]) by hub.freebsd.org (Postfix) with ESMTP id 955FA37B40C for ; Sun, 9 Jun 2002 09:39:36 -0700 (PDT) Received: from hades.hell.gr (patr530-b160.otenet.gr [212.205.244.168]) by mailsrv.otenet.gr (8.12.3/8.12.3) with ESMTP id g59GdOZN005834; Sun, 9 Jun 2002 19:39:30 +0300 (EEST) Received: from hades.hell.gr (hades [127.0.0.1]) by hades.hell.gr (8.12.3/8.12.3) with ESMTP id g59GdLvi003095; Sun, 9 Jun 2002 19:39:21 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Received: (from charon@localhost) by hades.hell.gr (8.12.3/8.12.3/Submit) id g59GdKmu003094; Sun, 9 Jun 2002 19:39:21 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Date: Sun, 9 Jun 2002 19:39:18 +0300 From: Giorgos Keramidas To: "Christopher J. Umina" Cc: FreeBSD Questions Subject: Re: Load Averages with C Message-ID: <20020609163918.GB1595@hades.hell.gr> References: <001201c20f54$46ba9e20$0301a8c0@uminafamily.com> <20020608222753.GA38586@hades.hell.gr> <001901c20fe2$9988cc60$0301a8c0@uminafamily.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <001901c20fe2$9988cc60$0301a8c0@uminafamily.com> Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG 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 > #include > > 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 #include 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