I'm having real difficulty understanding what is going on with memory allocation two specific scenarios that are producing different result but functionally appear identical to me when applying my naive wisdom. I created a simplified anologous code example below to demonstrate the issue I am encountering. I appreciate that that data is being passed to functions in an overaly complex way but please assume it is necassary unless fundamentally flawed.
In the first section of comment code. This works, no issue. In the second section of the commented code, the terminal flashes for a moment and the drops out with no errors or warnings.
I'd really appreciate an understanding of why this is occuring. Clearly I still have much to learn!
My thanks in advance,
Matminster.
#include <iostream>
#include <list>
struct item{
int item_ID;
std::list<int> related_item_IDs;
};
struct item * return_item_details(std::list<item> list_of_items, int item_ID) {
for(std::list<item>::iterator it = list_of_items.begin(); it != list_of_items.end(); it++) {
if(it->item_ID == item_ID) {
return & * it;
}
}
return NULL;
}
int find_shared_related_items(int item_1_ID,int item_2_ID, std::list<item> list_of_items) {
int total_shared_items=0;
//This works_________________________________________________________________
// struct item * item_1_pointer = return_item_details(list_of_items, item_1_ID);
// struct item * item_2_pointer = return_item_details(list_of_items, item_2_ID);
//___________________________________________________________________________
//But not this?________________________________________________________________
// struct item * item_1_pointer = (struct item * ) malloc(sizeof(struct item * ));
// item_1_pointer = return_item_details(list_of_items, item_1_ID);
// struct item * item_2_pointer = (struct item * ) malloc(sizeof(struct item * ));
// item_2_pointer = return_item_details(list_of_items, item_2_ID);
// //_____________________________________________________________________________
for(std::list<int>::iterator it = item_1_pointer->related_item_IDs.begin(); it != item_1_pointer->related_item_IDs.end(); it++) {
for(std::list<int>::iterator it2 = item_2_pointer->related_item_IDs.begin(); it2 != item_2_pointer->related_item_IDs.end(); it2++) {
if( * it == * it2) {
total_shared_items++;
}
}
}
return total_shared_items;
}
int main() {
item item_1;
item_1.item_ID = 1;
item_1.related_item_IDs.push_back(3);
item_1.related_item_IDs.push_back(4);
item_1.related_item_IDs.push_back(5);
item_1.related_item_IDs.push_back(6);
item item_2;
item_2.item_ID = 2;
item_2.related_item_IDs.push_back(5);
item_2.related_item_IDs.push_back(6);
item_2.related_item_IDs.push_back(7);
item_2.related_item_IDs.push_back(8);
std::list<item> list_of_items;
list_of_items.push_back(item_1);
list_of_items.push_back(item_2);
std::cout << "The total number of shared items between item " << item_1.item_ID << " and item " << item_2.item_ID << " is: " << find_shared_related_items(item_1.item_ID, item_2.item_ID, list_of_items) << "\n";
return 0;
}