I am currently working on verifying and dissecting the request line for pset6 server. I decided to make a smaller test program to explore some of the methods to extract the request, path, query, etc. I decided to use the strtok function to accomplish this.

I have all the elements teased apart correctly, and I was working on defining the root path (just using a hard coded string for the path) then concatenating that root with the path. This is when I encountered and oddity.

If I malloc "root" prior to defining "line";

int main(void)
    root = calloc(1, sizeof(LimitRequestFields));
    root = "~jharvard/dropbox/pset6/public"; //hard coding for testing 

    char line[80] = "GET /hello.php?name=derek HTTP/1.1"; //HC for testing
    char req_seg[3][LimitRequestFields];
    char* req_token = NULL;
    int token_cnt = 0;

the strtok function that divides my query into; path, MEME type, and query works fine.

// "req_token" is defined earlier in main and used with the initial 
   parsing of the request line into Method, Request, Version

// copy the request line for disection
char request[LimitRequestFields];
strcpy(request, req_seg[1]);

char req_path[LimitRequestFields];
char req_querry[LimitRequestFields] = "\0";
char* MIME = NULL;
const char q_mark[1] = "?";

// extract path
req_token = strtok(request, q_mark);
strncpy(req_path, req_token, LimitRequestFields);

// extract querry
req_token = strtok(NULL, q_mark);
if (req_token != NULL)
    strncpy(req_querry, req_token, LimitRequestFields);
    // printf("error(505)\n"); // printf for testing only
    // return 1;

// working output
Method= GET
Request= /hello.php?name=derek

req_Path= /hello.php
Query= name=derek
MIME= php
Absolute Path= ~jharvard/dropbox/pset6/public/hello.php

However, if I malloc "root" directly after I define "line", the function fails;

int main(void);    
    char line[80] = "GET /hello.php?name=derek HTTP/1.1";

    root = calloc(1, sizeof(LimitRequestFields));
    root = "~jharvard/dropbox/pset6/public";

//  Bad Output
Method= GET
Request= /hello.php?name=derek

req_Path= .
Query= n
Absolute Path= ~jharvard/dropbox/pset6/public.

In running GDB with the calloc call at the beginning of main; I discovered that the variable "request" is a valid string, however after the strtok, req_token only contains a single period (instead of the actual request).

If I move it back to the top of main, or all the way down close to where I concatenate absolute path it works fine.

If anyone understands why this occurs, could you please explain it to me? Thanks in advance for your time

  • You're unnecessarily using too many variables. This can be way confusing and makes your code very difficult to read and understand. If you don't intend to improve your code and minimize the number of variables used, probably it's best to update your question with the full code of the 2 versions to make it easy for us to compile your code and maybe use a debugger to debug it.
    – kzidane
    Mar 28, 2015 at 16:44
  • @Kareem, there are only 11 variables in the code, perhaps, out of context it is confusing? I could show you the entire code, for clarification. The information under good / bad output, are the result of printf's called at the end of the program.
    – Dhosborne
    Mar 28, 2015 at 23:17
  • That being said, if you have a more elegant way of accomplishing this, I am very willing to learn from you.
    – Dhosborne
    Mar 28, 2015 at 23:24

1 Answer 1


It turns out I am UN-necessarily malloc'ing memory and then writing over it. Thanks @Kareem

You must log in to answer this question.

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