简体   繁体   English

为什么我可以单独打印每个字符而不是整体打印?

[英]How come I can print out each character separately but not as a whole?

This is one part of a server implementation where I'm going through the request line header and giving output accordingly.这是服务器实现的一部分,我将通过请求行标头并相应地给出输出。 This might be a basic question.这可能是一个基本问题。 How come I can print out each character separately but not the string as whole?为什么我可以单独打印出每个字符而不是整个字符串?

Has this something to do with mismanagement of memory?这与内存管理不善有关吗?

This is part of pset6 in CS50 .这是 CS50 中pset6 的一部分。

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

 int
 main(int argc, char * argv[])
 {
    char* line = "GET /cat.html HTTP/1.1";

    char* method = strstr(line, "GET");
    if (strcmp(method, line) != 0)
    {
        printf("405 Method Not Allowed");
    }

    printf("%s\n", line);

    char* requestTarget = malloc(10);
    char* ch = strchr(line,'/');
    if (ch != NULL)
    {
        int i = 0;
        for (i = (ch-line); i < strlen(line)-9; i++)
        {
            requestTarget[i] = line[i];
            printf("%c", requestTarget[i]);
        }
        requestTarget[i] = '\0';
    }

    else
         printf(" 501 Not Implemented");

    printf("requestTarget: %s\n", requestTarget);

    free(requestTarget);
    return 0;
 } 

A side note, I know it's bad pratice to hard code in -9 in my for lop strlen(line)-9 .旁注,我知道在我的 for lop strlen(line)-9硬编码-9是不好的做法。 But I couldn't figure out how to just read the characters in the requested target cat.html .但我不知道如何只读取请求的目标cat.html的字符。 And I know that the header is specified by method SP request-target SP HTTP-version CRLF (is CRLF aka \\r\\n two characters?) So -9 works (I think) but maybe not the best.而且我知道标头是由method SP request-target SP HTTP-version CRLF (CRLF 又名\\r\\n两个字符?)所以-9有效(我认为)但可能不是最好的。

EDIT: I edited my loop so that I add an null terminator at the end.编辑:我编辑了我的循环,以便在最后添加一个空终止符。 This was originally meant to be in, but since I have edited my code so much now it was mistakenly taken out.这本来是要放进去的,但由于我现在对代码进行了大量编辑,因此被错误地删除了。 It still does not work though.尽管如此,它仍然不起作用。

Your code has undefined behavior, because it writes past the space that you allocated.您的代码具有未定义的行为,因为它写入了您分配的空间。

You do this copy你做这个副本

requestTarget[i] = line[i];

when i points to some location in the middle of the line[] , but requestTarget requires a smaller index.i指向line[]中间的某个位置时,但requestTarget需要较小的索引。 You need to "translate" the index, or create a separate one for the destination:您需要“翻译”索引,或为目的地创建一个单独的索引:

int j = 0;
for (int i = (ch-line); i < strlen(line)-9; i++, j++)
{
    requestTarget[j] = line[i];
    printf("%c", requestTarget[j]);
}
requestTarget[j] = '\0';

Note: you need to make sure that requestTarget has enough space for the characters that you wish to store in it, including the null terminator.注意:您需要确保requestTarget有足够的空间来存储您希望在其中存储的字符,包括空终止符。

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

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