1

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);
}
5
  • have you tried your program with shorter arrays? Like int T1[50]; ecc.. Commented Sep 10, 2020 at 14:57
  • 1
    At first glance, you are using up all the memory on the "Stack". You should look into allocating memory on the "Heap". Look into allocating memory on the heap in C. Commented Sep 10, 2020 at 15:02
  • @Burkinabé Automatic variables will be allocated on stack, when you have large automatic arrays, the stack will grow by a huge amount and cause a stack overflow. You could try malloc() and similar functions or use static variables. Commented Sep 10, 2020 at 15:03
  • I tested it, and it is working with shorter arrays. What if I want to use those sizes? Commented Sep 10, 2020 at 15:06
  • You should use size_t for array indexing and not int. Your code break when INT_MAX<1000000 (even when you would use size_t). Commented Sep 11, 2020 at 12:10

3 Answers 3

6

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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 e.g. /F 32768

Comments

0

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.

4 Comments

Nice solution. Just that, with this the size of the exe will also grow. But that should be acceptable given that we want to have it in the most unusal ways
@Sisir Tested it with gcc on Linux Debian AMD64, did not see a significant change in the elf size. Probably because the array can be in bss and does not need to initialized with something other than 0. Do you see an increase in Windows (assuming you use Windows because you mention exe)?
Interestingly, if I dont initialize anything to the arrays or intialize them with 0s then the size stays at 46KB. But if i initialize that with some non-zero values then the size bumps to ~14MB. The reason is that in the first case they are stored in BSS but in the second it is stored in Data Segement
@Sisir The compiler probably stores the data for the complete array as soon as you set at least one element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.