> std::cout << "You entered " + x << std::endl;
What you have here is C style pointer arithmetic in disguise.
A string literal has a type of 'const char *'.
A normal string could be considered as being equivalent to std::cout << &"You entered "[0]
But with a + there, what you now have is std::cout << &"You entered "[x]
Now, what I would like to understand is, how come that I am running my main.exe several times but does it remembers my previous run?
I thought after I exit main() and return 0, everything should be re-initialized again back to the start but it looks to me that it is remembering my previous run.
Thank you all for helping me in my previous problem and I just would like to understand this further.
I thought after I exit main() and return 0, everything should be re-initialized again back to the start but it looks to me that it is remembering my previous run.
No, the results of your previous run are long gone.
It isn't remembering anything - you are simply putting different input in each time (1, 2, 3, 4 ... from your original post).
Read @salem c's explanation of what is actually happening.
1 2 3 4 5
#include <iostream>
int main()
{
for ( int x = 0; x < 10; x++ ) std::cout << "You entered " + x << '\n'; // Unintended use of '+'
}
I thought after I exit main() and return 0, everything should be re-initialized again back to the start but it looks to me that it is remembering my previous run.
Everything except memory contents is re-set at the start. So un-initialised variables have an initial value of whatever happened to be in memory at their location - which could be their value from the last run or something different.
Modern operating systems normally zero out all the memory before handing it to the program. Global variables are automatically zero initialized. In the original program that the OP posted there is no use of uninitialized variables.
Note that reading from uninitialized variables are technically undefined behaviour. What seeplus says is often what ends up happening but there are no such guarantees in the language specification so it's nothing to rely on.
Time for Peter87 to disappear I think. Unsolicited evaluation and arguing about other people's (in this case @seeplus') contributions is arrogant and has fortunately been a thing of the past. Unfortunately it is now reappearing. Address the @OP, Peter87, or bugger off. You're not adding value with your nonsense.
Thanks all I think I understood your point. I just happen to read about pointer topics last night and from what I understood and what the answers that I have read here it looks like I am doing pointer arithmetic such that
Modern operating systems normally zero out all the memory before handing it to the program.
No, I don't think so. That would have too much of an performance impact. If at all the compiler would generate code that does it. But this happens rather in debug mode.
Global variables are automatically zero initialized.
Yes. And I find it kind of surprising. Why are global variables handled different than local variables?
Modern operating systems normally zero out all the memory before handing it to the program.
coder777 wrote:
No, I don't think so. That would have too much of an performance impact.
I'm talking about memory that is handed to the program from the OS when the program starts or needs more memory. Not when the memory is reused within the program. The OS zeroes the memory in order to prevent programs from reading the data that is left over in memory from other programs because that could be a security problem.
coder777 wrote:
Yes. And I find it kind of surprising. Why are global variables handled different than local variables?
All variables with static storage duration (which includes "globals") are zero initialized before normal initialization takes place. I think the reason the standard guarantees this is because it normally happens anyway due to what I said earlier.
I'm talking about memory that is handed to the program from the OS when the program starts or needs more memory.
Ok, that makes sense. Only the global data is initialized to zero, which then has an impact on the starting time. So the globals (which don't have a constructor) aren't initialized by the language...