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

Pages: 1-

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 2006-04-01 22:30

String a = getNth(x, y);
...blah blah...
free(a);

PS. WTF: "typedef char* String;"? Why?

Name: Anonymous 2006-04-01 23:07

>>2

Alternatively, you can specify that the variable holding the result be allocated ahead of time:

String getNth(String str, int n, String result) { ... }

Name: Anonymous 2006-04-02 9:49

I can't even tell what this function does, but it's bad form to return pointers in C since it forces you to malloc, and then you have to free later. Better to pass in a pointer to a string and have the function operate on that.

Name: Anonymous 2006-04-02 15:21

And the format strings accepted by sscanf() aren't regular expressions, so minus points for calling them regexps. Javur fool. Go read a book on C.

Name: Anonymous 2006-04-02 22:28

>>3
I think you mean

void getNth(String str, int n, String result) { ... }

Name: Anonymous 2006-04-02 22:46

>>1
I don't think malloc zeroes the allocated memory, so your'e concatenating %s onto some unknown shit (possibly past the end of the memory you allocated!)

Also, it's probably more efficient to loop through str while counting spaces than to build up this stupid format string that throws away the first n-1 strings. Actually, I can't think of how this function would be useful in the first place (if you call it more than once, you probably should just split up the string by spaces once into an argv-like array.)

Name: Anonymous 2009-11-01 17:38

typedef char* String;

void getNth(String dest, String str, int n)
{
    int i;
    String temp = malloc(sizeof(char) * MAX_SIZE);
    String regExp = malloc(sizeof(char) * MAX_SIZE);

    memset(temp, 0, sizeof(char) * MAX_SIZE);
    memset(regExp, 0, sizeof(char) * MAX_SIZE);

    /* what the hell is this? nobody knows.
     * please use real variable names and
     * comments in the future.
     */
    for(i = 0; i < n; i++)
    {
        if (i == n-1) strcat(regExp, "%s");
        else strcat(regExp, "%*s");
    }

    sscanf(str, regExp, temp);

    strcpy(dest, temp);

    free(temp);
    free(regExp);
    return temp;
}

Name: Anonymous 2009-11-01 17:40

>>10
oops, remove that "return temp;" there.

Name: Anonymous 2009-11-01 18:29

>>10
YOU HELPED HIM !!

Name: Anonymous 2009-11-01 19:07

>>12
Perhaps.  But I sense that if he is doing typedef char* String; that his code might be beyond my help.

Name: Anonymous 2009-11-01 19:47

>>13
See also the win32 library

Name: Anonymous 2009-11-01 20:33

make temp static

Name: Anonymous 2009-11-01 21:02

Stop with the sizeof(char).  You're just embarrassing yourself there.  I've get the ISO/IEC 9899:TC3 document in front of me right now, and it says sizeof(char) is 1.  Always.  Forever.  On every system ever made, and every system that will ever be made.  The whole point of sizeof is to determine the length of an array of unsigned char necessary to hold the representation of a value.  And the size of a an integer type is the same as the size of unsigned and signed variants of that type, so the representations can be compatible for values in the intersection of their ranges.  So sizeof(char) == sizeof(unsigned char), always.

I can understand writing 'char* fred = malloc(sizeof(*fred) * NUM_FRED)', but this 'sizeof(char)' thing is just ridiculous.

Oh, and to answer your problem... it seems like you are a little pointer shy.  And ditch your stupid "String" typedef.  You also don't want to do that stupid trick with sscanf, it's tricks like that which will end you up on the front of the daily WTF.


char *get_field(char const *restrict s, int n)
{
    char const *p;
    char *r;
    for (; n; ++s) {
        if (!*s)
            return NULL;
        if (*s == ' ') {
            n--;
            while (*s++ == ' ');
        }
    }
    for (p = s; *p && *p != ' '; ++p);
    r = xmalloc(p - s + 1);
    memcpy(r, s, p - s);
    r[p - s] = '\0';
    return r;
}

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.

Name: Anonymous 2009-11-01 22:11

>>17
Or, they could do the sane thing and use UTF-8.

Name: Anonymous 2009-11-02 1:01

>>15
hold on, what?

Name: nfsjlj 2011-01-23 1:01

Ignore this.

Name: Anonymous 2011-11-26 18:04

ANUS

Name: Anonymous 2012-03-28 2:19

my farts burn my anus
it hurts
in a good way

Name: !N883pjzK6k 2012-05-14 16:03

test

Name: !f5SKvMeRkg 2012-05-14 16:04

test

Name: !vYdMlR7JUw 2012-05-14 16:04

test

Name: !A0IK8V1.sA 2012-05-14 16:05

test

Name: !2PcFgcDp9k 2012-05-14 16:05

test

Name: !2PcFgcDp9k 2012-05-14 16:05

test

Name: !2PcFgcDp9k 2012-05-14 16:05

test

Name: !yfs7dwYG7Y 2012-05-14 16:06

test

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