Treat a void function as a value
- by Brendan Long
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.