簡體   English   中英

分段故障(核心轉儲)

[英]Segmentation fault (core dumped)

我是相對較新的C,我試圖運行一個簡單的程序,我得到這個錯誤消息:分段錯誤(核心轉儲)我只想打印數組位的任何值,但我不能,我會感激任何幫助關於這個錯誤。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <malloc.h>

int main()
{
    const long int N = 1000000000;
    const int smallN = 125000000;
    char bits[smallN];

    for(int i=0; i<smallN; i++){
        bits[i]=0xff;
    }

    printf("character = %c \n", bits[5]);
}

該數組似乎超出了堆棧大小(位是堆棧上的數組)。 您可以嘗試將其設置為全局或使用malloc分配數組。

請注意,這是Valgrind擅長精確定位的事情。 如果你在一大堆代碼中完成了這個,Valgrind會指出你錯誤的那一行。

對於在這個時代學習C語言,Valgrind是不可或缺的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM