Reputation: 31
For my mini assignment, in a 30-floor building, I have to gather the floors people press in a lift, then find the difference between each floor.
So, I plan to set an array (We have only been taught arrays as our only container) of 30 floors. The people in the lift will click on the buttons of the lift, so assuming (5, 10, 14, 19, 29).
I then plan to pass this array into a function which will calculate the difference between each floor.
Here is my code so far, I know its wrong since its not compiling and I may be wrong somewhere else too.
Here is the error message:
main.cpp: In function 'int* calculateDiff(int*, int)': main.cpp:26:7: warning: address of local variable 'floorsDiffResult' returned [-Wreturn-local-addr]
CODE
#include <iostream>
#include <numeric>
#include <algorithm>
using std::cout;
using std::endl;
int* calculateDiff(int floors[], int floorsSize);
int main()
{
int floorsPressed[30] = {5, 10, 14, 19, 29};
int floorsCounter = 5;
int* ptr = calculateDiff (floorsPressed, floorsCounter);
int floorsDiffResult[30];
for (int i = 0; i < floorsCounter; i++)
{
floorsDiffResult[i] = *(ptr + i); //Storing the difference into floorsDiffResult array
cout << "Difference: " << *(ptr + i) << endl;
}
}
int* calculateDiff(int floors[], int floorsSize)
{
int floorsDiffResult[30]; //Create another array to store the difference for other calculations later on such as finding the biggest difference, average of the difference etc.
std::adjacent_difference(floors, floors + floorsSize, floorsDiffResult);
std::move(floors + 1, floors + floorsSize, floorsDiffResult); //First element does not give the difference
return floorsDiffResult;
}
Upvotes: 2
Views: 104
Reputation: 28
The scope of the function will delete the contents of any pointers created inside it. I suggest you pass the output as a 3rd argument in the function:
void calculateDiff(int floors[], int floorsSize, int floorDiffResult [])
{
//int floorsDiffResult[30]; //Create another array to store the difference for other calculations later on such as finding the biggest difference, average of the difference etc.
std::adjacent_difference(floors, floors + floorsSize, floorsDiffResult);
std::move(floors + 1, floors + floorsSize, floorsDiffResult); //First element does not give the difference
}
and call it this way:
int floorsPressed[30] = {5, 10, 14, 19, 29};
int floorsCounter = 5;
int floorsDiffResult[30];
calculateDiff (floorsPressed, floorsCounter, floorsDiffResult);
for (int i = 0; i < 30; i++)
cout << "Difference: " << floorsDiffResult[i] << endl;
Note that in your code, you are looping on the floorsCounter (size=5) to fill the floorsDiffResult (size=30):
for(int i = 0; i < floorsCounter; i++)
{
floorsDiffResult[i] = *(ptr + i); //Storing the difference into floorsDiffResult array
cout << "Difference: " << *(ptr + i) << endl;
}
Make sure you don't have logical errors.
Upvotes: 0
Reputation: 2226
I don't know if the logic behind what you are trying to do here is correct or not, but there is a major problem here, You are returning pointer to local variable!
This is undefined behavior because it's local and it's lifetime is bounded to scope of your function and after that anything can happen, even the thing you expect (correct result).
So here is what you can do instead:
int* calculateDiff(int floors[], int* output, int floorsSize);
int main()
{
int floorsPressed[30] = {5, 10, 14, 19, 29};
int floorsReturn[30] = {};
int floorsCounter = 5;
int* ptr = calculateDiff(floorsPressed, floorsReturn, floorsCounter);
int floorsDiffResult[30];
for(int i = 0; i < floorsCounter; i++)
{
floorsDiffResult[i] = *(ptr + i); //Storing the difference into floorsDiffResult array
cout << "Difference: " << *(ptr + i) << endl;
}
}
int* calculateDiff(int floors[], int* output, int floorsSize)
{
//int floorsDiffResult[30]; //Create another array to store the difference for other calculations later on such as finding the biggest difference, average of the difference etc.
std::adjacent_difference(floors, floors + floorsSize, output);
std::move(floors + 1, floors + floorsSize, output); //First element does not give the difference
return output;
}
and you don't need to return a pointer from calculateDiff
, floorsReturn
will have your results after function executed, but I didn't want to change your approach that much.
Upvotes: 1