调用函数时出现分段错误
调用 Update_Multiplier 和 gdb 调试器时出现分段错误,显示以下内容:
程序收到信号 SIGSEGV,分段错误。 Update_Multiplier() 中的 0x080b74e8 ()
double upperbound = 116325;
double objective = 1.1707e+07;
int main()
{
Update_Multiplier();
}
void Update_Multiplier()
{
cout << "function 0" << endl;
// Determine subgradient vectors
double gra[1000][1000];
double grb[1000][1000];
double dumX = 0;
double stepsize[1000][1000];
double tuning=2;
double LRADum[1000][1000];
double LRBDum[1000][1000];
cout << "function 1" << endl;
// update subgradient vectors
for (int i=1; i<=noOfNodes; i++)
{
for (int j=1; j<=noOfNodes; j++)
{
if (C[i][j] != 0)
{
dumX=0;
for (int p=1; p<=noOfCommodity; p++)
{
dumX += X[i][j][p];
}
gra[i][j]=dumX-U[i][j]*Y[i][j]-Q[i][j];
grb[i][j]=Q[i][j]-B[i][j]*Y[i][j];
}
}
}
// update stepsize
cout << "function 2" << endl;
for (int i=1; i<=noOfNodes; i++)
{
for (int j=1; j<=noOfNodes; j++)
{
if (C[i][j] != 0)
{
stepsize[i][j]=(tuning*(UpperBound-Objective))/sqrt((gra[i][j]*gra[i][j])*(grb[i][j]*grb[i][j]));
LRADum[i][j]=LRA[i][j]+stepsize[i][j]*gra[i][j];
LRA[i][j]=LRADum[i][j];
LRBDum[i][j]=LRB[i][j]+stepsize[i][j]*grb[i][j];
LRB[i][j]=LRBDum[i][j];
}
}
}
}
Segmentation fault when calling the Update_Multiplier and gdb debugger shows these:
Program received signal SIGSEGV, Segmentation fault.
0x080b74e8 in Update_Multiplier() ()
double upperbound = 116325;
double objective = 1.1707e+07;
int main()
{
Update_Multiplier();
}
void Update_Multiplier()
{
cout << "function 0" << endl;
// Determine subgradient vectors
double gra[1000][1000];
double grb[1000][1000];
double dumX = 0;
double stepsize[1000][1000];
double tuning=2;
double LRADum[1000][1000];
double LRBDum[1000][1000];
cout << "function 1" << endl;
// update subgradient vectors
for (int i=1; i<=noOfNodes; i++)
{
for (int j=1; j<=noOfNodes; j++)
{
if (C[i][j] != 0)
{
dumX=0;
for (int p=1; p<=noOfCommodity; p++)
{
dumX += X[i][j][p];
}
gra[i][j]=dumX-U[i][j]*Y[i][j]-Q[i][j];
grb[i][j]=Q[i][j]-B[i][j]*Y[i][j];
}
}
}
// update stepsize
cout << "function 2" << endl;
for (int i=1; i<=noOfNodes; i++)
{
for (int j=1; j<=noOfNodes; j++)
{
if (C[i][j] != 0)
{
stepsize[i][j]=(tuning*(UpperBound-Objective))/sqrt((gra[i][j]*gra[i][j])*(grb[i][j]*grb[i][j]));
LRADum[i][j]=LRA[i][j]+stepsize[i][j]*gra[i][j];
LRA[i][j]=LRADum[i][j];
LRBDum[i][j]=LRB[i][j]+stepsize[i][j]*grb[i][j];
LRB[i][j]=LRBDum[i][j];
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我在你的代码中看到两个可疑的地方。
首先,您占用了太多堆栈空间(大约 40 MB)。
其次,数组的索引从 1 开始,它应该是 0:
将其更改为:
I see two suspicious things in your code.
First, you are taking too much stack space (about ~40 MB).
Second, you are starting the index of the array at 1, where it should be 0:
Change it to:
据猜测,你有堆栈溢出!您无法可靠地在堆栈上创建巨大的数组。您需要动态或静态创建它们。
At a guess, you have a stack overflow! You cannot reliably create gigantic arrays on the stack. You need to create them dynamically or statically.
你在哪里定义了
noOfNodes
?这个的初始值是多少?或者,您是从控制台读取的吗?如果未初始化,则它可能包含垃圾数据——这可能会也可能不会解释崩溃。Where did you define
noOfNodes
? What is the initial value of this? Or, do you read this in from the console? If this is uninitialized, it probably has junk data -- which may or may not explain the crash.您需要至少 40 MB 的堆栈才能运行此函数,因为您要分配五个数组,每个数组各有 100 万个 8 字节双精度数。
更改函数以使用 new 从堆中分配双精度数组。
You need a stack of at least 40 megabytes to run this function because you're allocating five arrays of one million eight-byte doubles each.
Change the function to allocate the double arrays from the heap using new.
您确实应该给我们完整的代码,例如 noOfNodes 没有在任何地方定义。
只是在黑暗中刺探:你是否可能溢出
C
因为你的索引(i
和j
)来自1
> 到noOfNodes
?You should really give us the whole code, e.g. noOfNodes is not defined anywhere.
Just a stab in the dark: are you possibly overflowing
C
since your indices (i
andj
) go from1
tonoOfNodes
?首先,尼尔说的是真的。
其次,C 和 C++ 数组从索引零开始。如果你声明
那么它的元素是
a[0]
,a[1]
...a[99]
。First, what Neil said is true.
Second, C and C++ arrays start from index zero. If you declare
Then its elements are
a[0]
,a[1]
...a[99]
.我看不出这段代码有什么问题,但是:如果 noOfNodes 为 1000,则可能会出现差一错误。
请记住,数组是 0 索引的,因此您必须访问索引 0 - 999 而不是 1 - 1000正如你正在做的那样
I can not see anything wrong with this code as given, BUT: You might have an off-by-one error if noOfNodes is 1000.
Remember that Arrays are 0-indexed so you have to access indexes 0 - 999 instead of 1 - 1000 as you are doing
我也有这个问题,我的函数返回了 std::string 现在我只是引用了一个 dreturn 类型 void 像这样:
和之前:
me too i have this problem and my function returned std::string now i just do reference an dreturn type void like that:
and before: