I'm trying to find the size of an array, and sizeof isn't working properly presumably because my array is a pointer, not an actual array (then again, I'm probably wrong). I'm new to C++, but not to programming.
Here's my function:
int getSizeOfPointerArray(int a[]){
int n=0;
while(true){
if(!a[n]){
cout << "a[" << n << "] doesn't exist, breaking" << endl;
break;
}
cout << "a[" << n << "] exists with value " << a[n] << " at memory address " << &a[n] << endl;
n++;
}
return n;
}
The function was called with the argument p where p was:
p = new (nothrow) int[f];
'f' was 3. The elements of the array were collected with:
for(n=0;n<f;n++){
string c = ((n!=f-1)?", ":" ");
cout << p[n] << c;
}
I was expecting to see the memory locations of the three elements of the array printed in the output - each four apart - and it to say that a[4] doesn't exist. Instead, it printed six memory addresses. The first three values were correct (all 3s), but the last three were -33686, -1414812757 and another -1414812757.
Why is this? Can I just divide the end result by 2, or is there not always double the elements you assigned? Is this the same with non-dynamic arrays?