Tag: segmentation fault

在`libpmpi.12.dylib`中使用MPI_Barrier时出现分段错误

我使用brew install mpich ,但是如果我使用MPI_Barrier ,我会得到分段错误。 请参阅以下简单代码: // Ac #include “mpi.h” #include int main(int argc, char *argv[]) { int rank, nprocs; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&nprocs); MPI_Comm_rank(MPI_COMM_WORLD,&rank); MPI_Barrier(MPI_COMM_WORLD); printf(“Hello, world. I am %d of %d\n”, rank, nprocs);fflush(stdout); MPI_Finalize(); return 0; } mpicc Ac -g -O0 -o A 运行mpirun -n 2 ./A ,我收到以下错误: ================================================================================== = = BAD TERMINATION OF ONE […]

C:合并 – 对具有不均匀元素数的数组进行排序

我一直在为我的程序编程类工作,我们提供了一个不能完全运行的合并排序程序。 它对具有偶数个整数的数组执行合并排序,但会抛出具有奇数个整数的分段错误。 我理解排序是如何工作的,并且因为奇数导致分段错误而导致分段错误,因为数组以某种方式被过度填充。 我也理解该解决方案将涉及测试原始数组是偶数还是奇数,然后根据这个不同地将值传递给合并函数。 尽管我对这个项目有所了解,但是我几个星期以来一直试图让这个项目正常运行,我希望有人可以给我一些建议。 在发布之前我已经做了很多寻找答案,但所有其他的例子都涉及到结构的合并排序程序,这超出了我迄今为止学到的内容。 您将在我在下面发布的代码中看到。 此外,完整的程序涉及一些其他文件,但我只包括mergesort.c文件和merge.c文件,正如我的教授所保证的那样,这是唯一需要进行任何更改的地方。 main文件工作正常,只负责填充数组并调用mergesort函数。 如果其他文件是必要的,请告诉我,我会发布它们。 我没有的唯一原因是因为我们使用的是Linux shell,而且我还没有找到一种将shell中的代码复制并粘贴到我自己的操作系统的实用方法,并且需要一段时间才能将其写出来。 提前感谢您提供的任何指示。 这是代码。 mergesort.c #include void mergesort(int key[], int n) //key is the array, n is the size of key { int j, k, m, *w; w = calloc(n, sizeof(int)); assert(w != NULL); for (k = 1; k < n; k *= 2) { […]

g_slice_alloc中的段错误

我正在使用以下行调用函数: void call_system_command(const char *command_params) { GString *cmd = g_string_sized_new(1024); g_string_append_printf(cmd, “/bin/bash /path/to/my/script ‘%s'”, command_params); system(cmd->str); g_string_free(cmd, TRUE); } 我正在使用g_string_sized_new获得段错误。 来自gdb的Backtrace显示: (gdb) bt #0 0x000000320ce56264 in g_slice_alloc () from /lib64/libglib-2.0.so.0 #1 0x000000320ce5c3db in g_string_sized_new () from /lib64/libglib-2.0.so.0 …. 我已经尝试导出G_SLICE = always-malloc,因此不使用glib自己的分配器,而是使用malloc。 但问题仍然存在。 我仍然在g_slice_alloc中遇到段错误。 我也从多个线程调用此函数’call_system_command’。 这可能是个问题吗? 该函数是插件的一部分,每15分钟由cron调用。 每次执行插件时都不会发生段错误,但每3-4天就会发生一次。 有关进一步调试的任何指示都会有所帮助。 提前致谢。

在struct中访问数组元素时出错

我正在尝试编写一个“ArrayList”程序(类似于Java ArrayList ),它将使用realloc自动扩展,这样程序员就不必担心数组中的存储空间。 这是我的代码: #include #include #include #include #define ARR_DEFAULT_SIZE 20 #define INCR 10 #define ARRTYPE char // Files using this #undef this macro and provide their own type typedef struct { ARRTYPE *arr; long size; long nextincr; } arrlst; arrlst *nlst(void); void add(arrlst *, ARRTYPE); ARRTYPE elmat(arrlst *, long); int main(int argc, char **argv) […]

C表示循环分段故障

当我尝试在gcc上编译此代码时,我收到了分段错误错误。 #include #include #define N_TIMES 600000 #define ARRAY_SIZE 10000 int main (void) { double *array = calloc(ARRAY_SIZE, sizeof(double)); double sum = 0; int i; double sum1 = 0; for (i = 0; i < N_TIMES; i++) { int j; for (j = 0; j < ARRAY_SIZE; j += 20) { sum += array[j] + array[j+1] […]

为什么这个程序不是段错误?

当我为gcc启用-O时,导致输出“Hello”的原因是什么? 它不应该仍然是段错(根据这个维基 )? % cat segv.c #include int main() { char * s = “Hello”; s[0] = ‘Y’; puts(s); return 0; } % gcc segv.c && ./a.out zsh: segmentation fault ./a.out % gcc -O segv.c && ./a.out Hello

分段引用char数组时出错

#include int main(void) { char * p= “strings are good”; printf(“%s”,*p); return 0; } 有人可以告诉我为什么我在这里得到分段错误?

不可能发生了! 这是什么意思?

我遇到了一个有趣的运行时错误。 我认为这是某种内存泄漏。 我写了以下程序: C代码: #include #include #include #include #include #define PRECISION 4096 #define DECIMAL_POINT 1 #define NULL_TERMINATOR 1 void gatherSquares(const uint32_t limit, uint32_t ** const arr, uint32_t * const count); uint32_t inList(const uint32_t n, const uint32_t * const arr, const uint32_t count); void print_help(const char * const str); int main(int argc, char* argv[]) { uint32_t […]

为什么我会出现分段错误?

我正在尝试编写一个程序,它接收一个明文文件作为它的参数并解析它,将所有数字加在一起,然后打印出总和。 以下是我的代码: #include #include #include static int sumNumbers(char filename[]) { int sum = 0; FILE *file = fopen(filename, “r”); char *str; while (fgets(str, sizeof BUFSIZ, file)) { while (*str != ‘\0’) { if (isdigit(*str)) { sum += atoi(str); str++; while (isdigit(*str)) str++; continue; } str++; } } fclose(file); return sum; } int main(int argc, char […]

初始化2Darrays时出现分段错误

我已经检查过我的代码正确地划分了内存空间,但是一旦我尝试将我的2D数组初始化为某些值然后总结这些值,我就会在2×2数组上收到分段错误。 我想最终将我的代码扩展到更大的数组,但我甚至无法在这里工作。 我知道关于malloc和2D数组有很多关于分段错误的post,但由于我的C知识刚刚开始,我一直无法找到一个帮助我解决问题的post。 您可以给予任何帮助,或者如果您能指出我之前的问题,将不胜感激。 谢谢! #include #include #include int main() { double sum=0; int i,j; int N = 2; double **array; array = malloc(N * sizeof(double *)); if(array == NULL) printf(“Failure to allocate memory.\n”); for(i=0; i<=N; i++) { array[i] = malloc(N * sizeof(double)); if(array[i] == NULL) { printf("Failed to allocate memory for arr[%d].\n", i); exit(0); […]