Arithmetic operation on multidimensional array - c++

I am doing some arithmetic operations on multidimensional array. Also tried some code using C++. But, output of my code contradict with my theoretical understanding on relation between different pointers and multidimensional array.
My understanding on 2D and 3D array.
2D Array
1)
Again, by using dereference operator
2)
3D Array
3)
Again, by using dereference operator
4)
I am confused about what B and C pointing in figure 2 and 4 respectively.
Code on 2D array
#include <iostream>
using namespace std;
int main(void){
int B[2][2] = {1, 2, 3, 4};
cout << B << endl;
cout << B+1 << endl;
return 0;
}
Output:
0x7ffec3dd9f80
0x7ffec3dd9f88
Here, pointer to int B is increased by ((1*4)+(1*4)) = 8 unit that is pointer to int (B+1).
Though I got one explanation from Link on what actually B points in 2D array. It points first element of 2D array.
Output of my code and explanation on this Link confused me.
What actually B points in 2D array?
And, If B points the address of first element of 2D array, then why value of B is increased by 8 unit when we add 1 with it? Why not increased by (1*4) unit?
Code on 3D array
#include <iostream>
using namespace std;
int main(void){
int C[2][2][2] = {{
{1, 2},
{3, 4}
},
{
{1, 2},
{3, 4}
}};
cout << C << endl;
cout << C[0] << endl;
cout << C[0] +1 << endl;
cout << C[0][0] +1 << endl;
return 0;
}
Output:
0x7ffeac58d130
0x7ffeac58d130
0x7ffeac58d138
0x7ffeac58d134
C gives starting address of whole array. But, which portion specifically it points? Is it pointing &C[0] or &C[0][0] or&C[0][0][0]?
cout << C[0]+1 << endl; here, C[0] is increased by 8 unit. But, if C[0] is pointing the base address of 1st 2D array inside 3D array, then it must be increased by 16 unit to get base &C[1].
cout << C[0][0] +1 << endl; here, C[0][0] is increased by 4 unit. But if it is pointing the base address of 1st 1D array of 1st 2D array inside 3D array, then it must be increased by 8 unit to get base &C[0][1].
Thank you.

Diagrams 1 and 3 are correct Diagrams 2 and 4 are not. *B and B[0] are defined to mean exactly the same thing. Similarly with **B and B[0][0].
So to fix Diagram 2, actually copy Diagram 1 but merely change the text B[0] to *B and so on. B by itself refers to the whole array. Ditto for Diagram 4.
B+1 is defined as meaning &(B[1]), it refers to a temporary pointer pointing at the object you have correctly marked as B[1] in Diagram 1.
In cout << B << endl;, treat B as saying B+0 (i.e. &(B[0])). When you use an array in a context where a pointer is expected, that is what happens (a temporary pointer is created, it behaves the same as if you had written &B[0] instead of B). That statement does not output B, it outputs the value of this temporary pointer.

Related

Calculating the number of elements in an array using pointer arithmetic

