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.
EDIT: Thanks a lot guys, I got it working by changing my one-line isPalindrome()
function to this:
return (...)?(calls == 1?free(pLeft),1:1):(calls == 1?free(pLeft),0:0);
(calls is keeping track of recursion depth)
No more memory leaks!