简体   繁体   English

结构成员在访问时被清除

[英]struct members gets cleared when accessed

First off, I'd like to apologize if my explanation is bad, English isn't my first language.首先,如果我的解释不好,我想道歉,英语不是我的第一语言。 I'll be happy to try and explain better if you didn't understand what I wrote here.如果您不理解我在这里写的内容,我将很乐意尝试更好地解释。

I'm trying to solve this problem .我正在尝试解决这个问题 I have an array of struct (Workshop) which is a member of another struct (Available_Workshops).我有一个结构数组(车间),它是另一个结构(Available_Workshops)的成员。 My issue is that all data members of all instances of the Workshop struct in the array gets cleared after the first iteration of the for loop in the CalculateMaxWorkshops function which consequently leads to a segmentation fault after the first loop.我的问题是,在CalculateMaxWorkshops function 中的 for 循环的第一次迭代后,数组中 Workshop 结构的所有实例的所有数据成员都被清除,从而导致第一次循环后出现分段错误。 I tried using vectors and dynamic arrays, but the the issue still occurs.我尝试使用向量和动态 arrays,但问题仍然存在。

Here's my code.这是我的代码。

#include <iostream>
using namespace std;

struct Workshop
{
    int start = 0;
    int dur = 0;
    int end = 0;
};

struct Available_Workshops
{
    int n = 0;
    Workshop *arr = new Workshop[n];
};

Available_Workshops* initialize(int s[], int d[], int n)
{
    Available_Workshops aw;
    Available_Workshops *u;
    aw.n = n;
    for (int i = 0 ; i < n ; i++)
    {
        Workshop w;
        w.start = s[i];
        w.dur = d[i];
        w.end = w.start + w.end;
        cout << w.end;
        aw.arr[i] = w;
    }
    u = &aw;
    return u;
};

int CalculateMaxWorkshops(Available_Workshops *time_table)
{
    int n = time_table-> n, //number of workshop objects
    int current_class, next_class=0;
    int max_classes = 0;
    for (int i = 0 ; i < n-1 ; i++)
    {
        Workshop cur = time_table -> arr[i];
        current_class = cur.end; //all struct members gets cleared for some reason after this line
    }
}

I can only edit the above, the code below is locked in the site editor.我只能编辑上面的,下面的代码被锁定在站点编辑器中。

int main() {
    int n; // number of workshops
    cin >> n;
    // create arrays of unknown size n
    int* start_time = new int[n];
    int* duration = new int[n];

    for(int i=0; i < n; i++){
        cin >> start_time[i];
    }
    for(int i = 0; i < n; i++){
        cin >> duration[i];
    }

    Available_Workshops * ptr;
    ptr = initialize(start_time,duration, n);
    cout << CalculateMaxWorkshops(ptr) << endl;
    return 0;
}

I tried a different approach in the CalculateMaxWorkshops function and it caused a different issue.我在CalculateMaxWorkshops function 中尝试了不同的方法,但它导致了不同的问题。

int CalculateMaxWorkshops(Available_Workshops *time_table)
{
    int n = time_table -> n, //number of workshop objects
    int current_class, next_class=0;
    int max_classes = 0;
    for (int i = 0 ; i < n-1 ; i++)
    {
        Workshop *cur = &(time_table -> arr[i]);
        current_class = cur -> end;
    }
}

The issue this time is that the array gets "off-setted" backwards.这次的问题是数组向后“偏移”。 In other words, data stored in arr[i] becomes stored in arr[i-1] and the data at arr[ni-1] becomes reinitialized.换句话说,存储在 arr[i] 中的数据被存储在 arr[i-1] 中,并且 arr[ni-1] 中的数据被重新初始化。

In summary, The first issue is that data is turning NULL, while the second issue is that data is being off-setted backwards and becoming reinitialized, for example start = -17891602 .总之,第一个问题是数据正在转向 NULL,而第二个问题是数据正在向后偏移并重新初始化,例如start = -17891602

In this function, you are returning a pointer that stores the address of a local variable:在此 function 中,您将返回一个存储局部变量地址的指针:

Available_Workshops* initialize(int s[], int d[], int n)
{
    Available_Workshops aw;
    Available_Workshops *u;
    // ...
    u = &aw;
    return u;
};

Dereferencing the pointer returned by this function invokes undefined behavior.取消引用此 function 返回的指针会调用未定义的行为。

You need to allocate memory for this pointer:您需要为此指针分配 memory :

Available_Workshops* initialize(int s[], int d[], int n)
{
    Available_Workshops aw;
    Available_Workshops *u;
    // ...
    u = new Available_Workshops{aw};
    return u;
};

and then remember to delete this memory later.然后记得稍后delete这个memory。

In general, I would suggest using std::unique_ptr , or preferably, avoiding pointers entirely.一般来说,我建议使用std::unique_ptr ,或者最好完全避免使用指针。 However, since you cannot change main , that is not an option you have.但是,由于您无法更改main ,因此这不是您的选择。

In addition to the problem cigien pointed out, you have another problem here除了cigien指出的问题,你这里还有一个问题

struct Available_Workshops
{
     int n = 0;
     Workshop *arr = new Workshop[n];
};

arr is a pointer to a zero length array (since n is zero). arr是指向零长度数组的指针(因为n为零)。 But here但在这儿

for (int i = 0 ; i < n ; i++)
{
    ...
    aw.arr[i] = w;
}

you treat it as if it has a length n .您将其视为长度为n

You need to allocate memory for enough workshops.您需要为足够的车间分配 memory。 Simple way to do that would be in a constructor简单的方法是在构造函数中

struct Available_Workshops
{
    Available_Workshops(int num) : n(num), arr(new Workshop[num]) {}
    int n;
    Workshop *arr;
};

which you can then use like this然后你可以像这样使用它

Available_Workshops* initialize(int s[], int d[], int n)
{
    Available_Workshops *u = new Available_Workshops(n);
    for (int i = 0 ; i < n ; i++)
    {
        Workshop w;
        w.start = s[i];
        w.dur = d[i];
        w.end = w.start + w.end;
        cout << w.end;
        u->arr[i] = w;
    }
    return u;
};

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

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