vector<uint8_t> renderArray = *new vector<uint8_t>(); 为什么renderArray.max_size()返回-1
时间: 2024-04-06 10:33:54 浏览: 21
`renderArray.max_size()`方法返回的是`vector<uint8_t>`类型可以包含的最大元素数量,而不是一个整数值。因此,如果`vector<uint8_t>`类型的元素数量达到了其最大容量,那么会抛出`length_error`异常,而不是返回-1。如果您遇到了返回-1的情况,可能是因为您没有正确地使用`max_size()`方法,或者您正在使用一个被修改过的库。
相关问题
std::vector<uint8_t> data 和std::array<uint8_t, 8> 区别
stdvector<uint8_t>和std::array<uint8_t, 8>是C++标准库中数组容器的两种不同类型。它们的区别如下:
1. 大小不同:std::vector是一种动态数组,可以根据需要动态增长或缩小其大小。而std::array是一种固定大小的数组,大小在创建时就确定了,无法改变。
2. 内存管理不同:std::vector使用堆上的内存来存储数组元素,因此它的大小可以在运行时动态改变。而std::array使用栈上的内存来存储数组元素,因此它的大小在编译时就确定了。
3. 功能不同:std::vector提供了很多便捷的操作函数,例如添加、删除和访问元素等。而std::array没有提供这些功能,因为它的大小是固定的,无法添加或删除元素。
4. 访问方式不同:std::vector可以通过下标或迭代器来访问元素,而std::array也支持这种方式,同时还可以通过std::array的成员函数at()来访问元素,这个函数会进行边界检查,确保不会访问越界。
综上所述,std::vector适用于需要动态改变大小的情况,而std::array适用于大小固定的情况。根据具体的需求选择合适的容器类型。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [C++11 std::array 用法小结](https://blog.csdn.net/hellokandy/article/details/125880281)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [std::vector<uint8_t>的数据转处理连接成16进制的string](https://blog.csdn.net/weixin_53567931/article/details/128544448)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
车载QNX系统跑以下代码,为什么打印是-1 std::vector<uint8_t>* renderArray= new std::vector<uint8_t>(); printf("renderArray->max_size(): %d\n",renderArray->max_size());
在QNX中,`std::vector`的`max_size()`函数返回的是该类型对象最多可以包含的元素个数,而不是最大可用内存大小。这个返回值取决于机器的架构,类型的大小以及可用内存的大小等因素。在你的代码中,你没有给`std::vector`指定初始容量,因此其`max_size()`函数返回的是该类型对象最多可以包含的元素个数。由于你在QNX中运行该代码,可能是因为QNX的限制,导致你的`std::vector`对象的最大容量为0,因此当你执行`renderArray->max_size()`时,它返回的是0,而不是你期望的值。因此,当你执行`printf`语句时,它输出的是-1,这是`printf`函数使用%d格式标识符输出0的结果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)