[英]pybind11 - std::vector member of boost::optional struct is emptied if any members of the struct are accessed
[英]struct members gets cleared when accessed
首先,如果我的解釋不好,我想道歉,英語不是我的第一語言。 如果您不理解我在這里寫的內容,我將很樂意嘗試更好地解釋。
我正在嘗試解決這個問題。 我有一個結構數組(車間),它是另一個結構(Available_Workshops)的成員。 我的問題是,在CalculateMaxWorkshops
function 中的 for 循環的第一次迭代后,數組中 Workshop 結構的所有實例的所有數據成員都被清除,從而導致第一次循環后出現分段錯誤。 我嘗試使用向量和動態 arrays,但問題仍然存在。
這是我的代碼。
#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
}
}
我只能編輯上面的,下面的代碼被鎖定在站點編輯器中。
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;
}
我在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;
}
}
這次的問題是數組向后“偏移”。 換句話說,存儲在 arr[i] 中的數據被存儲在 arr[i-1] 中,並且 arr[ni-1] 中的數據被重新初始化。
總之,第一個問題是數據正在轉向 NULL,而第二個問題是數據正在向后偏移並重新初始化,例如start = -17891602
。
在此 function 中,您將返回一個存儲局部變量地址的指針:
Available_Workshops* initialize(int s[], int d[], int n)
{
Available_Workshops aw;
Available_Workshops *u;
// ...
u = &aw;
return u;
};
取消引用此 function 返回的指針會調用未定義的行為。
您需要為此指針分配 memory :
Available_Workshops* initialize(int s[], int d[], int n)
{
Available_Workshops aw;
Available_Workshops *u;
// ...
u = new Available_Workshops{aw};
return u;
};
然后記得稍后delete
這個memory。
一般來說,我建議使用std::unique_ptr
,或者最好完全避免使用指針。 但是,由於您無法更改main
,因此這不是您的選擇。
除了cigien指出的問題,你這里還有一個問題
struct Available_Workshops
{
int n = 0;
Workshop *arr = new Workshop[n];
};
arr
是指向零長度數組的指針(因為n
為零)。 但在這兒
for (int i = 0 ; i < n ; i++)
{
...
aw.arr[i] = w;
}
您將其視為長度為n
。
您需要為足夠的車間分配 memory。 簡單的方法是在構造函數中
struct Available_Workshops
{
Available_Workshops(int num) : n(num), arr(new Workshop[num]) {}
int n;
Workshop *arr;
};
然后你可以像這樣使用它
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.