Tag: 分段 故障

不太有用的错误 – 家庭作业中的分段错误(核心转储)

编译包含此特定函数的程序时, /* * Function read_integer * * @Parameter CHAR* stringInt * * Parameter contains a string representing a struct integer. * Tokenizes the string by each character, converts each char * into an integer, and constructs a backwards linked list out * of the digits. * * @Return STRUCT* Integer */ struct integer* read_integer( char* […]

如何检测C中的变量uninitialized / catch段错误

我目前有一个程序,我需要测试作为参数传入的变量是否未初始化。 到目前为止,似乎在C中这很难做到,所以我的下一个想法是调用信号处理程序来捕获段错误。 但是,我的代码在尝试访问未初始化的变量时没有调用信号处理程序,如下所示: void segfault_sigaction(int signal, siginfo_t *si, void *arg) { printf(“Caught segfault at address %p\n”, si->si_addr); exit(0); } void myfree(void*p, char * file, int line){ struct sigaction sa; memset(&sa, 0, sizeof(sigaction)); sigemptyset(&sa.sa_mask); sa.sa_sigaction = segfault_sigaction; sa.sa_flags = SA_SIGINFO; sigaction(SIGSEGV, &sa, NULL); char up = *((char*)p); //Segfault 编辑:在Linux系统上

为什么我的悬空指针不会导致分段错误?

我的代码: #include #include int main(void) { int *p = (int *)malloc(sizeof(int)); free(p); *p = 42; return 0; } 我创建了一个指针,然后我将它指向分配的空间,最后我给它分配了42。 在我看来它不应该工作,它应该导致分段错误,但它的工作原理。 所以为什么? PS:我通常在Linux上用Gcc编译它

分段错误和链接列表的未知问题

所以我正在尝试编写一个机械程序。 程序很长但是这里有导致我问题的函数: recherche_noe和creation_noe 。 不需要打扰其余的。 这是法语所以请耐心lst_noe我的想法是这样的:首先在main我向用户询问lst_noe的noe lst_noe (这是noe列表)。 使用creation_noe他在向用户询问结构信息时做出了这一点。 最后recherche_noe返回我正在寻找的noe 。 所有信息都存储在struct maillage ,你有其他结构。 谢谢您的帮助。 #include #include #include /*==============================================*/ /* Déclaration des structures */ /*==============================================*/ struct matrice { char nom[20]; int n,m; double **tab; struct matrice *next; }; struct element { int num; int n1, n2; double k; struct element *next; }; struct noeud { […]

string和int的串联导致C中的分段错误

我不确定我做错了什么。 我正在尝试将hostname与pid连接以创建id 。 char *generate_id(void) { int ret; char id[1048]; char hostname[1024]; pid_t pid = getpid(); //hostname[1023] = ‘\0’; if ((ret = gethostname(hostname,1024) < 0)) { perror("gethostname"); exit(EXIT_FAILURE); } sprintf(id, "%s%d", pid); printf("hostname is %s\n", hostname); printf("The process id is %d\n", pid); printf("The unique id is %s", id); return id; } 编辑: 阅读一些答案后更新了代码: char *generate_id(void) […]

我有一个分段错误,但我没有找到oO?

它应该与合并排序。 合并和排序合并有两个function。 一些未知的函数(从文件和打印数组中读取数组)在输入文件中完全起作用。 Valgrind向我展示了失败是在array2的分配以及它在void合并的第3个while循环中的读写时。 void merge(int* array, int start, int middle, int end) { int size = end – start + 1; int *array2 = malloc(size*sizeof(array2)); int k = start; int m = middle + 1; int i = 0; int j = 0; while ( k <= middle && m <= end ) { […]

在C中拆分带分隔符的字符串 – 分段错误,无效空闲

我写了一个简单的代码来在C中用分隔符分割字符串。 当我删除所有自由时,代码工作得很好,但会导致内存泄漏。 当我不删除自由时,它不会显示内存泄漏但会给出分段错误。什么是拧干以及如何解决? #include #include #include unsigned int countWords(char *stringLine) { unsigned int count = 0; char* tmp = stringLine; char* last = 0; const char delim = ‘/’; while (*tmp) { if (delim == *tmp) { count++; last = tmp; } tmp++; } return count; } char **getWordsFromString(char *stringLine) { char** sizeNames = 0; […]

即使它分配的内存少于ulimit限制的内存,Proc也会崩溃

我通过ulimit -s 2000设置堆栈大小为2000Kb,为硬限制设置ulimit -Ss 2000。 在下面的程序中,我已经分配了appox 2040000(510000 x 4)个字节,这个字节小于我的限制即。 2048000(2000 * 4)字节,但我看到我的程序崩溃了! 任何人都可以建议为什么会这样。 #include #include int main() { int a[510000] = {0}; a[510000] = 1; printf(“%d”, a[510000]); fflush(stdout); sleep(70); } 编辑1:崩溃不是因为数组索引超出限制,因为我尝试降低索引并仍然崩溃。 只有当我通过ulimit限制时才会发生这种情况。

Fortran / C混合:如何在Fortran中访问动态分配的C数组?

我目前遇到内存问题:我有一个用Fortran编码的主程序,它调用C / C ++子程序来执行某些任务并将数据存储在动态分配的数组中。 问题是我需要在返回Fortran主程序时访问这些数据。 我试图在fortran中声明一个C指针(TYPE(C_PTR))指向数组,但它似乎不起作用。 该数组存在于C子例程中,但是当我回到Fortran主程序时尝试访问它时会出现段错误。 我在这里提供我的代码,任何想法? 谢谢你的帮忙 !! Fortran语言: PROGRAM FORT_C use iso_c_binding IMPLICIT NONE interface subroutine call_fc(pX,s) bind(C,name=’call_fc_’) import integer(c_int) :: s type(c_ptr), pointer :: pX end subroutine end interface integer(c_int) :: i integer(c_int) :: s integer(c_int), pointer :: X(:) type(C_ptr), pointer :: pX s=100 call call_fc(pX,s) call c_f_pointer(pX,X,(/s/)) ! This here […]

使用TQLI算法的特征值计算失败,出现分段错误

我试图使用我从南加州大学CACS网站获得的TQLI算法计算特征值。 我的测试脚本如下所示: #include int main() { int i; i = rand(); printf(“My random number: %d\n”, i); float d[4] = { {1, 2, 3, 4} }; float e[4] = { {0, 0, 0, 0} }; float z[4][4] = { {1.0, 0.0, 0.0, 0.0} , {0.0, 1.0, 0.0, 0.0} , {0.0, 0.0, 1.0, 0.0}, {0.0, 0.0, 0.0, […]