Getting gibberish when writing to an array

sizeof() give the size of the array in bytes. An int is 2 bytes x 5 elements = 10 bytes, thus the size result is correct. The reason the last 5 values read from the array are gibberish is because you're reading past the end of the array and getting whatever happens to be in memory at those locations.

The correct way to calculate the number of elements in an array is this:

sizeof(test)/sizeof(test[0])

That will work no matter what type you use.