退出代码3(不是我的返回值,正在寻找源代码)

发布于 2024-09-04 16:00:51 字数 3702 浏览 7 评论 0原文

您好,

我的程序退出时代码为 3。没有错误消息,没有异常,并且退出不是由我的代码启动的。

当我尝试从文本文件读取极长的整数值时会出现问题(文本文件存在并正确打开,并且先前读取成功)。

我使用了大量的内存(事实上,我认为这可能是原因,因为我几乎确定我超过了每个进程 2GB 的内存限制)。我还使用 GMP(或者更确切地说,MPIR)库来乘以 bignum。我相当确定这不是文件 I/O 问题,因为我在完全内存中的先前程序版本上得到了相同的错误代码。

系统:
MS Visual Studio 2008
MS Windows Vista 家庭高级版 x86
MPIR 2.1.0 rc2
4GB RAM

此错误代码可能源自何处?

编辑:这是以代码 EDIT2 退出的过程

void condenseBinSplitFile(const char *sourceFilename, int partCount){
//condense results file into final P and Q
std::string tempFilename;
std::string inputFilename(sourceFilename);
std::string outputFilename(BIN_SPLIT_FILENAME_DATA2);
mpz_class *P = new mpz_class(0);
mpz_class *Q = new mpz_class(0);
mpz_class *PP = new mpz_class(0);
mpz_class *QQ = new mpz_class(0);
FILE *sourceFile;
FILE *resultFile;

fpos_t oldPos;
int swapCount = 0;
while (partCount > 1){
    std::cout << partCount << std::endl;
    sourceFile = fopen(inputFilename.c_str(), "r");     
    resultFile = fopen(outputFilename.c_str(), "w");
    for (int i=0; i<partCount/2; i++){
        //Multiplication order:
        //Get Q, skip P
        //Get QQ, mul Q and QQ, print Q, delete Q
        //Jump back to P, get P
        //Mul P and QQ, delete QQ
        //Skip QQ, get PP
        //Mul P and PP, delete P and PP

        //Get Q, skip P
        mpz_inp_str(Q->get_mpz_t(), sourceFile, CALC_BASE);
        fgetpos(sourceFile, &oldPos);
        skipLine(sourceFile);
        skipLine(sourceFile);

        //Get QQ, mul Q and QQ, print Q, delete Q
        mpz_inp_str(QQ->get_mpz_t(), sourceFile, CALC_BASE);      
        (*Q) *= (*QQ);
        mpz_out_str(resultFile, CALC_BASE, Q->get_mpz_t());
        fputc('\n', resultFile);
        (*Q) = 0;

        //Jump back to P, get P
        fsetpos(sourceFile, &oldPos);
        mpz_inp_str(P->get_mpz_t(), sourceFile, CALC_BASE);

        //Mul P and QQ, delete QQ
        (*P) *= (*QQ);
        (*QQ) = 0;

        //Skip QQ, get PP
        skipLine(sourceFile);
        skipLine(sourceFile);
        mpz_inp_str(PP->get_mpz_t(), sourceFile, CALC_BASE); 
        //Mul P and PP, delete PP, print P, delete P             
        (*P) += (*PP);
        (*PP) = 0;
        mpz_out_str(resultFile, CALC_BASE, P->get_mpz_t());
        fputc('\n', resultFile);
        (*P) = 0;
    }
    partCount /= 2;

    fclose(sourceFile);
    fclose(resultFile);

    //swap filenames
    tempFilename = inputFilename;
    inputFilename = outputFilename;
    outputFilename = tempFilename;
    swapCount++;
}

delete P;
delete Q;
delete PP;
delete QQ;

remove(BIN_SPLIT_FILENAME_RESULTS);
if (swapCount%2 == 0)
    rename(sourceFilename, BIN_SPLIT_FILENAME_RESULTS);
else
    rename(BIN_SPLIT_FILENAME_DATA2, BIN_SPLIT_FILENAME_RESULTS);
}

:完全内存版本也以 3 退出

void binarySplitE(const ULONG first, const ULONG last, mpz_class *P, mpz_class *Q){
//P(first, last) = P(first, mid)*Q(mid, last) + P(mid, last)
//Q(first, last) = Q(first, mid)*Q(mid, last)
if (last - first == 1){
    calcP(P, first, last);
    calcQ(Q, first, last);
    return;
}

ULONG mid = (first+last)/2;
mpz_class *PP = new mpz_class(*P);
mpz_class *QQ = new mpz_class(*Q);
//Calculate P(first, mid) and Q(first, mid)
binarySplitE(first, mid, P, Q);
//Calculate P(mid, last) and Q(mid, last)
binarySplitE(mid, last, PP, QQ);

//P(first, last) == P(first, mid)
*P *= (*QQ);
//P(first, last) == P(first, mid)*Q(mid, last)
*P += (*PP);
//P(first, last) == P(first, mid)*Q(mid, last) + P(mid, last);

//Q(first, last) == Q(first, mid)
*Q *= (*QQ);
//Q(first, last) == Q(first, mid)*Q(mid, last)

delete PP;
delete QQ;
}

Greetings,

my program exits with the code 3. No error messages, no exceptions, and the exit is not initiated by my code.

The problem occurs when I am trying to read extremely long integer values from a text file (the text file is present and correctly opened, with successful prior reading).

I am using very large amounts of memory (in fact, I think that this might be the cause, as I am nearly sure I go over the 2GB per process memory limit). I am also using the GMP (or, rather, MPIR) library to multiply bignums. I am fairly sure that this is not a file I/O problem as I got the same error code on a previous program version that was fully in-memory.

System:
MS Visual Studio 2008
MS Windows Vista Home Premium x86
MPIR 2.1.0 rc2
4GB RAM

Where might this error code originate from?

EDIT: this is the procedure that exits with the code

void condenseBinSplitFile(const char *sourceFilename, int partCount){
//condense results file into final P and Q
std::string tempFilename;
std::string inputFilename(sourceFilename);
std::string outputFilename(BIN_SPLIT_FILENAME_DATA2);
mpz_class *P = new mpz_class(0);
mpz_class *Q = new mpz_class(0);
mpz_class *PP = new mpz_class(0);
mpz_class *QQ = new mpz_class(0);
FILE *sourceFile;
FILE *resultFile;

fpos_t oldPos;
int swapCount = 0;
while (partCount > 1){
    std::cout << partCount << std::endl;
    sourceFile = fopen(inputFilename.c_str(), "r");     
    resultFile = fopen(outputFilename.c_str(), "w");
    for (int i=0; i<partCount/2; i++){
        //Multiplication order:
        //Get Q, skip P
        //Get QQ, mul Q and QQ, print Q, delete Q
        //Jump back to P, get P
        //Mul P and QQ, delete QQ
        //Skip QQ, get PP
        //Mul P and PP, delete P and PP

        //Get Q, skip P
        mpz_inp_str(Q->get_mpz_t(), sourceFile, CALC_BASE);
        fgetpos(sourceFile, &oldPos);
        skipLine(sourceFile);
        skipLine(sourceFile);

        //Get QQ, mul Q and QQ, print Q, delete Q
        mpz_inp_str(QQ->get_mpz_t(), sourceFile, CALC_BASE);      
        (*Q) *= (*QQ);
        mpz_out_str(resultFile, CALC_BASE, Q->get_mpz_t());
        fputc('\n', resultFile);
        (*Q) = 0;

        //Jump back to P, get P
        fsetpos(sourceFile, &oldPos);
        mpz_inp_str(P->get_mpz_t(), sourceFile, CALC_BASE);

        //Mul P and QQ, delete QQ
        (*P) *= (*QQ);
        (*QQ) = 0;

        //Skip QQ, get PP
        skipLine(sourceFile);
        skipLine(sourceFile);
        mpz_inp_str(PP->get_mpz_t(), sourceFile, CALC_BASE); 
        //Mul P and PP, delete PP, print P, delete P             
        (*P) += (*PP);
        (*PP) = 0;
        mpz_out_str(resultFile, CALC_BASE, P->get_mpz_t());
        fputc('\n', resultFile);
        (*P) = 0;
    }
    partCount /= 2;

    fclose(sourceFile);
    fclose(resultFile);

    //swap filenames
    tempFilename = inputFilename;
    inputFilename = outputFilename;
    outputFilename = tempFilename;
    swapCount++;
}

delete P;
delete Q;
delete PP;
delete QQ;

remove(BIN_SPLIT_FILENAME_RESULTS);
if (swapCount%2 == 0)
    rename(sourceFilename, BIN_SPLIT_FILENAME_RESULTS);
else
    rename(BIN_SPLIT_FILENAME_DATA2, BIN_SPLIT_FILENAME_RESULTS);
}

EDIT2: completely in-memory version that also exits with 3

void binarySplitE(const ULONG first, const ULONG last, mpz_class *P, mpz_class *Q){
//P(first, last) = P(first, mid)*Q(mid, last) + P(mid, last)
//Q(first, last) = Q(first, mid)*Q(mid, last)
if (last - first == 1){
    calcP(P, first, last);
    calcQ(Q, first, last);
    return;
}

ULONG mid = (first+last)/2;
mpz_class *PP = new mpz_class(*P);
mpz_class *QQ = new mpz_class(*Q);
//Calculate P(first, mid) and Q(first, mid)
binarySplitE(first, mid, P, Q);
//Calculate P(mid, last) and Q(mid, last)
binarySplitE(mid, last, PP, QQ);

//P(first, last) == P(first, mid)
*P *= (*QQ);
//P(first, last) == P(first, mid)*Q(mid, last)
*P += (*PP);
//P(first, last) == P(first, mid)*Q(mid, last) + P(mid, last);

//Q(first, last) == Q(first, mid)
*Q *= (*QQ);
//Q(first, last) == Q(first, mid)*Q(mid, last)

delete PP;
delete QQ;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

十年九夏 2024-09-11 16:00:51

看来此退出代码是由 MPIR(GMP) 返回的,因为它无法分配大量内存。有点烦人的是,这没有出现在任何文档中。

It appears this exit code was returned by MPIR(GMP) because it could not allocate a big amount of memory. It's a bit annoying that this was not in any of the documentation though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文