Memory, no difference. y is on the stack where it is declared in the method. Here the only difference is the scope of y: in the second case, it is limited by the body of the for loop; firstly, it is not. This is purely at the language level: again, y is distributed exactly the same, that is, on the stack.
Just to make this point perfectly clear, here is a sample code:
void method1() {
for (;;) {
int a = 10;
}
}
void method2() {
int a;
for (;;) {
a = 10;
}
}
Here is the assembler generated in debug mode in both cases:
00000000 push ebp
00000001 mov ebp,esp
00000003 push eax
00000004 cmp dword ptr ds:[00662E14h],0
0000000b je 00000012
0000000d call 5D9FE081
00000012 xor edx,edx
00000014 mov dword ptr [ebp-4],edx
00000017 mov dword ptr [ebp-4],0Ah
0000001e nop
0000001f jmp 00000017
00000000 push ebp
00000001 mov ebp,esp
00000003 push eax
00000004 cmp dword ptr ds:[002B2E14h],0
0000000b je 00000012
0000000d call 5ED1E089
00000012 xor edx,edx
00000014 mov dword ptr [ebp-4],edx
00000017 mov dword ptr [ebp-4],0Ah
0000001e nop
0000001f jmp 00000017
, , . , , a, .
, , , , std::vector: , , , , , . :
for () {
std::vector<int> a;
}
std::vector<int> a;
for () {
}
, : -:
for () {
char *a = new char[100];
}
char *a = new char[100];
for () {
}