[英]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.每个语句在堆栈上分配
1000000*sizeof(int)
字节。 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) (PS:
calloc
& Co. 出错时返回 NULL,所以检查错误)
free(T1);
free(T2);
free(T3);
As @JCWasm has pointed out that you are facing a stackoverflow issue.正如@JCWasm 指出的那样,您正面临着一个 stackoverflow 问题。 The amount of static memory (stack memory) you are requesting is simply not allowed by default .
默认情况下根本不允许您请求的静态内存(堆栈内存)量。 Hence your program is crashing.
因此你的程序崩溃了。
For your refrence:供您参考:
gcc
the default stack size is ~8MB Referencegcc
,默认堆栈大小为 ~8MB参考Windows (Visual Studio)
it is 1MB ReferenceWindows (Visual Studio)
上是 1MB 参考Now, to fix your problem there are 2 possible ways:现在,要解决您的问题,有两种可能的方法:
Solution 1: (More generic and recommended)解决方案1:(更通用和推荐)
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)解决方案2:(仅当由于某种原因无法使用动态内存时)
All compilers have some provisions to insrease the stack size.所有编译器都有一些增加堆栈大小的规定。 The above links demonstarte the same.
上面的链接演示相同。
> ulimit -s 32768 # sets the stack size to 32M bytes
> ulimit -s 32768 # sets the stack size to 32M bytes
/F
to set the stack size eg /F 32768
/F
设置堆栈大小,例如/F 32768
The other 2 answers are correct with mentioning that you should use the heap ( malloc()
, calloc()
and free()
).其他 2 个答案是正确的,提到您应该使用堆(
malloc()
、 calloc()
和free()
)。 But there is also another solution, but this is not preferred: Using static
variables.但是还有另一种解决方案,但这不是首选:使用
static
变量。 static
variables are stored in the .data or .bss section, so they do not need the stack. static
变量存储在 .data 或 .bss 部分,因此它们不需要堆栈。 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.缺点是这个变量在运行过程中只存在一次,这使得
static
变量在使用多线程和其他场景时会出现问题。
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.但是,假设
sizeof int == 2
,您不太可能获得 2 MB 或更多 RAM,在这样的系统上,总 RAM 更有可能是 64 KiB 或类似的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.