我接管了一些代码,并遇到了一个奇怪的数组重新分配。这是一个 Array 类中的函数(由 JsonValue 使用)
void reserve( uint32_t newCapacity ) {
if ( newCapacity > length + additionalCapacity ) {
newCapacity = std::min( newCapacity, length + std::numeric_limits<decltype( additionalCapacity )>::max() );
JsonValue *newPtr = new JsonValue[newCapacity];
if ( length > 0 ) {
memcpy( newPtr, values, length * sizeof( JsonValue ) );
memset( values, 0, length * sizeof( JsonValue ) );
}
delete[] values;
values = newPtr;
additionalCapacity = uint16_t( newCapacity - length );
}
}
Run Code Online (Sandbox Code Playgroud)
我明白这一点;它只是分配一个新数组,并将旧数组中的内存内容复制到新数组中,然后将旧数组的内容清零。我也知道这样做是为了防止调用析构函数和移动。
这JsonValue是一个带有函数的类,以及一些存储在联合中的数据(字符串、数组、数字等)。
我担心的是这是否实际上是定义的行为。我知道它有效,并且自从我们几个月前开始使用它以来一直没有问题;但如果它未定义,那么并不意味着它会继续工作。 …
我注意到std::aligned_alloc()进入C ++ 17,我喜欢它。但是-当我需要重新分配时会发生什么?我可以手动执行此操作(假设当前分配的地址处的可用空间仅是我要求的空间量),但是标准库中是否不应该提供此功能?
c++ memory-alignment dynamic-allocation c++17 memory-reallocation
假设我遇到了一种情况,我想更改 vulkan 缓冲区 (VkBuffer) 的大小。例如,如果我想向现有顶点缓冲区添加更多顶点。我如何增大/缩小 VkBuffer?我是否会被迫创建一个新的缓冲区并放弃旧的缓冲区,或者是否有类似于 C 的功能realloc?它是否以 vulkan 扩展的形式存在?
此外,我正在使用 Vulkan 内存分配器 (VMA)。如果有这样的功能,我想要使用 VMA 和原始 vulkan 的两种解决方案realloc。
作为家庭作业,我必须编写一个仅使用malloc和free函数更改数组大小的函数。
我知道如何使用它realloc,但是我不知道如何使用它malloc。
typedef struct{
int *tab;
int size;
}arr;
Run Code Online (Sandbox Code Playgroud)
我需要编写以下函数:
void changeSizeDyn(arr *Dyn_arr, int size){
//
}
Run Code Online (Sandbox Code Playgroud)
如作业中所述:它只需要重新分配一次,并且只需要释放一个内存,并且只需要复制旧数组中的元素的一个副本即可。我搜索了很多,但仅使用找到了结果realloc。
不使用它甚至可以做到realloc吗?
假设我们有以下课程:
class Test {
public:
Test() {}
std::vector<int>& getIntList() {
return intList;
}
private:
std::vector<int> intList;
};
Run Code Online (Sandbox Code Playgroud)
同样,我们在main函数中有以下代码来声明类数组:
int main(void) {
Test* test[20];
for (int i = 0; i < 20; ++i) {
test[i] = new Test();
}
}
Run Code Online (Sandbox Code Playgroud)
在这些情况下,将实例化测试对象。
现在,如果我在每个类中的向量中随机添加多个项目,
在调整其内存大小的同时,可能会碰撞每个类中每个向量的内存地址范围。
在这种情况下,是否将整个“测试”对象复制到其他存储区中,并调整矢量的大小?或者,向量STL是仅在类引用向量时才复制到其他存储区并调整大小吗?
完全地,这样编码不是一个好主意吗?
我想重新分配一个具有自引用的名为的类CarJoker。
“重新分配”在这里意味着=更改对象的地址。
要使每个CarJoker生命实例都处于可调整大小的连续数组(例如池)中,此技术是必需的。
我考虑使用std::move,但是它无法CarJoker::wheels按照我希望的方式移动。
(MCVE)
#include <vector>
#include <iostream>
#include <string>
struct Wheel{
void setBlade(){}
void setTwinkle(){}
};
struct CarJoker{
Wheel wa;
Wheel wb;
Wheel wc;
std::vector<Wheel*> wheels;
float hp=5;
CarJoker(){
wheels.push_back(&wa);
wheels.push_back(&wb);
wheels.push_back(&wc);
}
void wow(){
//v want to apply something to every "wheel"
for(auto wheel:wheels){
wheel->setBlade();
}
//v want to apply something to some certain "wheel"
wa.setTwinkle();
}
};
int main(){
CarJoker car1;
CarJoker car2=std::move(car1);
std::cout<<"want to 1 …Run Code Online (Sandbox Code Playgroud) 我开发了如下的阻塞队列类
class Blocking_queue
{
public:
Blocking_queue();
int put(void* elem, size_t elem_size);
int take(void* event);
unsigned int get_size();
private:
typedef struct element
{
void* elem;
size_t elem_size;
struct element* next;
}element_t;
std::mutex m_lock;
std::condition_variable m_condition;
unsigned int m_size;
element_t* m_head;
element_t* m_tail;
};
Run Code Online (Sandbox Code Playgroud)
我希望该类尽可能通用,因此我正在使用一个void指针,该指针在元素添加到队列时分配,在从队列中删除时释放。
int Blocking_queue::take(void* event)
{
element_t* new_head = NULL;
int ret = 0;
// Queue empty
if(nullptr == m_head)
{
// Wait for an element to be added to the queue
std::unique_lock<std::mutex> unique_lock(m_lock);
m_condition.wait(unique_lock);
}
if(nullptr == realloc(event, …Run Code Online (Sandbox Code Playgroud) c++ ×6
c ×1
c++14 ×1
c++17 ×1
class ×1
free ×1
heap-memory ×1
malloc ×1
move ×1
raw-pointer ×1
stack-memory ×1
vector ×1
vulkan ×1