英文:
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 <vector>
#include <algorithm>
vector<int> uuid_list_index;
vector<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()));
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<int>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论