+ 7
How does this code work?
Particularly -> char *V[(puts("hello world")] https://code.sololearn.com/cAi6wHx0KAvk/?ref=app
17 Respostas
+ 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
+ 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
+ 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!
+ 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
+ 5
int main(int C, char *leo25[puts("hello world")]) {
}
+ 5
*AsterisK*
Even this works
https://code.sololearn.com/c2t8lYH739pP/?ref=app
+ 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. ;-)
+ 4
but HonFu it has no library, stdio I mean
+ 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
+ 4
This is a case for...
C++ Soldier (Babak)
+ 3
yeh HonFu I noticed that too, SL seems to know things it shouldn't
+ 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
+ 3
thanks a lot C++ Soldier (Babak)
+ 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
+ 3
Code playground seems to implicitly include "stdio.h"
+ 2
Does it also work on a normal pc?
Because SoloLearn console has occasionally been observed knowing things it shouldn't. 😏
+ 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