简体   繁体   中英

All the members of my array are at the same memory location when they were created using new()

I have created a seven-by-seven array of pointers to "Timeslot" objects in my constructor, using new, like so:

Timeslot ***schedule;

Schedule::Schedule(void)
{
    schedule = new Timeslot**[DAYS]();
    for(int day = 0; day < DAYS; day++){
        schedule[day] = new Timeslot*[TIMESLOTS]();
        for(int time = 0; time < TIMESLOTS; time++){
            schedule[day][time] = new Timeslot();
        }
    }
}

When i edit one Timeslot object, the change is made to all of them. I have tried to google this problem, but all instances i could find were people not using new .

Since i was asked, the change i'm making to the timeslot object that is being propagated to all of them is i'm flagging a bit in a bitmask, using a method of the Timeslot class.

void Timeslot::book(int instructor){
    bitmask = bitmask | instructormasks[instructor];
}

I have, since posting this question, discovered that yes each timeslot object IS getting its own unique memory address, and somehow the bitmask is being flagged in all of them. I'm looking into it now.

You have a buffer overflow here:

for (int day = 0; day <= DAYS; day++)
    schedule[day] = // rest of code

and here

for(int time = 0; time <= TIMESLOTS; time++)
    schedule[day][time] = //rest of code

that could be the cause of your problem.

It seems to me like you are defining a 3D array when you need a 2D one.

Timeslot ***schedule; // 3D array


schedule = new Timeslot**[DAYS]() // 2D array of 1D arrays? Or is it the other way around?

I haven't used C arrays for years so I am not sure what the effect of this would be in your initialization loops, but it's worth looking at.

There's no technical problem with your code. No buffer overflows or etc. And, obviously, operator new should return distinct addresses for different objects unless they were explicitly freed.

Then the problem is most probably in the rest of your code.

  • Are you using a standard heap ( new/delete )?
  • Is bitmask a non-static member of Timeslot class?

Anyway, your code is somewhat over-complicated. It's useful to allocate arrays of pointer-to-pointer-to-pointers in case you're really going to "play" with it, ie in runtime re-allocate pointers, or intentionally make several pointers to point on the same object. It's also useful in case you deal with huge objects, and don't want to demand long contiguous memory blocks.

But you say you have in total 7x7 = 49 objects, which are (supposedly) tiny. Then just use one "static" array:

Timeslot schedule[DAYS][TIMESLOTS];

You have a global variable schedule which is initialized in the Schedule constructor. Maybe you're doing the same thing with bitmask . Make sure that bitmask is a non-static member of Timeslot .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM