I am trying to add to a third vector. The addition and multiplication of two different vectors. However I can't understand why the output values are not as expected.
The methods I use are: show, add and perform Product.
void mostrar(int vecAMostrar[], int tamanioVector){
for(int i = 0; i < tamanioVector ; i++){
cout << vecAMostrar[i] << endl;
}
return;
}
void suma(int vec1[], int vec2[], int vecResultado[], int tamanioAceptado){
for(int i = 0; i < tamanioAceptado; i++){
vecResultado[i] = vec1[i] + vec2[i];
}
return;
}
void producto(int vec1[], int vec2[], int vecResultado[], int tamanioAceptado){
for(int i = 0; i < tamanioAceptado; i++){
vecResultado[i] = vec1[i] * vec2[i];
}
return;
}
But, when I call them in main:
int main()
{
int vecUno[] = {1, 2, 3, 4, 5};
int vecDos[] = {1, 2, 3, 4, 5};
int vecSuma[] = {};
int vecProducto[] = {};
producto(vecUno, vecDos, vecProducto, 5);
cout << "PRODUCTO" << endl;
mostrar(vecProducto, 5);
suma(vecUno, vecDos, vecSuma, 5);
cout << "SUMA" << endl;
mostrar(vecSuma, 5);
return 0;
}
What I get is that the second output is wrong. Am I doing something wrong in main or functions?
The problem is that it seems that the memory is not being handled well and vec2 takes the values of the last operation, I was testing and if you think the arrangements are fixed, there is no problem with the final result, try to make sure:
These are the lines where you initialize the vectors.
You are using two different language features that combined in the wrong way cause your code to behave erratically.
Implicit array size deduction.
The C++ language (and also C) allows the size of an array to be omitted if the elements are explicitly given to it when initializing it; in that case the compiler will count the elements saving the programmer that tedium, in your case:
Since when you initialize
vecSuma
yvecProducto
you have given them an empty list, the element count of those lists is zero, so they contain zero elements; on the other handvecUno
andvecDos
contain five elements because the element count of the list used to initialize them is five; in memory they would look something like:As you can see, they
vecSuma
neithervecProducto
point to memory space because they have 0 elements!Array-to-pointer decay.
Passing an array to a function without indicating its size is equivalent to passing a pointer pointing to the first element of the array, so your functions
mustrar
,suma
andproducto
could be rewritten to accept pointers without affecting their behavior:However, if you had specified the size in the array parameter of the functions (instead of adding it as an additional parameter) you would have gotten a compile-time error:
By passing the parameters as a reference to an array of a given size (that weird parameter of
tipo (&nombre)[tamaño]
) we prevent the array from decaying to a pointer and so the compiler is forced to check if the passed parameter matches the expected parameter; as we can see in the errors does not match: an array of no elements is not an array of five elements.What happened on your show?
We have seen that
vecSuma
andvecProducto
do not point to any memory space (of course, they have zero elements) so by indexing and writing to them you are forcing the program into undefined behavior which could make the program run without visible errors, or could lead to corruption. out of memory (displaying wrong values), or it could wipe your hard drive, cause world war 3, or summon demons inside your nostrils .How to solve it?
As Dariel Ramos Díaz de Villegas has already commented, a possible solution is to assign size to the destination arrays (although he has not explained why this solves it) in this way you avoid the undefined behavior since the arrays
vecSuma
andvecProducto
will point to a sufficiently large memory space. great for assigning the results of operations; If you are going to use prefixed-size arrays, I would also advise using the type of these arrays as the type of the parameters, thus ensuring that the compiler detects errors at compile time:In the above code we have defined an alias for the type
int[5]
(an array of five integers) and used it to declare the function parameters, you can see the working code here ; note that also the parameters that will be used only to be read are passed as constants, this is considered a good practice; Also keep in mind that I have removed the instructionreturn
at the end of the functions, it is not necessary because the function was going to end anyway.Another alternative, knowing the size beforehand, is to use a
std::array<int, 5>
instead of aint[5]
.Other things to keep in mind.
using namespace std;
headers (not applicable in your case), read this thread to find out why.\n
vsstd::endl
when possible (almost always), read this thread to find out why.