English 中文(简体)
Segmentation fault on time(0);
原标题:

I m rewriting an old program to do some new stuff, and suddenly I get a segmentation fault error on the following line of code:

time_t seconds_since_time_begun = time(0);

Why, oh why?

Update:
I have included the time.h header file in my code, and when I tried what pmg suggested below, both variables were 4 in size.

When I tried the following:

printf("4
");
// time_t seconds_since_time_begun = time(0);
printf("5
");

I still get segmentation fault right after "4" has been printed, even when the line where time() is called is commented out. "5" is not outputted. How is this possible?

Update 2:
I rebuilt my function step by step to try to isolate where the error occurred in my code, and I think I found the problem. I m not sure what was wrong but it had something to do with a comparison between an integer array value and 0. So, I don t think the segfault was caused by my call to time(0) .. but I m still not sure what actually happened. Anyways, problem solved. Thanks guys.

最佳回答

Often you will find that a segmentation fault will be caught on the incorrect line. What are the lines around it? Is it possible one of those could be causing it?

The standard library s time function take a time_t * as its argument, right? Hence, it s not > surprising you get a segfault: you re trying to dereference a NULL pointer!

time(0) or time(NULL) is the standard and accepted way to access the current time using the time function.

See reference.

问题回答

Perhaps you have an absurdly large object in your stack frame that s overflowing the stack? See this blog post for a detailed example of how that can occur and an analysis of the situation. The time(2) function allows NULL to be passed to it, and it s highly unlikely your time() implementation has a bug.

If you have #include <time.h> the compiler knows that it needs to convert that 0 to a NULL pointer. Otherwise, it passes an int to the function.

I think your implementation treats (int)0 differently than (time_t*)0.

So ... add

#include <time.h>

to your code

Edit

Try this

#include <stdio.h>
#include <time.h>

int main(void) {
    printf("size of (int): %d
", (int)sizeof (int));
    printf("size of (time_t*): %d
", (int)sizeof (time_t*));
    return 0;
}

Is it possible that your program contains a variable or local function namedtimethat s colliding with the standard library stimefunction?

As pointed out by Adam Rosenfield, a program might segfault at the point of calling just any function (not just system calls) when it s out of stack space.

Now, your program might actually do need to store huge data on the stack rather than the heap. In this case, the solution might be to change the maximum stack space for your particular environment.

On Linux, it can be changed using ulimit, e.g.

ulimit -S unlimited

Lovely, but did you intend to pass it a null pointer as time(time_t *t)?

time(NULL);
time(((void*)0));

Not a zero?





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签