I have come across a piece of example code that uses pointers and a simple subtraction to calculate the number of items in an array using C++.
I have run the code and it works but when I do the math on paper I get a different answer.
There explanation does not really show why this works and I was hoping someone could explain this too me.
#include <iostream>
using namespace std;
int main() {
int array[10] = {0, 9, 1, 8, 2, 7, 3, 6, 4, 5};
int stretch = *(&array + 1) - array;
cout << "Array is consists of: " << stretch << " numbers" << endl;
cout << "Hence, Length of Array is: " << stretch;
return 0;
}
From: https://www.educba.com/c-plus-plus-length-of-array/
When I run the code I get the number 10.
When I print the results of *(&array + 1) and array by
cout << *(&array+1) << endl; cout << array << endl;
I get of course two hex address's.
When I subtract these hex numbers I get 1C or 28???
Is it possible that C++ does not actually give the hex results or their translation to decimal but rather sees these numbers as addresses and therefore only returns the number of address slots remaining?
That is the closest I can come to an explanation if some one with more knowledge than I could explain this I would be very grateful.
Let's take one step back and take it step-by-step to see if it will help. Continuing from my comment, the problem you are having difficulty with is one of type.
Let's take the array iteself:
int array[10] = {0, 9, 1, 8, 2, 7, 3, 6, 4, 5};
On access, an array is converted to a pointer to the first element in the array (e.g. the address of the first element) subject to caveats not relevant here. So when you say array, you have type int *, a pointer to the first element in array.
Now what happens when I take the address of the array? (&array in)
int stretch = *(&array + 1) - array;
When you take the address of the array, the result is the same address as array, but has type int (*)[10] (a pointer-to-array-of int[10]). When you add 1 to that pointer (recall type controls pointer arithmetic), you get the address for the pointer to the next array of int[10] in memory after array -- which will be 10 int after the first element of array.
So *(&array + 1) gives you the address to the next array of int[10] after array, and then dereference is only needed for type compatibility. When you dereference an int (*)[10] you are left with int[10] -- which on access gives you the address of the first element of that array (one after the original)
Think through the types and let me know if you have further questions.
You forgot a small detail of how pointer addition or subtraction works. Let's start with a simple example.
int *p;
This is pointing to some integer. If, with your C++ compiler, ints are four bytes long:
++p;
This does not increment the actual pointer value by 1, but by 4. The pointer is now pointing to the next int. If you look at the actual pointer value, in hexadecimal, it will increase by 4, not 1.
Pointer subtraction works the same way:
int *a;
int *b;
// ...
size_t c=b-a;
If the difference in the hexadecimal values of a and b is 12, the result of this subtraction will not be 12, but 3.
When I subtract these hex numbers I get 1C or 28 ???
There must've been a mistake with your subtraction. Your result should be 0x28, or 40 (most likely you asked your debugger or compiler to do the subtraction, you got the result in hexadecimal and assumed that it was decimal instead). That would be the ten ints you were looking for.
I will try it with 5 items
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int array[] {1,2,3,4,5};
int items= sizeof(array)/sizeof(array[0]);
cout << items << endl;
int items2 = *(&array +1) - array;
cout << items2 << endl;
cout << array << endl;
cout << *(&array +1) << endl;
return 0;
}
root#localhost:~/Source/c++# g++ arraySize.cpp
root#localhost:~/Source/c++# ./a.out
5
5
0x7fe2ec2800
0x7fe2ec2814
using https://www.gigacalculator.com/calculators/hexadecimal-calculator.php to subtract the numbers from each other
I get
14 hex
20 decimal.
that fits with the 4 bytes to an integer.
thanx guys :)
this is an edit done on the 12th of december melbourne time ::
I have still had questions on this topic and something did not fit right with me about the entire route to counting array items via this code.
I found something I think is interesting and again would love to know why ( I shall try to explain it as best I can my self anyway)
*(&array + 1) is the question.
lets have a look at it.
as arrays are at there very nature in c and c++ only pointers to the first element in the array how can this work.
I shall use a small set of cout calls to see if I can find out whats happening.
#include <iostream>
using namespace std;
int main(int argc, char* argv[]){
int array[] {1,2,3,4,5,6,7,8,9,10};
int size {0};
size = *(&array + 1) - array;
cout << "size = *(&array + 1) - array = " << size << endl;
cout << "*(&array + 1) = " << *(&array + 1) << endl;
cout << "(&array + 1) = " << (&array + 1) << endl;
cout << "(array + 1) = " << (array + 1) << endl;
cout << "&array = " << &array << endl;
cout << "array = " << array << endl;
cout << "*(&array) = " << *(&array) << endl;
cout << "*(array) = " << *(array) << endl;
cout << "*array = " << *array << endl;
return 0;
}
again this is off proot in my phone so still under root with no systemd.
root#localhost:~/Source/c++# g++ arrayPointerSize.cpp
root#localhost:~/Source/c++# ./a.out
size = *(&array + 1) - array = 10
*(&array + 1) = 0x7ff6a51798
(&array + 1) = 0x7ff6a51798
(array + 1) = 0x7ff6a51774
&array = 0x7ff6a51770
array = 0x7ff6a51770
*(&array) = 0x7ff6a51770
*(array) = 1
*array = 1
we see that as a pointer array can be called with * too derefernce the pointer and give the variable held in position [0] in the array.
when calling &array or reference too array we get the return of the address at the first position in the array or [0].
when calling just array we also get the address for the first position in the array or [0].
when calling *array the * is working as it does for pointers and it is dereferencing the arrays first position [0] to give the variable.
now things get a little interesting.
*(array) also dereferences the array as is seen by its value being given as 1 in this instance.
yet *(&array) does not dereference the array and returns the address to the first position in the array.
in this instance memory address 0x7ff6a51770 is the first spot in the array array = 0x7ff6a51770
and &array (reference to the pointer of the position in memory that is the first spot in the array) gives the same address 0x7ff6a51770.
it is also of note in this instance to remind us of the fact that *(&array) is also returning the first possition in the array and *(array) is not
so we can not dereference a pointer too a position in memory as its variable is the position in memory.
if array and &array give the same answer as array is a pointer too the memory position in the first spot in our array and a reference to
this pointer.
why the different answer for (array + 1) and (&array + 1).
we get the memory address 0x7ff6a51774 for (array + 1) which is in line with an integer taking four bytes on my linux or
the addition of four bytes in memory past the first spot in the array (second array spot) but (&array + 1) gives a different answer.
if we follow the bytes and the code we see that (&array + 1) actually gives us the memory address four bytes after the end of the array.
so pointer too memory address add one gives the amount of bytes the variable type is past the memory address for the start of the array
and the reference to the pointer too the memory address add one gives the address the amount of bytes the variable type is after the last ?? spot in the array.
how then can array and &array return the same answer if (array + 1) and (&array + 1) do not.
it seems to me that the & reference when working with arrays overloads the + operator when doing arithmatic.
this would explain the difference in answers as straight &array has no operator too overload so returns the same answer as calling for
straight array
this small peice of code also shows that the use of pointers using *(&array + 1) is a very bad way to show a way to find array size with
pointers as really arrays are pointers and *(&array + 1) and (&array + 1) give the same result.
the heavy work was really being done by the reference operator &.
I may still be missing something here as I have used cout directly with the different experssions and being a stream it may
be limited in its ability to take advantage of what the reference operator is really doing when working with arrays.
I am still learning this language but I shall for sure keep this in mind as I dive deaper into c++.
I believe other than a few other trials with variables that the true answer will be found when reading the source for GCC g++.
I am not ready for that yet.

Why does * need to be put before (&a) to subtract a (a is an array)?

I was learning how to find the length of an array and I'm baffled by this solution. I tried to find an explanation online but there seems to be none.
int arr[5] = {5, 8, 1, 3, 6};
int len = *(&arr + 1) - arr;
cout << "The length of the array is: " << len;
The memory address of the array is the same as the memory address of the first element, and when you add to or subtract from a pointer, it is done by the size of the type it points to, so:
arr refers to int, and &arr refers to int[5].
&arr+1 increments the memory address in the size of five integers.
If you do (&arr+1)-arr you get a compile error, because they are different types.
If you do (&arr+1)-&arr you get 1, because the offset of the memory address is the same as one size of int[5].
Therefore, when you do *(&arr+1), you get the same memory address but pointing to int and not int[5]. Now you wont get a compile error, because both pointers point to int and you get the offset of the memory address in terms of int size, and not int[5].
Memory addresses and types are quite difficult to explain sometimes, I hope I made it clear. Here you have some code you can run to see some of the concepts mentioned:
int arr[5] = {5, 8, 1, 3, 6};
int len = *(&arr + 1) - arr;
cout << "arr: " << arr << endl;
cout << "arr + 1: " << arr+1 << endl;
cout << "&arr: " << &arr << endl;
cout << "&arr + 1: " << &arr+1 << endl;
cout << "*(&arr + 1): " << *(&arr+1) << endl;
// cout << "&arr + 1 - arr: " << &arr+1-arr << endl;
// error: invalid operands of types ‘int (*)[5]’ and ‘int [5]’ to binary ‘operator-’
cout << "The length of the array is: " << len;
The type of the array arr is int[5], the type of &arr is int(*)[5]. (&arr + 1) increases the array address on sizeof(int[5]) as it's done by the rules of the pointer arithmetic, i.e. computes the address after the array. *(&arr + 1) is int[5], an array right after arr, where arr[5] would place. The both arguments of the substractions decay to int*. The substraction of pointers to int gives 5.
This may be considered as undefined behavior, since substraction of pointers belonging to different object storages is not defined. Also results of expressions with pointers addressing unallocated memory (like the (&arr + 1)) are undefined.
First, the traditional way to get the size of an array is sizeof a/sizeof *a. C++11 adds std::extent<decltype(a)>::value. There is of course no way to get an array’s size from just a pointer to it, as in
void f(int x[]) {/* no size here */}
Since an array is not a suitable operand to -, the array-to-pointer conversion occurs for the right-hand operand, producing an int*. Both operands must be of this type for the subtraction to result in a number of ints. &arr is of course a pointer to the array (of type int(*)[5], which conveys the size), and so therefore is &arr+1. Adding a * (or, equivalently, writing (&arr)[1]) produces an lvalue that supposedly refers to “the next array after arr”, which itself decays to a pointer that works with -.
However, as the indexing form indicates, this involves referring to an array that does not exist and is thus undefined behavior.

Printing array of doubles gives unexpected results

In a program I am writing I am experiencing unexpected output when printing data from an array. I have tried with float and double. Here is the code:
#include <iostream>
int main()
{
double vector[3]{ 193.09375 , 338.5411682 , -4.0 };
double pVecX{ 193.09375 };
double pVecY{ 338.5411682 };
double pVecZ{ -4 };
std::cout << std::dec << vector[1] << '\n' << vector[2] << '\n' << vector[3] << '\n' << '\n';
std::cout << std::dec << pVecX << '\n' << pVecY << '\n' << pVecZ << '\n';
system("Pause");
return 0;
}
This is the output:
338.541
-4
1.42292e-306
193.094
338.541
-4
Press any key to continue . . .
Issues:
I expected the vectors to print in reverse order from how they were entered into the array.
(Even though I ask for [1]..[2]..[3], it is printing [2]..[3]..[1] (I Think that is the order))
When part of the array, the number "193.09375" becomes a (seemingly) random notated number, and is different every time the program runs.
I was reading about variables and understand that a variable stored outside of the range it is initialized as can cause wrap-around, I just do not know why that is happening here. (I assume it is based on the negative notation.)
I am certain that I am missing something simple, and I am fairly new.
An Arrays index starts at 0. So when you say vector[3] you are actually going out of bounds.
You only have 0, 1, and 2 indices or subscripts. Although you do have 3 elements. 0 would refer to your first element, 1 would refer to your second element, and 2 would refer to your 3 element, and so on and so forth.
(Like I mentioned in my comment.)
You should have something like this instead:
std::cout << std::dec << vector[0] << '\n' << vector[1] << '\n' << vector[2] << '\n' << '\n';
This should fix your problem. Also consider using a std::vector.
Also read about why you should not use system("Pause");.
As mentioned in other answers, the valid indexes for an array of size 3 is 0, 1, and 2. Using any other index invokes undefined behavior.
You can also avoid explicitly indexing into the array, if you use a loop:
for (auto v : vector)
std::cout << std::dec << v << '\n';
vector[3] is outside the bounds of the array. Behaviour of the program is undefined. Valid indices are 0, 1 and 2.
I expected the vectors to print in reverse order from how they were entered into the array.
Why? The indexes you print are ordered from lower to higher (and the << print in the order of the source code, etc.). If you wanted to reverse them, you need to print first the highest index, 2, then 1, then 0,
When part of the array, the number "193.09375" becomes a (seemingly) random notated number, and is different every time the program runs.
The vector array goes from 0 to 2, not from 1 to 3. When you try to access vector[3], it is undefined behavior and the program will likely printing whatever memory ends up there. Every time you run the program that memory may contain different things, it is a fairly normal result of undefined behavior.
Arrays in C++ are zero indexed, that means that the first element is accessed by the index 0, e.g.
int array[3] {5,6,7};
So array[0] == 5, array[1] == 6, array[2] == 7.
The reason you get a random number is that you are trying to print an element of the array which never got defined. In the given example of my array above, if I would try to print element array[3], the corresponds to a certain place in memory (which is not part of my array) which can be filled by any value, thats called undefined bahavior).
If you want to print out every element of an array, you could make use of range based for loops:
for (auto a : my_array) std::cout << a << std::endl;

Endiannes of machine and array elements storing

I am concerned about how arrays are stored in C. for example I have such array:
unsigned char array[] = {1, 0};
I have Little endian machine so as far as I know elements of this array will be stored in memory in reversed order like [0, 1].
But when i execute such code :
std::cout << (void*)&array[0] << std::endl;
std::cout << (void*)&array[1] << std::endl;
I will get two addresses. The second one will be bigger by one.
I don't understand this because if the 0 element will be placed first in memory and then 1, so why the address of first element of array (value 1) is lesser than second element (value 0).

2-D array without column number gives address?

int arr[][3] = {{1,3},{2,3},{6,7}};
cout << arr[0] << endl; // W/O col' somehow give you the address, not the element.
cout << arr[0][0] << endl;
I am curious as to why arr[0] prints the address, not the element.
It seems like somehow it's ignoring its dereference operator.
I'd be appreciated if you could help me in terms of a pointer or in depth.
Thank you.
Since arr is a two-dimensional array, arr[0] will give you an entire row (more precisely, the address of that row).
More precisely, since arr is of type int[][3] arr[0] is of type int[3], which decays to int *. Hence, cout << arr[0] << endl; prints a pointer.
array[0] holds the address of the array[0][0...n-1] values so only cout << arr[0]; prints the address since arr[0] is the pointer to the values in arr[0][0..n-1]
If you want specific value of an two dimensional array then you need to give both x and y index like
array[0][0] ... array[0][n-1]