Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

memory leak

Name: Anonymous 2006-04-01 20:07

typedef char* String;

String getNth(String str, int n)
{
    int i;
    String temp = malloc(sizeof(char) * MAX_SIZE);
    String regExp = malloc(sizeof(char) * MAX_SIZE);
       
    for(i = 0; i < n; i++)
    {
        if (i == n-1) strcat(regExp, "%s");
        else strcat(regExp, "%*s");
    }

    sscanf(str, regExp, temp);
    free(regExp);
    return temp;
}

this leaks memory (temp). a neat way to prevent this, please.

Name: Anonymous 2009-11-01 21:30

>>17
There is actually a reason why someone might want to use a sizeof(char). If they eventually wanted to change some code using strings from ASCII to UTF18 (wchar_t), they would have less of a trouble changing the code. On the other hand, they could just use a few [code#ifdef UNICODE[/code]'s to define functions/structures/variable.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List