Why are you trying to find out what your PC knows or doesn't know? When you are learning programming languages, you don't question how your computer is able to do something. You should focus more on learning the language more than trying to think what's happening in the background. Eventually, when you've studied enough, you'll realise that your PC is nothing but a stupid device that can perform just simple math operations and move things from one location in memory to another.
When you clear the vector, it gets rid of all the heap allocated memory and sets its inner size to 0. You'll understand better once you've covered OOP. For now, stop diving into thought experiments about what the PC/Computer/Compiler/Interpreter/CPU do and focus more on the important aspects of the language. There is so much that happens in the background that it'd just be a bad experience if you don't focus on the language and instead pay attention to low level implementation details directly.
This is the pseudo-representation/disassembly of what your computer understands based on the above code you described:
Code:
0x401350 lea 0x4(%esp),%ecx
0x401354 and $0xfffffff0,%esp
0x401357 pushl -0x4(%ecx)
0x40135a push %ebp
0x40135b mov %esp,%ebp
0x40135d push %edi
0x40135e push %esi
0x40135f push %ebx
0x401360 push %ecx
0x401361 sub $0x68,%esp
0x401364 movl $0x401880,-0x44(%ebp)
0x40136b movl $0x4b67c0,-0x40(%ebp)
0x401372 lea -0x3c(%ebp),%eax
0x401375 lea -0x18(%ebp),%ebx
0x401378 mov %ebx,(%eax)
0x40137a mov $0x4013f6,%edx
0x40137f mov %edx,0x4(%eax)
0x401382 mov %esp,0x8(%eax)
0x401385 lea -0x5c(%ebp),%eax
0x401388 mov %eax,(%esp)
0x40138b call 0x4279c0 <_Unwind_SjLj_Register>
0x401390 call 0x427150 <__main>
0x401395 lea -0x24(%ebp),%eax
0x401398 mov %eax,%ecx
0x40139a call 0x4a20cc <std::vector<int, std::allocator<int> >::vector()>
0x40139f movl $0x0,-0x28(%ebp)
0x4013a6 mov -0x28(%ebp),%eax
0x4013a9 cmp $0x9,%eax
0x4013ac jg 0x4013d3 <main()+131>
0x4013ae lea -0x24(%ebp),%eax
0x4013b1 lea -0x28(%ebp),%edx
0x4013b4 mov %edx,(%esp)
0x4013b7 movl $0x1,-0x58(%ebp)
0x4013be mov %eax,%ecx
0x4013c0 call 0x4a206c <std::vector<int, std::allocator<int> >::push_back(int const&)>
0x4013c5 sub $0x4,%esp
0x4013c8 mov -0x28(%ebp),%eax
0x4013cb add $0x1,%eax
0x4013ce mov %eax,-0x28(%ebp)
0x4013d1 jmp 0x4013a6 <main()+86>
0x4013d3 lea -0x24(%ebp),%eax
0x4013d6 mov %eax,%ecx
0x4013d8 call 0x4a2048 <std::vector<int, std::allocator<int> >::clear()>
0x4013dd movl $0x0,-0x60(%ebp)
0x4013e4 lea -0x24(%ebp),%eax
0x4013e7 mov %eax,%ecx
0x4013e9 call 0x4a2110 <std::vector<int, std::allocator<int> >::~vector()>
0x4013ee mov -0x60(%ebp),%eax
0x4013f1 mov %eax,-0x60(%ebp)
0x4013f4 jmp 0x40141b <main()+203>
0x4013f6 lea 0x18(%ebp),%ebp
0x4013f9 mov -0x54(%ebp),%eax
0x4013fc mov %eax,-0x60(%ebp)
0x4013ff lea -0x24(%ebp),%eax
0x401402 mov %eax,%ecx
0x401404 call 0x4a2110 <std::vector<int, std::allocator<int> >::~vector()>
0x401409 mov -0x60(%ebp),%eax
0x40140c mov %eax,(%esp)
0x40140f movl $0xffffffff,-0x58(%ebp)
0x401416 call 0x428020 <_Unwind_SjLj_Resume>
0x40141b lea -0x5c(%ebp),%eax
0x40141e mov %eax,(%esp)
0x401421 call 0x427b20 <_Unwind_SjLj_Unregister>
0x401426 mov -0x60(%ebp),%eax
0x401429 lea -0x10(%ebp),%esp
0x40142c pop %ecx
0x40142d pop %ebx
0x40142e pop %esi
0x40142f pop %edi
0x401430 pop %ebp
0x401431 lea -0x4(%ecx),%esp
0x401434 ret
A slightly more readable version:
Code:
;4 : {
0x401350 lea 0x4(%esp),%ecx
0x401354 and $0xfffffff0,%esp
0x401357 pushl -0x4(%ecx)
0x40135a push %ebp
0x40135b mov %esp,%ebp
0x40135d push %edi
0x40135e push %esi
0x40135f push %ebx
0x401360 push %ecx
0x401361 sub $0x68,%esp
0x401364 movl $0x401880,-0x44(%ebp)
0x40136b movl $0x4b67c0,-0x40(%ebp)
0x401372 lea -0x3c(%ebp),%eax
0x401375 lea -0x18(%ebp),%ebx
0x401378 mov %ebx,(%eax)
0x40137a mov $0x4013f6,%edx
0x40137f mov %edx,0x4(%eax)
0x401382 mov %esp,0x8(%eax)
0x401385 lea -0x5c(%ebp),%eax
0x401388 mov %eax,(%esp)
0x40138b call 0x4279c0 <_Unwind_SjLj_Register>
0x401390 call 0x427150 <__main>
;5 : std::vector <int> v;
0x401395 lea -0x24(%ebp),%eax
0x401398 mov %eax,%ecx
0x40139a call 0x4a20cc <std::vector<int, std::allocator<int> >::vector()>
0x4013e4 lea -0x24(%ebp),%eax
0x4013e7 mov %eax,%ecx
0x4013e9 call 0x4a2110 <std::vector<int, std::allocator<int> >::~vector()>
0x4013ee mov -0x60(%ebp),%eax
0x4013f1 mov %eax,-0x60(%ebp)
0x4013f4 jmp 0x40141b <main()+203>
0x4013f6 lea 0x18(%ebp),%ebp
0x4013f9 mov -0x54(%ebp),%eax
0x4013fc mov %eax,-0x60(%ebp)
0x4013ff lea -0x24(%ebp),%eax
0x401402 mov %eax,%ecx
0x401404 call 0x4a2110 <std::vector<int, std::allocator<int> >::~vector()>
0x401409 mov -0x60(%ebp),%eax
0x40140c mov %eax,(%esp)
0x40140f movl $0xffffffff,-0x58(%ebp)
0x401416 call 0x428020 <_Unwind_SjLj_Resume>
0x40141b lea -0x5c(%ebp),%eax
0x40141e mov %eax,(%esp)
0x401421 call 0x427b20 <_Unwind_SjLj_Unregister>
;7 : for (int i = 0; i < 10; i++)
0x40139f movl $0x0,-0x28(%ebp)
0x4013a6 mov -0x28(%ebp),%eax
0x4013a9 cmp $0x9,%eax
0x4013ac jg 0x4013d3 <main()+131>
0x4013c8 mov -0x28(%ebp),%eax
0x4013cb add $0x1,%eax
0x4013ce mov %eax,-0x28(%ebp)
0x4013d1 jmp 0x4013a6 <main()+86>
;8 : v.push_back(i);
0x4013ae lea -0x24(%ebp),%eax
0x4013b1 lea -0x28(%ebp),%edx
0x4013b4 mov %edx,(%esp)
0x4013b7 movl $0x1,-0x58(%ebp)
0x4013be mov %eax,%ecx
0x4013c0 call 0x4a206c <std::vector<int, std::allocator<int> >::push_back(int const&)>
0x4013c5 sub $0x4,%esp
;10 : v.clear ();
0x4013d3 lea -0x24(%ebp),%eax
0x4013d6 mov %eax,%ecx
0x4013d8 call 0x4a2048 <std::vector<int, std::allocator<int> >::clear()>
;12 : return 0;
0x4013dd movl $0x0,-0x60(%ebp)
;13 : }
0x401426 mov -0x60(%ebp),%eax
0x401429 lea -0x10(%ebp),%esp
0x40142c pop %ecx
0x40142d pop %ebx
0x40142e pop %esi
0x40142f pop %edi
0x401430 pop %ebp
0x401431 lea -0x4(%ecx),%esp
0x401434 ret
This is for the code:
Code:
#include <bits/stdc++.h>
int main (void)
{
std::vector <int> v;
for (int i = 0; i < 10; i++)
v.push_back(i);
v.clear ();
return 0;
}
As you can notice, your PC doesn't know anything. It's just a stupid machine that can do simple math operations and move things around and jump through memory locations using complex circuitry. It's you, the programmer, that makes it do cool things by exploiting these simple facts. And to be a good programmer, learning the language aspect is more important, at first, than figuring out "how PC can know" things.