Treat a void function as a value

Posted by Brendan Long on Stack Overflow See other posts from Stack Overflow or by Brendan Long
Published on 2010-04-18T10:37:51Z Indexed on 2010/04/18 10:43 UTC
Read the original article Hit count: 241

Filed under:
|

I'm writing some terrible, terrible code, and I need a way to put a free() in the middle of a statement. The actual code is:

int main(){
    return printf("%s", isPalindrome(fgets(malloc(1000), 1000, stdin))?"Yes!\n":"No!\n") >= 0;
    // leak 1000 bytes of memory
}

I was using alloca(), but I can't be sure that will actually work on my target computer. My problem is that free returns void, so my code has this error message:

error: void value not ignored as it ought to be

The obvious idea I had was:

int myfree(char *p){
    free(p);
    return 0;
}

Which is nice in that it makes the code even more unreadable, but I'd prefer not to add another function.

I also briefly tried treating free() as a function pointer, but I don't know if that would work, and I don't know enough about C to do it properly.

Note: I know this is a terrible idea. Don't try this at home kids.

© Stack Overflow or respective owner

Related posts about c

    Related posts about functions