将代码从Windows移植到Linux时,删除数组时出错

时间:2021-12-28 15:07:20

I'm trying to port a library, which correctly works in Windows, to Linux.

我正在尝试将一个在Windows中正确工作的库移植到Linux上。

In these lines of code I get an error:

在这些代码行中,我得到了一个错误:

long* permutation = new long[result->getGeneListCount()];
for(long i=0; i<result->getGeneListCount(); i++) 
        permutation[i]=i;
Util::ArrayUtil::DurstenfeldArrayPermutation<long>(permutation, result->getGeneListCount()); 

//result->PerformGenePermutation(permutation);
std::cout << "Just skipped the permutation" << std::endl;

delete[] permutation;

The error seems, to me, to occur during the deletion. I know that, since I have commented the PerformGenePermutation(), I could simply comment also the other lines, but similar problem could appear again in the other code, so I would like to understand the error.

在我看来,错误发生在删除过程中。我知道,因为我已经注释了PerformGenePermutation(),所以我可以简单地注释其他的行,但是类似的问题可能会在其他代码中再次出现,所以我想要理解错误。

The error-output which I get is:

我得到的误差输出是:

*** glibc detected *** /usr/lib/jvm/java-7-oracle/bin/java: munmap_chunk(): invalid pointer: 0x09f287f8 ***

Can anyone help me, please?

有人能帮我吗?

Please, ask me if you need further details.

请问我是否需要进一步的细节。

1 个解决方案

#1


2  

The given code & info is not sufficient to nail down the cause of the problem, but you can do the following:

给定的代码和信息不足以确定问题的原因,但是您可以做以下工作:

replace the code

替换的代码

long* permutation = new long[result->getGeneListCount()];
for(long i=0; i<result->getGeneListCount(); i++) 
        permutation[i]=i;
Util::ArrayUtil::DurstenfeldArrayPermutation<long>(permutation, result->getGeneListCount()); 

//result->PerformGenePermutation(permutation);
std::cout << "Just skipped the permutation" << std::endl;

delete[] permutation;

with

std::vector<long> permutation( result->getGeneListCount() );
for(long i=0; i<long(permutation.size()); i++) 
        permutation[i]=i;
Util::ArrayUtil::DurstenfeldArrayPermutation<long>(&permutation.at( 0 ), permutation.size()); 

//result->PerformGenePermutation(permutation);
std::cout << "Just skipped the permutation" << std::endl;

//delete[] permutation;

Note that the delete is removed since std::vector does that automatically for you.

注意,删除操作被删除,因为std::vector会自动为您删除。

If this now throws an exception from range error from std::vector::at, well then you know that the size is probably zero. Anyway you can now very simply check that in your debugger. And more importantly, if it does not throw an exception, then you know that all's well and good with this code (because std::vector is reliable), so the problem is then elsewhere.

如果现在抛出一个来自std::vector: at的范围错误的异常,那么您就知道大小可能为零。现在你可以在调试器中简单地检查一下。更重要的是,如果它不抛出异常,那么您就知道这个代码很好(因为std::vector是可靠的),所以问题就出在别处了。

Unfortunately this was too long to post as a comment, but it's not really an answer. This is a problem with SO. Since it's designed for pure answers it doesn't support general help.

不幸的是,这篇文章太长了,不能作为评论发表,但它并不是真正的答案。这是SO的问题。因为它是为纯粹的答案而设计的,它不支持一般的帮助。

#1


2  

The given code & info is not sufficient to nail down the cause of the problem, but you can do the following:

给定的代码和信息不足以确定问题的原因,但是您可以做以下工作:

replace the code

替换的代码

long* permutation = new long[result->getGeneListCount()];
for(long i=0; i<result->getGeneListCount(); i++) 
        permutation[i]=i;
Util::ArrayUtil::DurstenfeldArrayPermutation<long>(permutation, result->getGeneListCount()); 

//result->PerformGenePermutation(permutation);
std::cout << "Just skipped the permutation" << std::endl;

delete[] permutation;

with

std::vector<long> permutation( result->getGeneListCount() );
for(long i=0; i<long(permutation.size()); i++) 
        permutation[i]=i;
Util::ArrayUtil::DurstenfeldArrayPermutation<long>(&permutation.at( 0 ), permutation.size()); 

//result->PerformGenePermutation(permutation);
std::cout << "Just skipped the permutation" << std::endl;

//delete[] permutation;

Note that the delete is removed since std::vector does that automatically for you.

注意,删除操作被删除,因为std::vector会自动为您删除。

If this now throws an exception from range error from std::vector::at, well then you know that the size is probably zero. Anyway you can now very simply check that in your debugger. And more importantly, if it does not throw an exception, then you know that all's well and good with this code (because std::vector is reliable), so the problem is then elsewhere.

如果现在抛出一个来自std::vector: at的范围错误的异常,那么您就知道大小可能为零。现在你可以在调试器中简单地检查一下。更重要的是,如果它不抛出异常,那么您就知道这个代码很好(因为std::vector是可靠的),所以问题就出在别处了。

Unfortunately this was too long to post as a comment, but it's not really an answer. This is a problem with SO. Since it's designed for pure answers it doesn't support general help.

不幸的是,这篇文章太长了,不能作为评论发表,但它并不是真正的答案。这是SO的问题。因为它是为纯粹的答案而设计的,它不支持一般的帮助。