Click here to Skip to main content
15,886,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i type the code below:
C#
int arraysize;
    std::cin>>arraysize;
    int* arr=new int[arraysize];
    std::cout<<sizeof(arr)<<" - "<<arraysize;

I enter 9 as the arraysize.
but it prints 4 - 9.

I want to get number of the elements.

Thanks,
Posted
Updated 28-May-13 2:50am
v3
Comments
Mohibur Rashid 28-May-13 7:22am    
it printed correct data. when you read the size of a pointer it return size of pointer not what you allocate
m.r.m.40 28-May-13 7:35am    
is there any code like 'sizeof' that returns the size that i allocate?

You are stumbling over two subtleties of the C++ language:

(1) When you allocate an array dynamically (by new) your store a pointer to the first element. But that pointer no longer contains the length of the array. If you ask for "sizeof arr" you will get the size of that pointer and not the size of the array. Had you defined your array as

C++
int myArray [10];


then "sizeof myArray" would return the size of the array in bytes.

(2) You are confusing the number of elements and the length in bytes of an array. Your arraysize variable hold the number of elements. The size of the array in bytes would be x-times larger, x being the length of a single int variable.
 
Share this answer
 
Comments
m.r.m.40 28-May-13 7:42am    
thank you,
I'm looking for the number of elements of the array, as you said.
nv3 28-May-13 8:47am    
Why don't you use std::vector for storing the array. That frees you of the burden of correctly deallocating the memory of your array (delete []) and you can ask for the number of elements with the size() member function. Much better than handling the these things in your own code.
Hi,

it is simple. By sizeof(arr) You are just asking what is the size of POINTERG TO INT, because arr is pointer to int (see declaration int* arr). And the size of a pointer is always 4B (32b) on 32b OS.

If you want to print out the size of the whole array in bytes, you have to multiply the size of a single byte by the lenght of the array:
C++
int arraysize;
std::cin >> arraysize;
int* arr = new int[arraysize];
std::cout<< sizeof(int)*arraysize <<" - "<<arraysize;

Hope this helps.

Best regards,
J.K.
 
Share this answer
 
v2
If you declare a static array
int arr[9]
then you can use the _countof() macro to get the number of elements in the array. It is impossible to do that with a dynamically allocated array. Those you have to keep track of yourself.
 
Share this answer
 
Its printing 4 because the size of int* arr< is 4. (its the size of the pointer)
If you do the following: sizeof(arr) * arraysize you should get to the size of the array in bytes. However I personally would rather do this: sizeof(int) * arraysize in order to get the array size. This is because the size of arr is dependant of how system allocates its memory (16bit, 32bit, 64bit, ect. memory address allocation) Thus if system had 64 bit memory address allocation, sizeof(arr) would of been 8 bytes.

Please note that if voting, take into consideration the original version of the question which didn't state 'I want to get number of the elements.'.
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900