Hello,
I am having a hard time understanding the difference between compiletime and runtime.
Ex :
1 2 3 4 5
int a = 10;
int b[100];
int *c = newint[100];
What the rules say :
memory allocation is done at compile time for 'a' and 'b' , and the pointer 'c' itself, 100 bytes for 'c' will be allocated at runtime.
How I understand :
I wrote the code which says : "Compiler ! Give me 1 byte for 'a', 100 bytes for 'b' and 100 bytes for 'c', and also 4 bytes (or 8 depending on ARCH) for my pointer 'c' ", and of course ONLY when I click the executable file , and my CMD will pop up , the compiler Operating System will do so. ( Because I don't think while I am compiling my code , when it comes to line of 'a' or 'b' or 'c' it takes away the RAM addresses needed and uses them till I restart my PC )
Right now for me there isn't any compile or run time difference for this code.
What I do realise is that runtime can be associated with user input , let's say an interactive menu where I have to make a decission.
Actually, everything in your snippet (except the 10, 100, and 100) happens at runtime. There are only a few special cases where things happen at compile time - for example, constexpr functions, template instantiation, and some static constants are substituted during compile time.
You might be confused with the scope of objects bound to local variables versus the scope of dynamically allocated objects. Objects bound to local variables have to have a known size - the compiler knows this size at compile time and makes provisions for it in the executable, but the actual allocation still happens at runtime.
when I click the executable file , and my CMD will pop up , the compiler will do so.
It's not the the compiler that does it. The compiler is only involved at compile time (when the program is compiled). If you uninstall the compiler you should still be able to run the executable.
this will instruct the operating system to take memory ( 4 bytes for a and 100 bytes for b ) and allocate it from the place called stack ( which I can change its size ), and this memory will be freed when these variables above will go out of their scope .
but when writing
int *c = newint[100];
the operating system will take memory from heap , and I will be able to free it whenever I want , in worst case when the program closes, and c pointer stays on stack.
I would read about it( could you give a link ? ), but I don't know the name of this , I googled runtime vs compile time , didn't understand much.