+ 7

How does this code work?

Particularly -> char *V[(puts("hello world")] https://code.sololearn.com/cAi6wHx0KAvk/?ref=app

15th Feb 2019, 3:08 PM
Abhishek Tandon
Abhishek Tandon - avatar
17 odpowiedzi
+ 4
And we discussed about this code with Infinity ♾️ , that is my hypothesis : I have never seen this syntax so it'll only be a guess : There is a simplification of memory allocation since C99 that allows static arrays to have dynamic size (do not use it, it has a cost in efficiency and if the allocation fail you can't do a thing as it is not you who do it) so the size of V is determined by the result of puts and as the program is called without parameters (sololearn), it does not fail
17th Feb 2019, 6:56 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 9
In terms of CPU time, variable length arrays and dynamically allocated arrays have pretty much the same spec. (it's not super accurate, though) ____ http://quick-bench.com/YHZQGhE-6ZC6zRXZHdJocsX0Mhs
17th Feb 2019, 8:35 AM
Babak
Babak - avatar
+ 8
"but HonFu it has no library, stdio I mean" Compiler answer: " ..\Playground\:2:24: warning: implicit declaration of function 'puts' [-Wimplicit-function-declaration] int main(int C,char *V[puts("hello world")]) { ^~~~ " More elaboration: C language < C99 allows to define (use) a function without providing a prototype explicitly [As for SL's compiler it shows C11 as its current conformation ¹ so it warns you to consider that it's not compliance with the standard ²]. The reason why the compiler is acting so relaxed is that the OP's code would successfully pass the compilation stage because it assumes the `printf` lives (declared) somewhere else. In the next stage, the linker (which in GCC it always implicitly links the standard library to the *.obj or *.o code produced by the compiler) finds the `printf` declaration (and links its implementation which may be in another pre-compiled object code) without any linkage issue. The actual problem arises when the implicit declaration of function/macro introduces something beyond what the linker can see (by default). _____ ¹ printf("%ld", __STDC_VERSION__); // 201112 == C11 ² Warning but not error for the sake of backward compatibility yet C++ kicks code's ass!
17th Feb 2019, 7:18 AM
Babak
Babak - avatar
+ 7
"do not use it (VLA), it has a cost in efficiency and if the allocation fail " Really?! Do you mean heap allocation/access is faster than stack allocation/access?! 8D Despite the fact that they are optional in C11, but they are used especially for a small number of items (to make sure you wouldn't smash the stack!). Suffice it to say, "The main purpose of VLAs is to simplify programming of numerical algorithms. " _____ ¹ https://en.wikipedia.org/wiki/Variable-length_array
17th Feb 2019, 7:49 AM
Babak
Babak - avatar
+ 5
int main(int C, char *leo25[puts("hello world")]) { }
15th Feb 2019, 4:23 PM
Charan Leo25
Charan Leo25 - avatar
16th Feb 2019, 8:12 PM
Abhishek Tandon
Abhishek Tandon - avatar
+ 4
main can take two arguments: The size of an array and an array of strings. The array is supposed to contain the name of the program plus command line arguments, like when you start the exe from the console and add arguments. Normally that just looks like char *c[] though. Normally an int in the brackets means you define a size for an array. puts seems to return a non-negative int if nothing goes wrong. So the size of the array is decided on the fly by a puts statement, that has been sneaked into the index. Compiler doesn't seem to care. That's my interpretation, but I don't know if I'm wrong - or how wrong. ;-)
16th Feb 2019, 8:05 PM
HonFu
HonFu - avatar
+ 4
but HonFu it has no library, stdio I mean
16th Feb 2019, 8:09 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 4
HonFu I simplified the code and now it seems the first parameter isn't needed at all? https://code.sololearn.com/crn2bWCg01FV/?ref=app
16th Feb 2019, 8:15 PM
Abhishek Tandon
Abhishek Tandon - avatar
+ 4
This is a case for... C++ Soldier (Babak)
16th Feb 2019, 8:20 PM
HonFu
HonFu - avatar
+ 3
yeh HonFu I noticed that too, SL seems to know things it shouldn't
16th Feb 2019, 10:15 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 3
Infinity ♾️ for the lack of stdio.h, some compilers pre include some standard libraries and that is why you do not need to put them in the compilation line
17th Feb 2019, 6:49 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 3
thanks a lot C++ Soldier (Babak)
17th Feb 2019, 9:54 AM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 3
"By efficiency, I do not necessarily means time" Yeah, probably you mean generated binary code to deal with the VLA. But it wouldn't be the case on the release mode. ~~~~~~~~ "Firstly it is not quicker as your compiler has to do the work it does on the heap, on the stack" My little experiment confirmed that. ~~~~~~~~ "It is also risky as you can't manage allocation failure" Absolutely! But who said you can't rely on ~1MB of stack size and make a VLA like `int x = 100000; int v[x];` (~400KB) in a separate function to do some numerical stuff? From some kernel developer's POV it definitely unacceptable to mess about with a bomb, but for an independent scientist or a hobbyist who's working on something not too esoteric without worrying about memory leakage, it has its own good. Bonus reading for anyone interested in the motivation behind inventing the VLA (accepted answer): https://stackoverflow.com/questions/22530363/whats-the-point-of-vla-anyway
17th Feb 2019, 8:08 PM
Babak
Babak - avatar
+ 3
Code playground seems to implicitly include "stdio.h"
17th Feb 2019, 10:13 PM
Sonic
Sonic - avatar
+ 2
Does it also work on a normal pc? Because SoloLearn console has occasionally been observed knowing things it shouldn't. 😏
16th Feb 2019, 8:10 PM
HonFu
HonFu - avatar
+ 2
C++ Soldier (Babak) By efficiency, I do not necessarily means time. Firstly it is not quicker as your compiler has to do the work it does on the heap, on the stack. It is also risky as you can't manage allocation failure
17th Feb 2019, 7:19 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar