简体   繁体   English

我的代码给出了段错误。 我不知道如何调试,甚至不知道错误在哪里

[英]My code gives segfault. I don't know how to debug or even find where the error is

I just wrote a program to convert infix notation to postfix notation but I'm getting a seg fault. 我刚刚编写了一个将中缀表示法转换为后缀表示法的程序,但出现段错误。 This is the first time I have encountered a segmentation fault and I have no idea how to find where the problem is. 这是我第一次遇到细分错误,也不知道如何查找问题所在。 I went through the code a few times but I can't find out where my code is trying to access restricted memory space or if there is any other problem causing the seg fault (I'm not even sure what seg fault is). 我浏览了几次代码,但是我找不到我的代码试图访问受限的内存空间,或者是否存在导致seg错误的其他问题(我什至不知道是什么seg错误)。

I'm not sure if I'm allowed to paste the whole code over here. 我不确定是否可以将整个代码粘贴到此处。 So, I'm putting this up on pastebin : http://pastebin.com/M0mTU8Jg 因此,我将其放在pastebin上: http : //pastebin.com/M0mTU8Jg

Another question: In my code at line 13, if I replace strcat(")",infix) with infix[strlen(infix)]=")" , I get this warning: warning: assignment makes integer from pointer without a cast [enabled by default] . 另一个问题:在第13行的代码中,如果将strcat(")",infix)替换为infix[strlen(infix)]=")" ,则会收到以下警告: warning: assignment makes integer from pointer without a cast [enabled by default] What is the cause of this warning? 此警告的原因是什么? Similar statements later in the program do not raise any warnings for example in line no. 程序后面类似的语句不会发出任何警告,例如,在行no。 24 24

EDIT: The seg fault was because I had used wrong order of arguments in the strcat() function so it was trying to append string to a constant that is a read only memory. 编辑: seg错误是因为我在strcat()函数中使用了错误的参数顺序,因此它试图将字符串追加到作为只读内存的常量。

Nevermind the second question, I was assigning a string (char *) to a char so the warning. 没关系,第二个问题是我为char分配了一个字符串(char *),因此发出警告。 Just needed to replace double quotes with single quotes. 只需将双引号替换为单引号即可。

You do really need to know how to use a debugger and find what line your code is segfaulting on, it's an important skill. 您确实确实需要知道如何使用调试器,并找出您的代码在哪段隔离,这是一项重要技能。 That being said, I see one cause immediately: 话虽如此,我立即看到一个原因:

strcat(")",infix);

strcat(char * dest, cosnt char * src) appends to dest , and in this case dest is a string constant stored in read only memory. strcat(char * dest, cosnt char * src)附加到dest ,在这种情况下, dest是存储在只读存储器中的字符串常量。 Maybe all that is necessary is to reverse the order of the arguments to strcat() , but I don't know your intentions with the infix variable. 也许所有必要的就是将参数的顺序反转为strcat() ,但是我不知道infix变量的意图。

Can you run your code with debugger? 您可以使用调试器运行代码吗? It will show you what line seg fault will happen. 它会告诉您将发生什么线路段故障。 Here's about segfault: http://en.wikipedia.org/wiki/Segmentation_fault 这是关于段错误的信息: http : //en.wikipedia.org/wiki/Segmentation_fault

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

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