调用函数时出现分段错误

发布于 2024-08-19 00:32:00 字数 1667 浏览 5 评论 0原文

调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

客…行舟 2024-08-26 00:32:00

我在你的代码中看到两个可疑的地方。

首先,您占用了太多堆栈空间(大约 40 MB)。
其次,数组的索引从 1 开始,它应该是 0:

for (int i=1; i<=noOfNodes; i++)

将其更改为:

for (int i=0; i<noOfNodes; i++)

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:

for (int i=1; i<=noOfNodes; i++)

Change it to:

for (int i=0; i<noOfNodes; i++)
浅笑依然 2024-08-26 00:32:00

据猜测,你有堆栈溢出!您无法可靠地在堆栈上创建巨大的数组。您需要动态或静态创建它们。

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.

习ぎ惯性依靠 2024-08-26 00:32:00

你在哪里定义了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.

怀念你的温柔 2024-08-26 00:32:00

您需要至少 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.

乖乖哒 2024-08-26 00:32:00

您确实应该给我们完整的代码,例如 noOfNodes 没有在任何地方定义。

只是在黑暗中刺探:你是否可能溢出 C 因为你的索引(ij)来自 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 and j) go from 1 to noOfNodes?

乜一 2024-08-26 00:32:00

首先,尼尔说的是真的。

其次,C 和 C++ 数组从索引零开始。如果你声明

int a[100]; // 100 elements, from zeroth to ninety-ninth.

那么它的元素是 a[0], a[1] ... a[99]

First, what Neil said is true.

Second, C and C++ arrays start from index zero. If you declare

int a[100]; // 100 elements, from zeroth to ninety-ninth.

Then its elements are a[0], a[1] ... a[99].

萌酱 2024-08-26 00:32:00

我看不出这段代码有什么问题,但是:如果 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

浅黛梨妆こ 2024-08-26 00:32:00

我也有这个问题,我的函数返回了 std::string 现在我只是引用了一个 dreturn 类型 void 像这样:

void readOnDicoFile(std::ifstream file/*If you have "using namespace std;" instruction, put ifstream, not std::ifstream (you can put both)*/)

和之前:

std::string readOnDicoFile(std::string fileName/*If you have "using namespace std;" instruction, put ifstream, not std::ifstream (you can put both)*/)        

me too i have this problem and my function returned std::string now i just do reference an dreturn type void like that:

void readOnDicoFile(std::ifstream file/*If you have "using namespace std;" instruction, put ifstream, not std::ifstream (you can put both)*/)

and before:

std::string readOnDicoFile(std::string fileName/*If you have "using namespace std;" instruction, put ifstream, not std::ifstream (you can put both)*/)        
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文