0

I am taking this course online. I do not have anyone to ask for help that knows anything about coding so I rely 100% on this discussion board (and the internet in general) for help. I am having trouble with pointers. I couldn't finish Pset5 so I quit stressing and went ahead with Pset6 in hopes that I could learn something that would help. I get so frustrated because I know exactly what I want to do, but I do not know how to execute it in code.

I am working on Parse and I am going through each rule and trying to get it to pass check50 and gdb. I think I am doing everything logically, but I keep getting it wrong.

This is what I am having trouble with: I have created const char* for known points in "line": one for the first space between the method and the request line, one for the 2nd space between the request line and the version, and one to "\r" of CRLF. I malloc memory for the method, request line, and the version and store it in a char* for each using, as an example, char* method = malloc(sp1 - line), where sp1 is a const char* for the first space in line. I then strncpy(method, line, strlen(method) in order to copy "GET" to method. Then I if (strcmp(method, "GET") != 0) ...error(405)...

I have tried this and countless other variations with each rule and sometimes it works and sometimes it doesn't. I am totally lost because even writing all of the code out on paper and checking and double checking it seems correct but when I compile and run check50 soemething is always wrong.

Somebody please tell me if this is the completely wrong way of going about solving this problem.

2

1 Answer 1

0

Let's look at your example, assuming line = "GET /cat.jpg HTTP/1.1\r\n":

char* method = malloc(sp1 - line): In this case (relatively) sp1 = 3 and line = 0, so method is malloc'd for 3 bytes. Which is enough to hold "GET", but does not accommodate the null-terminator. When you are allocating a string (char*) variable, remember to always allocate one additional byte "for the computer".

strncpy(method, line, strlen(method)): strlen reports the length of string up to the terminating null byte. It does not report the number of bytes allocated. Since method has not been populated yet, strlen(method) gives "unpredictable results". In this example you know that you want to copy 3 bytes, so something like strncpy(method, line, sp1 - line) will give (closer to) the result you want. With the warning that method will not be null terminated.

malloc does not initialize the memory it reserves for a variable. There is no telling what might be in that memory ("unpredictable result"). calloc, on the other hand, reserves the memory and initializes it to 0. (Technically speaking the "null-terminator" is a 0. The '\0' convention is a convenience for the humans.) malloc is like making a reservation for 6 at a restaurant (even though you are a party of 5; one seat for the "uninvited null-guest"!). But when you're seated, the table has not been cleared from the previous party. At chez calloc however, the table is cleared and ready for your meal.

IMO this is not "the completely wrong way of going about solving this problem". You need to give yourself time to understand how the computer sees the things that are very clear to you. After all, if programming was intuitive and easy, there would be no need for "school".

2
  • Thank you for taking the time to explain this. Would adding +1 outside of parentheses, (sp1 - line)+1, be the preferred way of ensuring space for the null terminator? Dec 15, 2016 at 4:07
  • The only think I know for sure is you have to add 1 for the null terminator. Mathematically, malloc(sp1 - line + 1) is the same as malloc((sp1 - line) + 1) which makes the inner parens unnecessary. Dec 15, 2016 at 12:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .