I am using the following function
int parse_headers(char *str, struct net_header *header)
{
char *pch;
struct net_header *h, *temp;
pch = strtok(str, "\n");
header->name = pch;
h = malloc(sizeof(struct net_header));
header->next = h;
while ((pch = strtok(NULL, "\n")) != NULL)
{
h->name = pch;
temp = malloc(sizeof(struct net_header));
h->next = temp;
h = temp;
}
return N_SUCCESS;
}
Up until the line header->next = h
, everything works as planned. However, after the line h = malloc(sizeof(struct net_header));
, the variables pch
and str
for some reason turn to NULL
(I set breakpoints to find this). After the line temp = malloc(sizeof(struct net_header));
, header
also turns to NULL
. Clearly, I have some kind of memory management issue, but I can't seem to find what it is. The header
argument is initialized like this immediately before I call the function
header = malloc(sizeof(struct net_header));
struct net_header
is declared as
struct net_header
{
char *name;
char *content;
struct net_header *next;
};
I ran Xcode's static analyzer, which found no issues. I also have no compiler warnings or errors. I am running this program on Mac OS X 10.9.
Why are my variables being nullified after I call malloc()
?