int a[100];
int sum = 0;
for(int i = 0; i < 100; ++i) {
cin >> a[i];
sum += a[i];
}
and
int sum = 0;
for(int i = 0; i < 100; ++i) {
int val;
cin >> val;
sum += val;
}
My doubt is whether both the code snippets consume same amount of memory ?
Thanks in advance!
Edit : I don't understand why it's getting downvoted despite being a valid doubt.
The first one is consuming more memory because of the array.
Of course not.
You use an array in the first code so it'll consume more memory than the second one.
When the variable val goes out of scope, it is deleted from the stack so it is not that new memory is allocated in every iteration for 100 times, therefore, the 2nd one takes less memory. Also, the array might have overhead.