could you help me with little problem?
I have following class;
class Link
{
private:
Demand *demand_[NUMBER_OF_CORES][NUMBER_OF_SLICES];
public:
Link()
{
for (int i = 0; i < NUMBER_OF_CORES; i++)
{
for (int j = 0; j < NUMBER_OF_SLICES; j++)
{
demand_[i][j] = NULL;
}
}
}
int virtualPut();
}
There will be problem with demand_ array. In the constructor everything is fine, after initialization I can use if (demand_[i][j] == NULL).
Problem starts in virtualPut()
int Link::virtualPut()
{
for (int i = 0; i < NUMBER_OF_CORES; i++)
{
for (int j = 0; j < NUMBER_OF_SLICES; j++)
{
std::cout << "We're in " << i << " " << j << " \n" << std::flush;
if (demand_[i][j] == NULL) //SEGMENTATION FAULT
{
std::cout << "EMPTY\n";
}
}
}
}
And also - if I call virtualPut() in constructor (just for test) it works fine.
But outside Link class I use.
void someFunction(Link *tab, int links)
{
tab = new Link[links];
tab[0].virtualPut(); //also just for test
}
What could be a problem here? I know that I can use vector, but that won't help me understand this memory problem.
One more thing - Dr. Memory says:
UNADDRESSABLE ACCESS: reading 0x0000000000000009-0x0000000000000011 8 byte(s)
But why?
EDIT! Problem solved in comments, thank you
demand_pointer? From your code it's uninitializedDemand *demand_[NUMBER_OF_CORES][NUMBER_OF_SLICES];?demand_[x][y]is NULL or not?someFunction,tabis aLink*. Trytab = new Link(); tab.virtualPut();someFunctionis strange. You're passing a pointer toLinkas the first argument, changing it within the function, but the caller tosomeFunctionwill never receive those changes due to the pointer being passed by value. Methinks there are other errors in your code that you're not showing us that is causing the issue. Please post a minimal reproducible example. Also, sinceLinkis a class, it requires that theLinkobject itself be valid for anything to work properly, including thedemand_member. We have no idea ifLinkis valid or not.