1

Someone gave this code as a method to find the length of an array called a:

std::cout << "Length of array = " << (sizeof(a)/sizeof(*a)) << std::endl;

The only thing I don't understand is

sizeof(*a)

I don't know what the * is doing here. I also assume it's a fraction. What's it doing in the denominator?

Tarick Welling
  • 3,119
  • 3
  • 19
  • 44
frankfurt
  • 29
  • 3
  • This may help: https://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c – Aziz Jun 06 '20 at 07:44
  • 1
    Turbo has `std`? – Ardent Coder Jun 06 '20 at 07:51
  • Recommendations: Switch to a C++11 compatible compiler, such as recent [GCC](http://gcc.gnu.org/) (perhaps [MinGW](http://mingw-w64.org/) on Windows) or [Clang](http://clang.llvm.org/). Read a good [C++ programming](http://stroustrup.com/programming.html) book and [more about C++](https://en.cppreference.com/w/cpp). Provide some [mre] in your question. Use [standard C++ containers](https://en.cppreference.com/w/cpp/container) – Basile Starynkevitch Jun 06 '20 at 07:55

5 Answers5

2

In C++, an int is 4 bytes. sizeof(*a) is getting the byte size of the type stored by your array. What (sizeof(a)/sizeof(*a)) is doing is dividing the total bytes stored by your array by the byte size of the array type, thus giving you the length.

Ex: you have an array [1, 2, 3, 4] which is 16 bytes, and the size of an int is 4 bytes. 16 bytes / 4 bytes = 4, the length of your array.

slick_reaper
  • 192
  • 8
  • I am not sure that an `int` is *always* 4 bytes. On [Arduino](http://arduino.cc/) it probably is 2 bytes. On some VLIW processors, it might be one word of 64 bits. You could find implementations of C++ with `sizeof(char)` and `sizeof(int)` being 1 (and 16 bits `char`) – Basile Starynkevitch Jun 06 '20 at 07:58
  • An `int` does not have to be 4 bytes. The only requirements are that it has to be at least 16 bits and at least as large as a `short`. Yes, in many implementations these days its 32 bits, but don't assume that it always is. – Pete Becker Jun 06 '20 at 12:43
1

sizeof(a)

is the size of the full array

sizeof(*a)

is the size of one element of the array, *a being the first element of the array, equivalent of a[0]

this is why you need to divide sizeof(a) by sizeof(*a) to have the number of elements

If you have an array of char because by definition sizeof(char) is 1 you do not need to divide by the size of an element, else you need.

For instance having an array of 10 int and supposing sizeof(int) is 4 the size of the array is 10*4=40, which is not the number of elements => you need to divide by the size of an element

int a[10];

std::cout << sizeof(a) << std::endl; // print 40 supposing sizeof(int) is 4
std::cout << sizeof(*a) << std::endl; // print 4 supposing sizeof(int) is 4
std::cout << (sizeof(a)/sizeof(*a)) << std::endl; // print 10 expected

Note to use sizeof(*a) is better than to use sizeof(int) because in case you change the type of the array sizeof(*a) will be still valid

bruno
  • 32,421
  • 7
  • 25
  • 37
0

*a is the first element in your array. The asterisk * is used to dereference the pointer, i.e. we're getting the value stored at that address.

The reason is that size(a) is going to return the size of the entire array in bytes. This may not be the size of the array, as there are often multiple bytes in an element. Therefore, we must divide by the size of an individual element, hence (sizeof(a)/sizeof(*a)).

Green-Avocado
  • 901
  • 5
  • 20
0

One way to do it if you know the data type is:

int size = sizeof(a)/sizeof(int)

*a is a reference to the first element of the array.

sizeof(*a) gives the amount of memory used by the first element of the array. For example, a standard int element on a 32-bit computer would use 4 bits.

sizeof(a) is the size of the whole array.

tersrth
  • 861
  • 6
  • 18
  • "an int element would use 4/8 bytes" -- or 2, or 16, or maybe even 18 bits. The **only** requirements on the size of an `int` is that it has to be at least 16 bits and at least as large as a `short`. – Pete Becker Jun 06 '20 at 12:46
  • @PeteBecker Yes. Thank you for clarifying. What I meant to write was an integer on a standard 32-bit computer is 4 bits. This size can be modified using keywords like short and long. – tersrth Jun 06 '20 at 12:58
  • I didn't flag it, but in the text that I quoted in my comment I changed "bits" to "bytes", because that's closer to correct; you're talking about 4 "bytes" but calling it 4 "bits". – Pete Becker Jun 06 '20 at 13:06
0

Implying a is the name of the array, sizeof(a) gives the size of the full array, and the (*a) is simply a pointer to the first element of the array i.e. a[0] hence holding one element (all of which are equal sizes), hence the size will divide the full array size/size of one element, equalling the number of slots or elements.

qedk
  • 468
  • 6
  • 18