No, you can't get it like that, that'll give you the size of an
int
which is what
a
is pointing to.
In your example you'll have to do
sizeof(int) * 1000
to get the allocated amount, because all you hold a reference to is the pointer, not an array. To get the amount allocated by the array it has to be declared as an array
(for example
int myArray[1000];
).
Consider the following three examples:
int* a = new int[1000];
int b[1000];
char c[] = "Hello, world!";
cout << "sizeof(int) * 1000 = " << (sizeof(int)*1000) << endl;
cout << "sizeof a = " << (sizeof a) << endl;
cout << "sizeof *a = " << (sizeof *a) << endl;
cout << "sizeof b = " << (sizeof b) << endl;
cout << "sizeof *b = " << (sizeof *b) << endl;
cout << "sizeof c = " << (sizeof c) << endl;
cout << "sizeof *c = " << (sizeof *c) << endl;
Which will print something like:
sizeof(int) * 1000 = 4000
sizeof a = 4
sizeof *a = 4
sizeof b = 4000
sizeof *b = 4
sizeof c = 14
sizeof *c = 1
Hope this helps,
Fredrik