The way you calculate the length
of your vector is wrong:
length = sizeof(x.capacity() - 1) / sizeof(int);
since capacity()
returns the size of the storage space currently allocated for the vector, expressed in terms of elements and thus your length
is equal to 1
in your example.
You should use size()
instead:
length = x.size();