简体   繁体   English

总线10错误与递归调用

[英]Bus 10 error with recursive call

So I'm having trouble with this recursive function call... I am trying to program a function that will check to see if every string in my struct follows alphabetical order, and if it is true, it will return 1, if not, it will return 0. 因此,我在使用此递归函数调用时遇到了麻烦...我正在尝试编写一个函数,该函数将检查结构中的每个字符串是否都遵循字母顺序,如果为true,则返回1,否则返回1。它将返回0。

I keep getting a bus 10 error, and I have tried putting print statements/cannot find where my error is. 我一直遇到10总线错误,并且尝试将打印语句/找不到错误所在。 Can anyone help? 有人可以帮忙吗? Thank you. 谢谢。

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

typedef struct string_list string_list;
struct string_list {
  char *val;
  string_list *next;
};

int sl_sorted_asc(string_list *ss) {

 int  string_c = strcmp(ss->val, ss->next->val);
 if (string_c >= 0) {
   if (ss->next != NULL) {
   sl_sorted_asc(ss->next);
   }
   else {
     return 1;
   }
 }
 else  {
   return 0;
 }
 return 1;
}

int main() {

  string_list hi;
  hi.val = "Leeho";
  hi.next->val = "Ferris";
  hi.next->next->val = "Donny";

printf("%d\n", sl_sorted_asc(&hi));

}

It may be because in this line of your sl_sorted_asc function 可能是因为在sl_sorted_asc函数的这一行中

 int  string_c = strcmp(ss->val, ss->next->val);

You are not first checking to see if ss->next is null or not. 您不是先检查ss-> next是否为null。

(compulsory link to http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ ) (强制链接到http://ericlippert.com/2014/03/05/how-to-debug-small-programs/

To figure out problems like this, there's a program called valgrind which is simply invaluable (as in, if your OS doesn't support it, get a virtual machine NOW!); 为了找出类似的问题,有一个名为valgrind的程序非常有价值(例如,如果您的操作系统不支持,请立即获取虚拟机!); eg in this case it reports ==3637== Use of uninitialised value of size 8 ==3637== at 0x4005BE: main (bus10.c:33) 例如,在这种情况下,它报告==3637== Use of uninitialised value of size 8 ==3637== at 0x4005BE: main (bus10.c:33)

Line 33 is "hi.next->val = ..."; 第33行是“ hi.next-> val = ...”; the problem (in this line - is it the only place?) is that, well, you never did "hi.next = ...", so hi.next points to... what? 问题(在行-是唯一的地方吗?)是,好吧,您从未执行过“ hi.next = ...”,所以hi.next指向...是什么? Basically, anything - the moment the stack is grown to make room for the hi variable, hi.next will take whatever bits were in there, meaning it can point to just about anything (including itself, memory addresses that do not exist- you name it). 基本上,任何东西-在堆栈增长为hi变量腾出空间的那一刻,hi.next都会占用其中的任何位,这意味着它可以指向几乎任何东西(包括它本身,不存在的内存地址-您可以命名)它)。

TLDR: never, ever use a variable you did not initialize (unless you want expert C programmers to lecture you on "undefined behavior" & friends) TLDR:永远, 永远使用(除非你想专家C程序员来教训你的“未定义行为”和朋友),你没有初始化变量

the code is referencing memory that is not allocated 该代码正在引用未分配的内存

for instance in the following lines: 例如以下几行:

// allocates one instance of the struct on the stack
string_list hi;    

// sets the 'val' field of the instance of the struct on the stack
hi.val = "Leeho";    

// follows a non-initialized pointer, 
// in the instance of the struct on the stack,
// to a non allocated instance of the struct
// then writes on that non allocated instance of the struct
// I.E. writes to random memory address
hi.next->val = "Ferris";   

// follows a non-initialized pointer
// contained in a non allocated instance of the struct
// to a non allocated instance of the struct
// then writes on that non allocated instance of the struct  
// I.E. follows random values in memory 
//      to some random location in memory
//      then writes on an offset to that random location           
hi.next->next->val = "Donny";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM