I want to find out the size of ptr, i mean what is the ptr[max].
Code:
#include <iostream>
using namespace std;
int main(void) {
int arraytest[] = { 0, 1, 2, 3, 4, 5, 6 , 7, 8 ,9};
cout << "sizeof(arraytest): " << sizeof(arraytest) << endl; //working
double *ptr = NULL;
int input = 0;
int i = 0;
while (1)
{
cout << "Enter digit, 0 to end." << endl;
cin >> input;
if (input == 0)
{
break;
}
ptr = static_cast <double *> ( realloc(ptr, ((i + 1) * sizeof(*ptr))) );
ptr[i] = i;
i++;
cout << "sizeof(*ptr): " << sizeof(*ptr) << endl; //not working, wrong size
}
free(ptr);
ptr = NULL;
return EXIT_SUCCESS;
}
For example if I would want to output all items of arraytest I could write
Code:
for(int j = 0; j < (sizeof(arraytest) / sizeof(int)); j++) {
cout << arraytest[j] << endl;
}
Which in find quite generic.
For the dynamic array there is no such nice way. I could only work around and count that manually (variable i).
Is there any nice way like with sizeof?