Strange code from the AI

I can't believe that this works, but then it does and as such I learn something about how the pre-processor works.


#include <stdio.h> #define monkey int main(){printf("monkey\n"); return 0;} monkey

gcc monkey.c -o monkey ./monkey

Program output at console.


monkey

I wonder how long it would take somebody to find a fault that was included in the header of some sub header deep into the structure of includes. Obviously it functions as literal GSR ( Global Search and Replace ). In this case it is more like GSW ( Gun Shot Wound ) So I did this:


#define monkey int main(){return 0;} monkey

How to get the intermediate code.


gcc -E monkey.c -o monkey.pre cat monkey.pre

Contents of the file monkey.pre


# 1 "monkey.c" # 1 "<built-in>" # 1 "<command-line>" # 1 "monkey.c" int main(){return 0;}

Turning assertions off , which is something I have seen applied and simply space off, but here it is. By default, ANSI C compilers generate code to check assertions at run-time. Assertion-checking can be turned off by defining the NDEBUG flag by using gcc with the -DNDEBUG flag. To use assertions you would include "assert.h" with code like the following:


#include <stdio.h> #include <stdlib.h> #include <assert.h> int main (){ void *memory; memory =malloc(100); assert(memory != NULL); printf("This prints if allocate succeeds.\n"); free(memory); }

And finally generates this little piece of work.


int main (){ void *memory; memory =malloc(100); ((memory != ((void *)0)) ? (void) (0) : __assert_fail ("memory != ((void *)0)", "monkey.c", 7, __PRETTY_FUNCTION__)); printf("This prints if allocate succeeds.\n"); free(memory); }

I would show the generated assembly code, but that is pretty obvious so I will leave you with this.

0 comments:

Contributors

Automated Intelligence

Automated Intelligence
Auftrag der unendlichen LOL katzen