Tag: 分段 故障

分段错误p_thread可能存在竞争条件

问题:我创建了一个子线程TIDS的链接列表,并希望在继续主线程之前等待所有子tid完成执行。 基本上我有目录遍历(目录由给定struct的成员指定)。 每当我看到一个目录或文件时,我都会创建一个新线程并将其threadID放入链表中。 但是,当我遍历链表并调用pthread_join我得到一个分段错误(核心转储) – 我无法理解为什么。 我相信可能与种族状况有关,但我不确定。 当我删除pthread_join我不再是段错误但是我没有完全遍历目录中的目录。 程序: void *handy(void *arguments) { static pthread_t threadA; threadA=pthread_self(); handarg *thestruct=(handarg*)arguments; DIR *dp=opendir(thestruct->directory); DIR *outExist=opendir(thestruct->outputdirectory); if(outExist==NULL) { if((thestruct->outputdirectory = “”)) { ; } else { printf(“The outputdirectory doesnt exist, program terminated\n”); exit(1); } } struct dirent *entry; // has two important members (d-type)-tells me if its a […]

递归引起的分段错误

我正在编写一个程序,要取1-10之间的数字,并显示所有可能的方法来安排数字。 防爆输入:3输出: 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 每当我输入9或10时,程序会产生分段错误并转储核心。 我相信问题是我的递归算法被调用了太多次。 有人可以帮助指出我如何限制所需的递归调用量吗? 这是我目前的代码: void rearange(int numbers[11], int index, int num, int fact) { int temp = numbers[index]; numbers[index] = numbers[index-1]; numbers[index-1] = temp; int i; for (i = 1; i 0) // If we […]

使用stdlib的qsort()对字符串数组进行排序

一些序言:我是一名计算机工程专业的学生,​​经过3个学期的Java(直到数据结构),在C学习第一堂课。 这个问题与家庭作业有关,但是为我解决了几个步骤。 我有一个输入文件,我读入内存,它存储在char [9] [500]。 我读入最多500个最大长度为8的字符串。我试图使用stdlib内置的qsort()函数对这个数组进行排序,并且有一些内存错误。 重要的代码片段: char data[4][500][60]; char debug[500][9]; size_t count = 0; /* initialize file, open for reading */ FILE* pUserlog; pUserlog = fopen(“userlog”,”r”); while(!feof(pUserlog)) { fscanf(pUserlog, “%9s %8s %16s”,debug[count], data[1][count], data[2][count]); fgets(data[3][count], 60, pUserlog); count++; } 本节将数据读入数组。 这部分感兴趣的数组是“debug”。 这是上面指定的数组。 这是我的qsort比较函数: int compare(const void* a, const void* b) { const char **ia […]

递归main() – 为什么会出现段错误?

为什么以下程序会出现段错误? int main() { main(); } 即使它是一个没有结束的递归,因此根据定义也是无效的,我不明白它为什么是segfaults(gcc 4.4.3和clang 1.5(trunk))。

C:如何将双指针传递给函数

当我将双指针传递给函数以初始化内存时,我遇到了分段错误 int main() { double **A; initialize(A, 10, 10); …… } void initialize(double **A, int r, int c) { A = (double **)malloc(sizeof(double *)*r); for(int i = 0; i< r; i++) { A[i] = (double *)malloc(sizeof(double) *c); for(int j = 0; j < c; j++) { A[i][j] = 0.0; } } } 如何将双指针传递给函数…..

复制内存时为什么分段出错

我在x86_32上运行ubuntu …并且在运行此程序时我一直在分段错误。 enter code here #include #include char *memcp(char *dest, const char *src, size_t n) { char *dp = dest; const char *sp = src; while(n–) *dp++ = *sp++; return dest; } int main() { char *s = “abcde”; char *d; char *r = memcp(d,s,6); printf(“%s”,r); return(0); } 这段代码的问题在于它在我朋友的x86_64机器上运行在windows和ubuntu上。 请帮帮我..

分段错误

可能重复: btree实现中的分段错误 我得到这样的错误,我该怎么调试呢? 你能给一些方法来调试这个错误: *** glibc detected *** ./a.out: free(): invalid pointer: 0x0821b158 *** ======= Backtrace: ========= /lib/tls/i686/cmov/libc.so.6(+0x6b591)[0xbd7591] /lib/tls/i686/cmov/libc.so.6(+0x6cde8)[0xbd8de8] /lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0xbdbecd] ./a.out[0x80490c3] ./a.out[0x8048bdc] ./a.out[0x8048642] /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0xb82bd6] ./a.out[0x80484b1] ======= Memory map: ======== 004e5000-00500000 r-xp 00000000 08:06 1192669 /lib/ld-2.11.1.so 00500000-00501000 r–p 0001a000 08:06 1192669 /lib/ld-2.11.1.so 00501000-00502000 rw-p 0001b000 08:06 1192669 /lib/ld-2.11.1.so 007aa000-007c7000 r-xp 00000000 08:06 1179731 /lib/libgcc_s.so.1 007c7000-007c8000 r–p […]

访问超出内存后,分段错误不会立即出现

我编写了这段代码并且期待一个段落故障,但似乎我被允许访问我不应该访问的内存块。 #include int main() { int tab[1]; tab[0]=42; int i; //Expecting Seg Fault from i==1… for(i=0;;i++) { printf(“%d \t %d \n”, i, tab[i]); } return 0; } 我正在编译使用: gcc -Wall -Wextra my_code.c -o segfault && ./segfault 执行后,变量i在得到分段错误之前达到1000的值。 我的问题是:为什么我能够阅读tab到目前为止? PS:使用#include 并声明int * tab = (int*)malloc(sizeof(int)); 什么都不改变…… 谢谢,最好的。

为什么会产生分段错误?

#include void foo(int **arr) { arr[1][1]++; } main() { int arr[20][20]; printf(“%d\n”,arr[1][1]); foo((int**)arr); printf(“%d\n”,arr[1][1]); }

在堆栈上分配大型数组时出现分段错误

当我编译这个简单的C代码时它很好但是在取消注释该行之后它显示了分段错误。 我不知道这有什么问题。 请帮忙。 #include int main() { int arr[10002][10002]; int color[10002]; int neigh; // scanf(“%d”,&neigh); return 0; }