Tag: 分段 故障

程序接收信号SIGSEGV,分段故障。 调试时

如果我调试我的代码然后我得到“程序接收信号SIGSEGV,分段错误。” 这是我的代码 – #include #include #include int main() { struct term { char* name; long int id; float term_gpa; }; struct term *term_ptr, student; term_ptr = &student; strcpy( term_ptr->name,”niton”); term_ptr->id = 942044; term_ptr->term_gpa = 3.75; printf(“Name : %s”,term_ptr->name); printf(“Name : %s”,student.name); getch(); return 0; } 我在第17行收到此错误。请帮帮我! 对不起,我的英语不好。

重新初始化arrays是否会产生段错误?

我正在对输入文件格式(u,v,weight)的大量图形数据运行宽度优先搜索和贝尔曼福特算法。 我在广度优先搜索中初始化,所有顶点都应标记为0以供未访问。 后来在程序中,因为我每次添加边缘后都会调用BFS,而不是在程序结束时(这是关于Bellman ford和BFS的研究项目的一部分,即使它没有多大意义)我将顶点数组重新初始化为unvisited。 但是,当我重新初始化顶点数组时运行更大的集合时,我遇到了分段错误。 我假设有更大的集合,因为我有一些较小的测试数据集,从8个顶点到10,然后在100和更大,它失败了。 以下是我在程序开始时初始化的方法: for(i=0;i<numberVertices;i++) { vertexArray[i].v = i+1; vertexArray[i].adj = NULL; vertexArray[i].marked = 0; if(i==0) { vertexArray[i].weight = 0; } else{ vertexArray[i].weight = 10000; } } 以下是我在BFS结束后直接到达文件末尾时,在while循环结束时重新初始化的方式: BFS(q, u) for(i=0;i<numberVertices;i++) { vertexArray[i].marked = 0; } 就像我说它似乎适用于较小的数据集,但我不明白为什么当我重新初始化它似乎是错误的。 请让我知道你的想法和建议! Valgrind输出示例案例: ==6634== Memcheck, a memory error detector ==6634== Copyright (C) 2002-2010, and GNU GPL’d, […]

试图在循环中访问数组元素会导致分段错误,为什么?

我正在尝试创建一个二维数组,其中每个坐标都随机分配1或0。 它工作得很好,直到它到达坐标[20] [3]。 之后,它只是抛出“分段错误11”。 我绝对不知道怎么或为什么。 特别是因为我可以创建一个200 * 200的矩阵,但它仍然只在坐标[200] [3]处得到相同的问题。 所以它总是在最后一个x坐标中出现错误的第三个y坐标。 #include #include int main() { int x, y, i, j ; x = 20; y = 20; int grid [x][y]; for ( i = 0; i <= x; i++) { for ( j = 0; j <= y; j++) { grid[i][j] = rand() % 2 […]

c分段故障fgets

int main( int argc, char** argv) { FILE *inFilePtr = fopen(*(argv + 1), “r”); char *rawdata = malloc(sizeof(char) * 100); float *ary = malloc(sizeof(float) * 50); int counter = 0; float averageAns; int count = 0; while (count < 1 ){ //fgets(rawdata, 50, inFilePtr); //I have tried both fscanf(inFilePtr, "%s", rawdata); *(ary + counter) = […]

使用strcpy,malloc和struct 时,我有一个分段错误(核心转储)

好的,当我运行此代码时,我有一个分段错误: #include #include #include #define MAX 64 struct example { char *name; }; int main() { struct example *s = malloc (MAX); strcpy(s->name ,”Hello World!!”); return !printf(“%s\n”, s->name); } 终端输出: alshamlan@alshamlan-VGN-CR520E:/tmp/interview$ make q1 cc -Wall -g q1.c -o q1 alshamlan@alshamlan-VGN-CR520E:/tmp/interview$ ./q1 Segmentation fault (core dumped) alshamlan@alshamlan-VGN-CR520E:/tmp/interview$ gedit q1.c 有人可以解释发生了什么吗?

cython分段故障处理

我正在包装一些C库,我有一个function,在某些情况下可能导致分段错误。 在这种情况下,我需要调用第二个函数,在这种情况下将成功完成。 有谁知道如何处理cython中的分段错误?

写出比malloced更多的字符。 为什么不失败?

为什么以下工作并没有抛出某种分段错误? char *path = “/usr/bin/”; char *random = “012”; // path + random + \0 // so its malloc(13), but I get 16 bytes due to memory alignment (im on 32bit) newPath = (char *) malloc(strlen(path) + strlen(random) + 1); strcat(newPath, path); strcat(newPath, “random”); // newPath is now: “/usr/bin/012\0” which makes 13 characters. 但是,如果我添加 strcat(newPath, […]

为什么缓冲区溢出会在访问整数时导致分段错误?

在从函数A()调用函数B()期间,B()分配一个100-char数组并多次填充,包括一次使用101个字符的字符串,一次使用110个字符的字符串。 这是一个明显的错误。 之后,函数A()尝试访问完全不相关的int变量i,并发生分段错误。 我理解为什么会发生缓冲区溢出,但为什么在访问此整数时会出现分段错误? 为什么我不简单地得到垃圾数据?

调用fgets时出现分段错误

我正在尝试编写一个简单的程序来读取纯文本列表中列出的文件中的数据,但是当我尝试在我的processFile函数中调用fgets()时,我一直遇到分段错误。 如果我只是调用processFile(“file.txt”)这样的东西就不会发生,但是当我尝试通过我的processList函数调用processFile时就会发生这种情况。 #include #include #include void processFile (char *file) { char line[256]; FILE* pgmFile; pgmFile = fopen(file, “r”); fgets(line, 200, pgmFile); // Seg fault here fclose(pgmFile); } // Runs processFile on every file listed in list void processList (char *list) { FILE *pgmList; pgmList = fopen(list, “r”); char line[256]; while (fgets(line, 255, pgmList) != NULL) […]

从C中的函数内部分配字符串数组

我有一个函数扫描文件并返回行数和字符串数组中的行,我的函数如下所示: int load_lines(char* _file, char** _array){ FILE *infile; char line_buffer[BUFSIZ]; char line_number; infile = fopen(_file, “r”); ine_number = 0; while (fgets(line_buffer, sizeof(line_buffer), infile)) ++line_number; fclose(infile); _array = malloc (line_number * sizeof(char*)); infile = fopen(_file, “r”); line_number = 0; while (fgets(line_buffer, sizeof(line_buffer), infile)) { _array[line_number] = malloc(strlen(line_buffer) + 1); strcpy(_array[line_number], line_buffer); //a printf on _array[line_number] works […]