简体   繁体   中英

Segmentation fault (core dumped) with array

Having a problem with my code. I have made some research about the problem, but the solution I found seems not be solve my problem. Most of the solutions said that it is due to array out of index. Here is my code.

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


int myrandom(int min, int max){
   return min + rand() / (RAND_MAX / (max - min + 1) + 1);
}

int main() {
    clock_t begin = clock();
    int T1[1000000];
    int T2[1000000];
    int T3[1000000];
    
    for (int i = 0; i < 1000000; i++) {
        T1[i] = myrandom(-10, 10);
        T2[i] = myrandom(-10, 10);
        T3[i] = T1[i]*T2[i];//<-- Getting Segmentation fault (core dumped) here
    }
    
    
    
    
    clock_t end = clock();
    double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    printf("Program terminé en %f Secondes\n", time_spent);
}

You overflow your stack.

int T1[1000000];
int T2[1000000];
int T3[1000000];

Each of this statements allocates 1000000*sizeof(int) bytes on the stack. This leads to a stack overflow and corruption of other, unrelated memory.

Try using dynamic allocation:

int* T1=calloc(1000000,sizeof(int));
int* T2=calloc(1000000,sizeof(int));
int* T3=calloc(1000000,sizeof(int));

And then free it after use. (PS: calloc & Co. return NULL on error, so check the error)

free(T1);
free(T2);
free(T3);

As @JCWasm has pointed out that you are facing a stackoverflow issue. The amount of static memory (stack memory) you are requesting is simply not allowed by default . Hence your program is crashing.

For your refrence:

  • On gcc the default stack size is ~8MB Reference
  • On Windows (Visual Studio) it is 1MB Reference

Now, to fix your problem there are 2 possible ways:

Solution 1: (More generic and recommended)

Use dynamic memory:

int* T1=malloc(1000000*sizeof(int));
int* T2=malloc(1000000*sizeof(int));
int* T3=malloc(1000000*sizeof(int));

Solution 2: (Only if you cannot use dynamic memory for some reason)

All compilers have some provisions to insrease the stack size. The above links demonstarte the same.

  • For gcc: > ulimit -s 32768 # sets the stack size to 32M bytes
  • For Visual Studio: Use compiler flag /F to set the stack size eg /F 32768

The other 2 answers are correct with mentioning that you should use the heap ( malloc() , calloc() and free() ). But there is also another solution, but this is not preferred: Using static variables. static variables are stored in the .data or .bss section, so they do not need the stack. The disadvantage is that this variables exist only once in the running process this makes static variables problematic when you use multi threading and in other scenarios.

Prefer the heap when you can, but there are some freestanding environments that may do not have a heap. But there you are less likely to get 2 MB or more RAM, assuming sizeof int == 2 , on such a system the total RAM is more likely to be 64 KiB or something like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM