r/programmingcirclejerk • u/Hauleth Zygohistomorphic prepromorphism • Sep 11 '17
When JS programmer is forced to write C code
https://twitter.com/MarcosBL/status/64111042419323289758
u/StallmanTheWhite Sep 11 '17
Is this available as a library yet? Microlibraries truly are the future.
54
u/CrazyMerlyn Sep 11 '17
Note: This module doesn't consider -0 to be a negative number. If you want to detect -0, use the negative-zero module.
36
16
u/electrostat what is pointer :S Sep 11 '17
HOLY FUCK, you just blew my mind. -_________________- I can't tell if this is a joke or not...
7
38
u/msiekkinen Sep 11 '17
I used my minifying transpiler but it comes out as unreadable machine code
arg < 022
Sep 11 '17
mov eax, [ebp+08] cmp eax, 0 jge loc_whateverYou need to disassemble it to become readable.
9
5
u/ws-ilazki in open defiance of the Gopher Values Sep 12 '17
> Didn't use movfuscator to represent everything as
mov.Filthy 1x heathen!
4
u/irqlnotdispatchlevel Tiny little god in a tiny little world Sep 12 '17
I like the more readable
8b450883f8007d008
u/StallmanTheWhite Sep 11 '17
Ugh, why can't machine use human readable naming conventions!?!?!
4
5
u/Neckbeard_Prime Sep 11 '17
Is it available as a webscale wweb service as on the internet yet? FaaS truly is the future.
23
Sep 11 '17 edited Sep 11 '17
I hate to break the jerk peeps but the code is from the Buggy 4-Function Calculator from 2007. It's actually genius :D
EDIT: The rest of the submissions are also great.
8
Sep 11 '17
Clearly SP asked the Google for an implementation of IsNegative for C, and this is what he found. Don't take this away from me.
4
u/yourgifrecipesucks Sep 11 '17
I'm just impressed they casted malloc.
4
u/Hauleth Zygohistomorphic prepromorphism Sep 11 '17
Like you should do.
0
u/Zatherz of questionable pressisscion Sep 11 '17
No, you shouldn't.
0
u/ThisIs_MyName loves Java Sep 11 '17
Never used C++?
3
u/leoel Sep 11 '17
C++ completed the implicit and sometimes undefined behaviour of C-style casts by the adjunction of explicit casts. So if you use C++ you would probably use another kind of cast. https://softwareengineering.stackexchange.com/questions/50442/c-style-casts-or-c-style-casts
0
Sep 11 '17
[deleted]
2
u/Hauleth Zygohistomorphic prepromorphism Sep 11 '17
Why you shouldn’t? This isn’t wrong and IMHO can help sometimes.
10
Sep 11 '17
It means they're using a C++ compiler which doesn't automatically cast void pointers like C does
2
5
Sep 12 '17
Can't jerk. Too many offshore contractors I've worked with use Strings for everything. Numbers, dates, even checking literal strings "True" and "False" in if conditions instead of using booleans.
0
Sep 11 '17
[deleted]
38
u/msiekkinen Sep 11 '17
Naw, that's intentional so you have a counter to tell you how many times you checked if something was negative.
16
u/cmov NRDC. Not Rust Don't Care. Sep 11 '17
lol no RAII
27
u/brutallysimple Sep 11 '17 edited Sep 11 '17
This .1x pleb didn't use C's brutally simple RAII:
void cleanup(char **arg) { free(*arg); } static int isNegative(float arg) { __attribute__((__cleanup__(cleanup))) char *p = (char*) malloc(20); sprintf(p, "%f", arg); return p[0] == '-'; }Simple and elegant.
20
7
1
7
u/Sovex66 Sep 11 '17
Could you explain why there a memory leak ?
41
u/DasEwigeLicht Sep 11 '17
See the part that says
malloc? That's memory allocation. See the part where that memory is freed again?12
u/VodkaHaze Sep 11 '17
It also has a potential buffer overrun, no?
28
u/throwawayco111 Sep 11 '17 edited Sep 11 '17
Yeah. It should be:
char *p = malloc(SIZE_MAX);
SIZE_MAXis defined inlimits.h.33
u/thukydides0 what is pointer :S Sep 11 '17
No, you forgot the null terminator:
char *p = malloc(SIZE_MAX + 1);13
7
u/Noughmad log10(x) programmer Sep 11 '17
Maybe it's time to stop writing C manually.
17
u/cmov NRDC. Not Rust Don't Care. Sep 11 '17
Hi, have you tried Rust?
6
0
2
6
u/irqlnotdispatchlevel Tiny little god in a tiny little world Sep 12 '17
lol the OS will free all the resources when the process is terminated, so why bother?
3
11
3
5
u/Arkaad log10(x) programmer Sep 12 '17
Bright green font over a black background?
Does he want to lose his eyesight?!