Your size() function cannot work like you want it to because when you pass an array to a function, the array decays to a pointer to its first element. Your a decays to a int* and sizeof operator on a pointer returns the size of the pointer, not the array. Consider using a std::vector instead as this will allow you to retrieve the size of the vector each time you pass it to the function.