Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Segmentation fault when calling clock()

I am trying to understand the effects of caching programmatically using the following program. I am getting segfault with the code. I used GDB (compiled with -g -O0) and found that it was segmentation faulting on

start = clock() (first occourance)

Am I doing something wrong? The code looks fine to me. Can someone point out the mistake?

#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>

#define MAX_SIZE (16*1024*1024)
int main()
{
    clock_t start, end;
    double cpu_time;
    int i = 0;
    int arr[MAX_SIZE];

    /* CPU clock ticks count start */
    start = clock();

    /* Loop 1 */
    for (i = 0; i < MAX_SIZE; i++) 
        arr[i] *= 3;

    /* CPU clock ticks count stop */
    end = clock();

    cpu_time = ((double) (end - start)) / CLOCKS_PER_SEC;

    printf("CPU time for loop 1 %.6f secs.\n", cpu_time);

    /* CPU clock ticks count start */
    start = clock();

    /* Loop 2 */
    for (i = 0; i < MAX_SIZE; i += 16) 
        arr[i] *= 3;

    /* CPU clock ticks count stop */
    end = clock();

    cpu_time = ((double) (end - start)) / CLOCKS_PER_SEC;

    printf("CPU time for loop 2 %.6f secs.\n", cpu_time);

    return 0;
}
like image 552
liv2hak Avatar asked Jun 16 '13 22:06

liv2hak


People also ask

What is causing my segmentation fault?

A segmentation fault (aka segfault) is a common condition that causes programs to crash; they are often associated with a file named core . Segfaults are caused by a program trying to read or write an illegal memory location.

Is segmentation fault run-time error?

A common run-time error for C programs by beginners is a "segmentation violation" or "segmentation fault." When you run your program and the system reports a "segmentation violation," it means your program has attempted to access an area of memory that it is not allowed to access.

What is segmentation fault in C ++=?

Core Dump/Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you.” When a piece of code tries to do read and write operation in a read only location in memory or freed block of memory, it is known as core dump.

What causes a segmentation fault 11?

When Segmentation fault 11 occurs, it means that a program has attempted to access a memory location that it's not allowed to access. The error can also occur if the application tries to access memory in a method that isn't allowed.


2 Answers

The array might be too big for the stack. Try making it static instead, so it goes into the global variable space. As an added bonus, static variables are initialized to all zero.

Unlike other kinds of storage, the compiler can check that resources exist for globals at compile time (and the OS can double check at runtime before the program starts) so you don't need to handle out of memory errors. An uninitialized array won't make your executable file bigger.

This is an unfortunate rough edge of the way the stack works. It lives in a fixed-size buffer, set by the program executable's configuration according to the operating system, but its actual size is seldom checked against the available space.

Welcome to Stack Overflow land!

like image 65
Potatoswatter Avatar answered Oct 22 '22 19:10

Potatoswatter


Try to change:

int arr[MAX_SIZE];

to:

int *arr = (int*)malloc(MAX_SIZE * sizeof(int));

As Potatoswatter suggested The array might be too big for the stack... You might allocate on the heap, than on the stack...

More informations.

like image 44
Gauthier Boaglio Avatar answered Oct 22 '22 20:10

Gauthier Boaglio