在安卓上,矢量排列溢出问题

huangapple go评论72阅读模式
英文:

on android, the vector permutation overflow problem

问题

我使用了next_permutation函数来生成一个向量列表的排列,但在运行程序时出现了堆栈溢出的问题,这是由于向量的大小引起的。

#include <vector>
#include <algorithm>

std::vector<int> uuid_list_index;
std::vector<std::vector<int>> permutation_uuid_lists;
for(size_t i=0; i<12; i++)
{
    uuid_list_index.push_back(i);
}
do permutation_uuid_lists.push_back(uuid_list_index);
while(next_permutation(uuid_list_index.begin(), uuid_list_index.end()));

运行程序时,二进制溢出崩溃。如何实现一个用于列表{0,1,2,3,4,5,6,7,8,9,10,11,12}的排列函数?

英文:

I use next_permutation function to have a vector list permutation, but the stack is overflow when running the program, due to size of the vector

#include &lt;vector&gt;
#include &lt;algorithm&gt;

 vector&lt;int&gt; uuid_list_index;
vector&lt;vector&lt;int&gt;&gt; permutation_uuid_lists;
        for(size_t i=0; i&lt;12; i++)
        {
            uuid_list_index.push_back(i);
        }
        do permutation_uuid_lists.push_back(uuid_list_index);
        while(next_permutation(uuid_list_index.begin(), uuid_list_index.end()));

when run the program, the binary overflow crash, How implement a permutation function for list {0,1,2,3,4,5,6,7,8,9,10,11,12}?

答案1

得分: 3

这一点并不太令人惊讶。uuid_list_index 共有12个不同的条目。

长度为 N 的序列的排列数是 N!;以及
12! = 479001600

permutation_uuid_lists 包含超过 4.79 亿std::vector<int>;因为每个向量至少有12字节的头部(armv8),并包含指向至少12个4字节整数内存元素的指针,另外还意味着在内存分配表中有一个16字节的条目:你试图使用约30 GB 的 RAM。这超过了你的手机拥有的内存。

英文:

This is not too surprising at all. uuid_list_index has 12 different entries.

The number of permutations of a sequence of length N is N!; and
<br>
12! = 479001600.

permutation_uuid_lists contains more than 479 million std::vector&lt;int&gt;s; since each vector has at least a 12 Byte header (armv8) and contains a pointer to at least 12× 4-byte integer memory elements, plus implies a 16-byte entry in a memory allocation table: you're trying to use around 30 GB of RAM. That's more than your phone has.

huangapple
  • 本文由 发表于 2023年1月9日 19:47:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056836.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定