简体   繁体   English

我的数组的所有成员在使用 new() 创建时都位于相同的 memory 位置

[英]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:我在我的构造函数中使用 new 创建了一个指向“Timeslot”对象的七乘七指针数组,如下所示:

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.当我编辑一个时隙 object 时,所有时隙都会发生变化。 I have tried to google this problem, but all instances i could find were people not using new .我试图用谷歌搜索这个问题,但我能找到的所有实例都是没有使用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.自从我被问到后,我对传播给所有这些人的时隙 object 所做的更改是我使用时隙 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.自发布此问题以来,我发现是的,每个时隙 object 都获得了自己唯一的 memory 地址,并且以某种方式在所有时隙中都标记了位掩码。 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.在我看来,当你需要一个二维数组时,你正在定义一个 3D 数组。

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.多年来我没有使用 C arrays 所以我不确定这对你的初始化循环有什么影响,但它值得一看。

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.没有缓冲区溢出等。而且,很明显, operator new应该为不同的对象返回不同的地址,除非它们被显式释放。

Then the problem is most probably in the rest of your code.那么问题很可能出在您的代码的 rest 中。

  • Are you using a standard heap ( new/delete )?您使用的是标准堆( new/delete )吗?
  • Is bitmask a non-static member of Timeslot class? bitmaskTimeslot 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.分配 arrays 的指针到指针到指针是很有用的,以防你真的要“玩”它,即在运行时重新分配指针,或者有意地让几个指针指向同一个 object。它是如果您处理巨大的对象并且不想要求很长的连续 memory 块,也很有用。

But you say you have in total 7x7 = 49 objects, which are (supposedly) tiny.但是你说你总共有 7x7 = 49 个对象,这些对象(应该)很小。 Then just use one "static" array:然后只使用一个“静态”数组:

Timeslot schedule[DAYS][TIMESLOTS];

You have a global variable schedule which is initialized in the Schedule constructor.您有一个在Schedule构造函数中初始化的全局变量schedule Maybe you're doing the same thing with bitmask .也许你正在用bitmask做同样的事情。 Make sure that bitmask is a non-static member of Timeslot .确保bitmaskTimeslot的非静态成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类/结构成员总是按照它们被声明的顺序在内存中创建吗? - Do class/struct members always get created in memory in the order they were declared? 检查所有数组成员是否具有相同的值 - Check if all the array members have the same value 在 PyObject_NEW 创建时初始化非平凡的 class 成员 - Initialize nontrivial class members when created by PyObject_NEW C ++初始化程序在相同的内存位置不断创建新对象 - C++ initializer constantly creating new objects at same memory location 是为 n 创建单独的 memory 位置还是 m 或 n 都指向同一位置? - Is separate memory location created for n or both m or n point to the same location? 类成员和成员函数的内存位置 - Class members and member functions memory location 何时使用alloca为类成员释放内存? - When is memory allocated using alloca freed for class members? 何时创建并销毁静态成员? - When static members are created and destroyed? 定义一个C数组,其中每个元素都是相同的内存位置 - Defining a C array where every element is the same memory location 如果派生类不添加新成员,实例是否会占用与基础 class 相同的 memory? - If derived classes do not add new members, will instances take up the same memory as the base class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM