Size of an 2dim array - c++

When I have something like this:
int Numbers[] =
{
1,
2,
3,
}
I can simply get size of this array 'int numNumbers = ARRAYSIZE(Numbers);'
And my question is... Will this work for array like this:
int Numbers[][2] =
{
{ 1,2 },
{ 3,4 },
{ 5,6 },
}

I found this definition of ARRAYSIZE:
#define ARRAYSIZE(a) (sizeof(a) / sizeof(*(a)))
This definitely works for both single and two dimensional arrays. sizeof(a) is size of array a i.e. (number of elements in array) * (size of an element) where sizeof (*a) is size of an element.
Similarly for two dimensional case, sizeof(a) is number of elements in complete array and sizeof(*a) means size of array in one of its dimension.

Assuming ARRAYSIZE is something like:
#define ARRAYSIZE(a) (sizeof(a)/sizeof((a)[0]))
Then yes, it will, because in the second case a[0] is the same as "array of 2 integers", so sizeof(a[0]) will evaluate to sizeof(int)*2.
ARRAYSIZE() will always work with arrays of arbitrary dimension, just be careful not to use it inside a function where the array was passed as an argument, since you will not have a real array anymore in that case.

Related

Creating a 2-dimensional array with different sizes within a loop [duplicate]

This question already has answers here:
Do jagged arrays exist in C/C++?
(12 answers)
Closed 1 year ago.
The problem
I have an array of int's which vary in value called sizes. I want to create a 2d array (called arr) where the first dimension size is the length of the sizes array, and the second dimension's size is the value of the integer at that index. I hope the following code makes it easier to understand my question.
int sizes[4] = { 1, 2, 3, 4 };
// What I need is an array with this shape
{
{0}, // arr[0]
{ 0, 0 }, // arr[1]
{ 0, 0, 0 }, // arr[2]
{ 0, 0, 0, 0 } // arr[3]
};
My actual code, the sizes array is larger, so I would like to avoid lots of repeating lines of code if that is possible.
My attempts
I am new to C++, but I have read about constexpr. I think I can create a function to return this array, but I have yet to get this to work.
I also think I can loop over the sizes array, and for each int, create an array with that size, but I can't then assign it to arr.
C++ doesn't support jagged 2d arrays. A 2d array needs to have a size of N x M where N and M are both greater than zero.
Instead of using a 2d array, you can use a 2d vector to get this behavior like
std::vector<std::vector<int>> table;
for (auto size : sizes)
table.push_back(std::vector<int>(size));

Array larger than specified size in C++ [duplicate]

This question already has answers here:
c++ sizeof(array) return twice the array's declared length
(6 answers)
How do I find the length of an array?
(30 answers)
Closed 4 years ago.
When testing an array like this:
float test [3] = {1, 2, 3};
cout << "Test size: " << sizeof(test) << endl;
The size shown by the print statement is 12. Similarly, when I made the size 12, the number of elements in the array was 47. Isn't this the correct way to specify the size of an array in C++?
sizeof doesn't return the number of elements in the array, it returns the memory size in bytes. Since your array contains three regular integers, and integers are 4 bytes in size, that's 3 * 4 == 12 bytes.
To get the length of the array, you have a few options:
Option 1
int size = sizeof(test) / sizeof(test[0]);
What that does is get the size of the array (which is 12 bytes) then divides that by the size of a single element (which is 4 bytes). Obviously 12 / 4 == 3.
Option 2
As pointed out by #PaulMcKenzie, if you're able to use C++17 you can use std::size. That makes it very easy because you can just do this:
int size = std::size(test);
Option 3 (Not recommended)
int size = *(&test + 1) - test;
Which is a clever hack using pointers, explained here. Will result in undefined behaviour and may break, depending on the compiler and its optimisations.
If you want the more efficient way of calculating the number of elements in an array, you could use templates.
template<typename T, std::size_t Size>
std::size_t GetArrLength(T(&)[Size]) { return Size; }
If we call it as such:
float test[3] = {1,2,3};
GetArrLength(test);
The compiler will try to deduce the template parameters. For the parameter type to match what you're passing, T must be float and size must be 3 (making the parameter a reference to an array of 3 floats).
You must use following way to get the actual size of array you requires.
int size = sizeof(test)/sizeof(test[0]);
sizeof() actually returns no. of bytes. So, you get the no. of bytes in your array object. To get actually the no. of elements, we need to divide it by the size of an element there. For example, here sizeof(float)
You need to divide the single element size to get the array size.
int sz = sizeof(test) / sizeof(test[0]);
cout << "test size: " << sz << endl;
This will work :
size_t n = sizeof(test)/sizeof(test[0]);
size_t is a type guaranteed to hold any array index, so use size_t.
The standard library has a type trait for this purpose.
#include <type_traits>
#include <iostream>
int main()
{
float test [3] = {1, 2, 3};
std::cout
<< std::extent<decltype(test)>::value
<< '\n';
}

How does C++ find the size of an array?

Difference From Other Questions
I am not asking how to find the size, but how the computer finds the size.
Goal
I want to find out how C++ finds the size of an array (using sizeof(array)), and a 2D array (using sizeof(array)).
When I ran the code, I thought the output would be 3 and 6. But it was 12 and 24!? How do I make the the output 3 and 6?
I don't know how to calculate the size of an array, so when I say "an output of three/six", I mean the amount of numbers in the array.
Code
#include <iostream>
using namespace std;
int main()
{
int oneDArray [3] = {1, 2, 3};
int twoDArray [2][3] = {{1, 2, 3}, {1, 2, 3}};
cout << sizeof(oneDArray) << "\n" << sizeof(twoDArray);
return 0;
}
The sizeof operator returns the size in bytes, not elements.
For single dimensional arrays (not passed as a function argument, which would cause decay to a pointer), you can get the element count by dividing the size of the array by the size of the first element, e.g.
sizeof(oneDArray) / sizeof(oneDArray[0]) // Gets 3
For multidimensional arrays, that would tell you the size of the first dimension, not the number of elements total, so you'd need to drill down deeper:
sizeof(twoDArray) / sizeof(twoDArray[0]) // Gets 2 (first dimension of array)
sizeof(twoDArray) / sizeof(twoDArray[0][0]) // Gets 6 (number of elements)
You can of course explicitly name the type of an element to avoid nested indexing (sizeof(twoDArray) / sizeof(int) get 6 just fine), but I avoid it as a rule; it's too common for an array to change types during development (say, it turns out you need to use int64_t because you have values into the trillions), and if the whole expression isn't in terms of the array, it's easy to overlook it and end up with broken code.
sizeof returns bytes, if you expect number of elements, divide by the size of each element
cout << sizeof(oneDArray)/sizeof(int) << "\n" << sizeof(twoDArray)/sizeof(int);
Output:
3
6

How do sizeof(arr) / sizeof(arr[0]) work?

When looking for a size of an array in a for loop I've seen people write
int arr[10];
for(int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++){}
How is sizeof(arr) / sizeof(arr[0]) the length of the array? How does it technically work?
If you have an array then sizeof(array) returns the number of bytes the array occupies. Since each element can take more than 1 byte of space, you have to divide the result with the size of one element (sizeof(array[0])). This gives you number of elements in the array.
Example:
std::uint32_t array[10];
auto sizeOfInt = sizeof(std::uint32_t); // 4
auto numOfBytes = sizeof(array); // 10*sizeOfInt = 40
auto sizeOfElement = sizeof(array[0]); // sizeOfInt = 4
auto numOfElements = sizeof(array) / sizeof(array[0]); // numOfBytes / sizeOfElement = 40 / 4 = 10
LIVE EXAMPLE
Note that if you pass an array to a function, the above won't work since the array decays to a pointer and sizeof(array) returns the size of the pointer.
std::size_t function(std::uint32_t a[]) // same for void function(std::uint32_t a[10])
{
return sizeof(a); // sizeof(std::uint32_t*)!
}
std::uint32_t array[10];
auto sizeOfArray = function(array); // array decays to a pointer inside function()
LIVE EXAMPLE #2
As it is described in the C++ Standard (5.3.3 Sizeof)
1 The sizeof operator yields the number of bytes in the object
representation of its operand. The operand is either an expression,
which is an unevaluated operand (Clause 5), or a parenthesized
type-id.
In this expression
sizeof(arr) / sizeof(arr[0])
there are used two subexpressions with the sizeof operator.
This subexpression
sizeof(arr)
yields the number of bytes occupied by array arr (I suppose that arr is an array).
For example if you declared an array like
int arr[10];
then the compiler has to reserve memory that to hold 10 elements of type int. If for example sizeof( int ) is equal to 4 then the compiler will reserve 10 * 4 = 40 bytes of memory.
Subexpression
sizeof(arr[0])
gives the number of bytes occupied by one element in the array. You could use any index as for example
sizeof(arr[1000])
because the expression is unevaluated. It is only important the size in bytes of the object (an element of the array) used inside the operator.
Thus if you know the total bytes that were reserved for an array
sizeof(arr)
and know how many bytes each element of the array occupies (all elements of an array have the same size) then you can calculate the number of elements in the array by using the formula
sizeof(arr) / sizeof(arr[0])
Here is a simple relation. If you have an array of N elements of type T
T arr[N];
and you know the size of the memory occupied by the array then you can calculate the size of its element by using formula
sizeof( arr ) / N == size of an element of the array.
And vice verse
If you know the size of the memory occupied by the array and the size of its element you can calculate the number of elements in the array
sizeof( arr ) / sizeof( a[0] ) == N - number of elements in the array
The last expression you can rewrite also the following way
sizeof( arr ) / sizeof( T ) == N - number of elements in the array
because the elements of the array have type T and each element of the array occupies exactly the number of bytes that are required to allocate an object of type T.
Take into acccount that usually beginners make such an error. They pass an array as an argument to a function. For example let's assume that you have a function
void f( int a[] )
{
// ...
}
And you pass to the function your array
int arr[10];
f(arr);
then the function uses the pointer to the first element of the array. In fact the function has declaration
void f( int *a )
{
// ...
}
So if you write for example within the function
void f( int *a )
{
size_t n = sizeof( a ) / sizeof( a[0] );
// ...
}
then as a within the function is a pointer (it is not an array) then you will get something like
void f( int *a )
{
size_t n = sizeof( int * ) / sizeof( int );
// ...
}
Usually the size of a pointer equal to either 8 or 4 bytes depending of the used environment. And you won't get the number of elements. You will get some weird value.
int - is equal to 4 bytes
sizeof(int) it means: 1 * 4 = 4
int arr[10] - is holding 10 int
sizeof(arr) it means: 10 * 4 = 40, we got 10 int and every int got 4 bytes,, arr without the [] it means all the arr.
sizeof(arr[0]) it means: 1 * 4 = 4
sizeof(arr) / sizeof(arr[0]) = 10*4 / 1*4 = 10,, and it is the length of the array.
It only works if arr has not been decayed into a pointer, that is, it is an array type, not a pointer type.
sizeof(arr) is the total size occupied by the array.
sizeof(arr[0]) is the size of the first element in the array. (Note that zero length arrays are not permitted in C++ so this element always exists if the array itself exists).
Since all the elements will be of the same size, the number of elements is sizeof(arr) / sizeof(arr[0]).
When dealing with an array (some_type name[some_size]) sizeof(name) is how many bytes the array occupies. Dividing the total size of the array by the size of one element (sizeof(name[0])) gives you how many elements are in the array.
c++ way to use extent, which allows u to get a number of elements in Nth dimension of the array.
see http://en.cppreference.com/w/cpp/types/extent for details
int values[] = { 1 };
std::extent<decltype(values)>::value == 1
Let's take an example like the arr[]={1,2,4,3,5}.
Then the size of the array will be 5 and the size of arr[0] will be "1" BECAUSE it consists of an element in it. Basically it's a subarray the from the above problem it will be 5/1 it will automatically returns the size of array =5.
The difference is
int arr[5] = {1,2,3,4,5};
here arr is Pointer to array and &arr[0] is pointer of type Integer

fill_n() is not letting change variable value

I am using fill_n() function to initialize my array values, but then I cannot change the values of the array, it is always the initial value.
Can someone explain me why is that?
#include<iostream>
#include<ctime>
int main(){
//Matrix
int m[3][3] = {{1,0,0},{0,1,0},{0,0,1}};
int v[3] = {5,6,7};
int result[3];// = {0,0,0};
int n = 0, i, j;
std::fill_n(result,sizeof(result),0);
clock_t time = clock();
while(n<1000){
n++;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
result[j] = result[j]+(v[i]*m[j][i]); //changing the values here.
}
}
}
time = clock() - time;
for(i=0;i<3;i++){
std::cout<<result[i]<<"\n";
}
std::cout<<"Execution time: "<<((float)time)/CLOCKS_PER_SEC<<"\n";
return 0;
}
The output of the code is always 0.
By std::fill_n(result,sizeof(result),0); you are asking to fill the first sizeof(result) elements by 0.
However, only 3 elements are available in result. Thus you wrote out-of-bound and led to undefined behavior. You should write std::fill_n(result, sizeof(result) / sizeof(result[0]), 0); instead.
Note that sizeof(result) returns size of result in bytes, not in elements.
sizeof(result) returns the number of bytes held by the variable result. Since result is an array of 3 ints, its size in memory is 3 * sizeof(int), which (unless sizeof(int) is 1) much greater than 3. Thus you are writing pass array bounds and incurring undefined behavior.
The actual way to calculate the size would be to divide the entire size of result by its element type. That is, sizeof(result) / sizeof(int).
If you want to initialize each element to 0, a simpler way to do it would be to value-initialize the array:
int result[3]{}; // or = {} (pre C++11)
Number of elements (or size) of an array of type type would be sizeof(array) / sizeof(type). Second argument required for fill_n is the number of elements to fill a container (in your case, an array of type int) and not the size of an array in bytes. Naturally, sizeof(array) returns sizeof(type) times number of elements.