Tag: 分段故障

由于在更改巨大页面分配的大小时initialize()函数导致的分段错误

当我使用大页面分配的内存大小时,我在程序中遇到分段错误,即,当我定义LENGTH = 4 * 1024时,存在seg错误。 当我定义4 * 1024 * 1024时,没有seg错误。 这是什么原因? 代码如下: #define _POSIX_C_SOURCE 199309 #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #define PROTECTION (PROT_READ | PROT_WRITE) #define LENGTH (4*1024) //#define LENGTH (4*1024*1024) #define LINE_SIZE 64 #define ASSOC 16 #define CACHE_SIZE (4*1024*1024) #define WAY_SIZE (CACHE_SIZE/ASSOC) #ifndef MAP_HUGETLB #define MAP_HUGETLB 0x40000 #endif […]

C中的scanf和字符串的分段错误

我是c的初学者,我遇到了scanf和字符串的问题。 这是我写的问题的一个例子。 #include #include int main(void) { char* string; scanf(“%s”, &string); if (strcmp(string, “Foo”) == 0) //segmentation fault here printf(“Bar”); } 基本上,这个代码编译,但是当我运行它时,我在strcmp()中得到一个分段错误 如果我用“&string”替换该行中的“string”,它可以工作,但是我从编译器中得到了这个错误 /usr/include/stdio.h:362:12: note: expected ‘const char * __restrict__’ but argument is of type ‘char **’ 这让我觉得这个解决方案并不是很理想。 如果我声明这样的字符串: char string[100]; 它没有任何警告,但这也不理想,因为我不确定字符串的大小。 有没有更好的解决方案我在这里缺少,或者这些是我唯一的选择? 谢谢。

扫描期间C中的分段错误

我试图扫描一个整数用于我的程序。 但是我的程序在编译期间给了我分段错误,这是给我错误的部分: int main(void) { int totalHeight=0, floorWidth=0, amountOfStories, amountWindowForTop, amountWindowForMiddle, amountWindowForBottom, windowHeight, middleWindowWidth, topWindowWidth, bottomWindowWidth, minimumHeight, minimumWidth; char topFloorWindowContent, middleFloorWindowContent, bottomFloorWindowContent, windowBorder, floorBorder; int tempMax; printf(“please enter how many stories your building would like to have: “); scanf(“%d”,&amountOfStories); minimumHeight=amountOfStories*6+1; while((totalHeight<minimumHeight)||((totalHeight%amountOfStories)!=1)) { printf("please enter the totalHeight (minimum %d): ",minimumHeight); scanf("%d",&totalHeight); } printf("please enter how many […]

扫描后的SegFault?

#include #define TimeConverter 60 #define TempFormula time * time * 4 / time + 2 – 20 double HoursMinToTime(int hour, int min); double Temperature(double time); int main() { int hour, min; double time, temperature; printf(“Hours and minutes: “); scanf(“%d %d”, hour, min); //Segfault HERE time = HoursMinToTime(hour, min); temperature = Temperature(time); printf(“After a %lf hour […]