1

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()?

12
  • Does *str turn to NULL or str itself? str is a copy of a pointer on the stack or in a register and should never be affected by a call to malloc even in case of some memory management issue. The same applies to pch.
    – Meixner
    Apr 14, 2015 at 19:23
  • header->next = h; will be a problem if header is NULL. How sure are you that header is not NULL?
    – R Sahu
    Apr 14, 2015 at 19:23
  • @Meixner prior to malloc, it contains a string ("foo"), but after the call it is NULL, so I would say str itself. Apr 14, 2015 at 19:25
  • @RSahu When I set the breakpoint anywhere before the while-loop, header points to a valid object, and its name value is properly set. Apr 14, 2015 at 19:25
  • 1
    try assigning to h with any other function call than malloc(). If you get similar behaviour (variables change arbitrarily), you suffer from stack corruption.
    – mfro
    Apr 14, 2015 at 20:29

2 Answers 2

0

if you need to keep strtok result you have to dup it, with for example strdup

int parse_headers(char *str, struct net_header *header)
{
    char *pch;
    struct net_header *h, *temp;

    pch = strtok(str, "\n");
    header->name = strdup(pch);

    h = malloc(sizeof(struct net_header));
    header->next = h;
    while ((pch = strtok(NULL, "\n")) != NULL)
    {
        h->name = strdup(pch);

        temp = malloc(sizeof(struct net_header));
        h->next = temp;
        h = temp;
    }
    return N_SUCCESS;
}

you need to call free somewhere to free the memory

3
  • For more info, see here: stackoverflow.com/questions/7100214/strtok-segfault
    – Tim
    Apr 14, 2015 at 19:34
  • 1
    This is helpful, but I still have the nulling problem. Apr 14, 2015 at 19:35
  • That's only if str might be changed and you want header->name to be independent of str. So far, this has nothing to do with his problem.
    – antiduh
    Apr 14, 2015 at 19:35
0

I tried to reproduce your error, running on Mac OS X 10.10, and there is no problem with your code, it works perfectly.

Your problem comes from another call to malloc() with a bad size like in this post and in this one, generally caused by passing a pointer's size instead of the real size of variable's type.

I cannot tell you where since I don't see your entire code running before this "bad call", and I couldn't ask in comments because I don't have enough rights.

Hope it helps.

2
  • I'll look into this when I get home and let you know if I find anything. Apr 14, 2015 at 20:24
  • Any news about your issue ? Do you succeed to fix it ?
    – Misery
    Apr 16, 2015 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.