[英]my Program received signal SIGSEGV, Segmentation fault, help please
When I try to run this program, it doesn't work.当我尝试运行此程序时,它不起作用。 When I go to debugs, a message pops up "program received signal sigsegv, segmentation fault"
当我去调试时,弹出一条消息“程序收到信号sigsegv,分段错误”
i debug the programe with gdb, and this is the messages.我用 gdb 调试程序,这是消息。 Program received signal SIGSEGV, Segmentation fault.
程序收到信号 SIGSEGV,分段错误。 0x0000555555555195 in ft_atoi (str=0x555555556004 "111") at ft_atoi.c:33 33 while (ft_isspace(str[i]))
0x0000555555555195 in ft_atoi (str=0x555555556004 "111") at ft_atoi.c:33 33 while (ft_isspace(str[i]))
#include <string.h>
#include <stdio.h>
static int ft_isspace(char c)
{
if (c == '\n' || c == '\f' || c == ' ' ||
c == '\r' || c == '\v' || c == '\t');
return (1);
return (0);
}
int ft_atoi(const char *str)
{
int i;
int n;
int signe;
n = 0;
i = 0;
signe = 1;
while (ft_isspace(str[i]))
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i++] == '-')
signe = -1;
}
while (str[i] >= '0' && (str[i] <= '9') && str[i] != '\0')
{
if (((unsigned long)n > 9223372036854775807 / 10) ||
(((unsigned long)n == 9223372036854775807 / 10) &&
((unsigned long)n % 10) > 7))
return ((signe > 0 ? -1 : 0));
n = n * 10 + (*(str + (i++)) - '0');
}
return (n * signe);
}
int main()
{
printf("%d", ft_atoi("111"));
return 0;
}
static int ft_isspace(char c)
{
if (c == '\n' || c == '\f' || c == ' ' ||
c == '\r' || c == '\v' || c == '\t');
return (1);
return (0);
}
This will always return 1, causing the loop to run off the end.这将始终返回 1,导致循环结束。 Notice the semicolon here:
注意这里的分号:
c == '\r' || c == '\v' || c == '\t'); <-- HERE
Imagine if it looked like this:想象一下,如果它看起来像这样:
c == '\r' || c == '\v' || c == '\t') j=2;
That would cause the if
statement to decide whether j=2
is executed.这将导致
if
语句决定是否执行j=2
。 And the return
on the next line would execute regardless of the results of the if
.无论
if
的结果如何,下一行的return
都会执行。
Well your code does that same thing, except instead of j=2
, you have an empty statement.那么你的代码做同样的事情,除了
j=2
,你有一个空语句。
The syntax is: if <expression> <statement>语法是: if <expression> <statement>
Your expression ends with that last close parenthesis.您的表达式以最后一个右括号结束。 So your controlled statement is just the
;
所以你的受控语句就是
;
. .
Just remove that semicolon so the return
is the statement that is controlled by the if
and that appears immediately after the expression.只需删除该分号,这样
return
的语句就是由if
控制的并且紧接在表达式之后的语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.