Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 28 Sep 1999 21:21:13 -0400 (EDT)
From:      James Snow <sno@teardrop.org>
To:        freebsd-questions@freebsd.org
Subject:   Weird segfault
Message-ID:  <Pine.BSF.3.96.990928211524.52235A-100000@silver.teardrop.org>

next in thread | raw e-mail | index | archive | help


I didn't write this code, but I was looking over it for someone else and I
can't figure out for the life of me why it segfaults.

My experience with Unix has led me to believe that whenever something
isn't working, I'm doing something wrong. :) So, I was wondering if
someone could point out for me what's gone wrong here:

This code does not work:

----
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/sysctl.h>

int uptime()
{
	int mib[1];
	int utsec, utday, uthour, utmin;
	size_t len;
	
	mib[0] = CTL_KERN;
	mib[1] = KERN_BOOTTIME;
	len = sizeof(utsec);
	sysctl(mib, 2, &utsec, &len, NULL, 0);
	utsec = time(NULL) - utsec;

	utday = utsec / 86400;
	utsec %= 86400;
	
	uthour = utsec / 3600;
	utsec %= 3600;
	
	utmin = utsec / 60;
	utsec %= 60;
	
	return printf("%dd %dh:%dm:%ds\n", utday, uthour, utmin, utsec);
}

int loadavg() {
	double avg[2];
	if ((getloadavg(avg, 3)) == -1)
	 	return printf("Not available\n");

	return printf("%.2f %.2f %.2f\n", avg[0], avg[1], avg[2]);
}

int main() {
	printf("a:");
	uptime();
	printf("b:");
	loadavg();
	printf("c:\n");
	return 0;
}
----

But this code does work:

------
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/sysctl.h>

int main() {
        int mib[1];
        int utsec, utday, uthour, utmin;
        size_t len;
        double avg[2];

        mib[0] = CTL_KERN;
        mib[1] = KERN_BOOTTIME;
        len = sizeof(utsec);
        sysctl(mib, 2, &utsec, &len, NULL, 0);
        utsec = time(NULL) - utsec;
        utday = utsec / 86400;
        utsec %= 86400;
        uthour = utsec / 3600;
        utsec %= 3600;
        utmin = utsec / 60;
        utsec %= 60;
        printf("%dd %dh:%dm:%ds\n", utday, uthour, utmin, utsec);

        if ((getloadavg(avg, 3)) == -1)
                printf("Not available\n");
        else
                printf("%.2f %.2f %.2f\n", avg[0], avg[1], avg[2]);

        return 0;
}
----

The first bit of code segfaults when it returns from the function
loadavg(). Not at or after the call to printf, (try inserting additional
calls to printf afterwards to see this) but specifically when it returns.

What am I missing here?

Thanks in advance,
-sno

          o-( sno at teardrop dot org - I am Geek. Hear me ^G )-o
          |  CBFC 702E 0F86 29C6 4386 16DD 6B0B 0D56 D77A 5CD5  |
          |  8B24 4914 605E BB0E 3E10 4A6C 261F 0BA0 386D 032A  |
          o-( We live in the short term and hope for the best )-o



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?Pine.BSF.3.96.990928211524.52235A-100000>