Anonymous
Learn the basics
Anonymous
Read it
Anonymous
will it pass the compilation
Jussi
we told you 5 times you cannot do that. Now believe us and never write code like that :-)
Anonymous
we told you 5 times you cannot do that. Now believe us and never write code like that :-)
I know that it is silly to do that and I won't do that. but just wanted to ask whether it will pass the compilation. that's it
Anonymous
Yes
Thanks for it.
Anonymous
u forgot include type for std::array :)
I think it's obvious :)
Jussi
:-)
Anonymous
how does return by value work
Anonymous
first the copy of the returned value is made and then returned and the original value is deleted.
Anonymous
Is that right?
I_Interface
Is that right?
Return by value is the simplest and safest return type to use. When a value is returned by value, a copy of that value is returned to the caller. As with pass by value, you can return by value literals (e.g. 5), variables (e.g. x), or expressions (e.g. x+1), which makes return by value very flexible.
Anonymous
Also, I read that When not to use return by value: When returning a built-in array or pointer (use return by address) When returning a large struct or class (use return by reference)
MᏫᎻᎯᎷᎷᎬᎠ
.
MᏫᎻᎯᎷᎷᎬᎠ
how does return by value work
I guess you mean pass by value
Anonymous
No. return
Anonymous
by value
Anonymous
not pass
MᏫᎻᎯᎷᎷᎬᎠ
Well
olli
So, these are return by address. Right?
There's no such thing as "return by address". Your function declaration defines the return type to be a pointer to int. Your array decays to a pointer when used as parameter or return type
Anonymous
https://www.learncpp.com/cpp-tutorial/74a-returning-values-by-value-reference-and-address/
MᏫᎻᎯᎷᎷᎬᎠ
Well The function returns the bare value which is not a reference or a pointer to a memory location
Anonymous
I read that a copy is returned
Anonymous
copy of the value to be returned.
MᏫᎻᎯᎷᎷᎬᎠ
It always return a copy of a something
MᏫᎻᎯᎷᎷᎬᎠ
No matter what
Anonymous
OK.
MᏫᎻᎯᎷᎷᎬᎠ
The real talk is What copy is returned
MᏫᎻᎯᎷᎷᎬᎠ
A reference or a Pointer or a value
Anonymous
can return by address be used to safely return local static members
Anonymous
?
MᏫᎻᎯᎷᎷᎬᎠ
If it's a reference or a pointers The refered/pointed location must be valid after the function call ends
MᏫᎻᎯᎷᎷᎬᎠ
Or it's ub
MᏫᎻᎯᎷᎷᎬᎠ
As long as the static member is valid
MᏫᎻᎯᎷᎷᎬᎠ
But not the vise versa
Anonymous
what scope do local static variables fall in? Are they destroyed after exiting from function?
MᏫᎻᎯᎷᎷᎬᎠ
what scope do local static variables fall in? Are they destroyed after exiting from function?
iirc, static variables are variables that is meant to be existed till the program finishes its execution
MᏫᎻᎯᎷᎷᎬᎠ
But Static member function can't use non-static members
MᏫᎻᎯᎷᎷᎬᎠ
Because static members are instantiated without the need of constructing an object
olli
iirc, static variables are variables that is meant to be existed till the program finishes its execution
Correct, "static storage duration: The storage for these entities shall last for the duration of the program" [basic.stc.static]
Anonymous
are class static varaibles have same lifetime as that of local storage variables?
Anonymous
OK so local storage have lifetime as same as that of the program?
MᏫᎻᎯᎷᎷᎬᎠ
Local variables are limited to the lifetime of the object that created them
MᏫᎻᎯᎷᎷᎬᎠ
Unless they are not allocated on the heap
Srihari
is any one doing 30 days challeng in hacker rank??
Srihari
MᏫᎻᎯᎷᎷᎬᎠ
Or the destructor failed to be called
Srihari
how can i pass the hidden test cases??
Ludovic 'Archivist'
are class static varaibles have same lifetime as that of local storage variables?
Absolutely not, they have an initialization check and if not initialized they get initialized
Anonymous
You have to pass all the test cases
Harsh
i use java for competitive programming i want to switch to c++ can anyone one send me link to a tutorial which can teach me c++
Anonymous
You should write code that matches the specification and it should pass all of them
olli
OK so local storage have lifetime as same as that of the program?
Block-scope Variables have an automatic storage duration and are valid until the block, they were initialized in, ends
Harsh
yes
Ludovic 'Archivist'
Anonymous
The hidden test cases are just hints at what special cases your code is failing at
Harsh
send me the link
Harsh
Srihari i will help you
MᏫᎻᎯᎷᎷᎬᎠ
And they are not cleaned up until exit is called
Doesn't exist function terminate the program without freeing resources?!🤔
MᏫᎻᎯᎷᎷᎬᎠ
☝️☝️☝️☝️☝️
Read bjarne's book
olli
☝️☝️☝️☝️☝️
There are a lot of information on the web to learn cpp. A good place to start can be https://en.cppreference.com/w/