[英]Why do i get an Segmentation fault?
I know it has to do with my for loop. 我知道这与我的for循环有关。 Have tried to modify it, but whatever I put in for the arguments, I still get an Segmentation fault.
尝试修改它,但是无论我为参数添加什么,我仍然会遇到细分错误。
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(int argc, char * argv[])
{
char* request_target = "/cat.html?q=Alice hej";
// TODO: extract query from request-target
char query[20];
// finds start of query
char* startofquery = strstr(request_target, "q=");
if (startofquery != NULL)
{
int j = 0;
for (int i = strlen(request_target) - strlen(startofquery); i == ' '; i++, j++)
{
query[j] = request_target[i];
}
request_target[j] = '\0';
}
else
{
printf("400 Bad Request");
}
printf("%s", query);
}
This line defines a string literal 该行定义了字符串文字
char* request_target = "/cat.html?q=Alice hej";
It is undefined behaviour to write to a string literal 写入字符串文字是未定义的行为
you are doing that here : 您在这里这样做:
request_target[j] = '\0';
use a char array instead 改用char数组
char request_target[] = "/cat.html?q=Alice hej";
Also, if I understand correctly you are trying to extract q=Alice
from /cat.html?q=Alice hej
. 另外,如果我正确理解,您正在尝试从
/cat.html?q=Alice hej
提取q=Alice
。 The for loop you implemented has a few issues ( i == ' '
) as mentioned in other answers. 如其他答案中所述,您实现的for循环有几个问题(
i == ' '
)。 And is not actually necessary. 并且实际上没有必要。 You can copy this part quite simply:
您可以非常简单地复制此部分:
char *startofquery = strstr(request_target, "q=");
char *endofquery = strchr(startofquery, ' ');
int querySize = endofquery - startofquery;
if (startofquery != NULL && endofquery != NULL) {
memcpy(query, startofquery, querySize);
query[querySize] = '\0';
}
This is less error prone and will most likely perform better. 这样不太容易出错,很可能会表现更好。 In this case you don't need to define
request_target
as an array but I would recommend to make it const so that you will get a useful compiler error if you attempt to write: 在这种情况下,您不需要将
request_target
定义为数组,但我建议将其设置为const,这样,如果您尝试编写以下操作,将会得到有用的编译器错误:
const char *request_target = "/cat.html?q=Alice hej";
There are two points that I can see appears to be troublesome. 我可以看到两点似乎很麻烦。
in your for loop, the terminating condition 在您的for循环中,终止条件
i == ' '
should be 应该
request_target[i] != ' '
if you want to run the loop until it gets a space character, or, 如果您要运行循环直到获得空格字符,或者,
request_target[i] != '\\0'
if you would want to run the loop til the end of the source string. 如果您想运行循环直到源字符串的末尾。
request_target[j] = '\\0';
should be query[j] = '\\0';
应该是
query[j] = '\\0';
because, 因为,
1) You cannot modify a string literal, it's undefined behavior . 1)您不能修改字符串文字,这是未定义的行为 。
2) Probably you want to null terminate the destination array. 2)可能您想为null终止目标数组。
I see two problems in your for loop. 我在您的for循环中看到两个问题。 The first one is the condition:
第一个是条件:
for (int i = strlen(request_target) - strlen(startofquery); i == ' '; i++, j++)
Here i
is an integer (position in your string), but you are comparing against a char ( ' '
). i
在这里是一个整数(字符串中的位置),但是您正在与一个char( ' '
)进行比较。 It should be: 它应该是:
for (int i = strlen(request_target) - strlen(startofquery); request_target[i] != ' '; i++, j++)
The second problem is you're trying to write into a string literal (UB) here: 第二个问题是您要在此处写入字符串文字(UB):
request_target[j] = '\0';
It should be: 它应该是:
query[j] = '\0';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.