栈堆的emplace和push_c++ - 向量Push_Back VS Emplace_Back - 堆栈内存溢出

我有两种将向量元素添加到向量的方法。

GUI_Vertices.emplace_back();

GUI_Vertices.back().pos.x = ((float)x / 400) - 1.f;

GUI_Vertices.back().pos.y = ((float)y / 300) - 1.f;

GUI_Vertices.back().texCoord.x = u;

GUI_Vertices.back().texCoord.y = v;

GUI_Vertices.back().color.r = m_Color.r / 128;

GUI_Vertices.back().color.g = m_Color.g / 128;

GUI_Vertices.back().color.b = m_Color.b / 128;

GUI_Vertices.back().color.a = m_Color.a / 128;

上面的代码有效,但是我被迫向GUI_Vertices向量添加一个新元素。

Vertex NewVertex;

NewVertex.pos.x = ((float)x / 400) - 1.f;

NewVertex.pos.y = ((float)y / 300) - 1.f;

NewVertex.texCoord.x = u;

NewVertex.texCoord.y = v;

NewVertex.color.r = m_Color.r / 128;

NewVertex.color.g = m_Color.g / 128;

NewVertex.color.b = m_Color.b / 128;

NewVertex.color.a = m_Color.a / 128;

GUI_Vertices.emplace_back(NewVertex);

上面的代码有时可以工作,如果需要,我可以有条件地将NewVertex添加到GUI_Vertices向量中。

这是Vertex的定义:

struct Vertex {

glm::vec3 pos;

glm::vec4 color;

glm::vec2 texCoord;

static VkVertexInputBindingDescription getBindingDescription() {

VkVertexInputBindingDescription bindingDescription = {};

bindingDescription.binding = 0;

bindingDescription.stride = sizeof(Vertex);

bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;

return bindingDescription;

}

static std::array getAttributeDescriptions() {

std::array attributeDescriptions = {};

attributeDescriptions[0].binding = 0;

attributeDescriptions[0].location = 0;

attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;

attributeDescriptions[0].offset = offsetof(Vertex, pos);

attributeDescriptions[1].binding = 0;

attributeDescriptions[1].location = 1;

attributeDescriptions[1].format = VK_FORMAT_R32G32B32A32_SFLOAT;

attributeDescriptions[1].offset = offsetof(Vertex, color);

attributeDescriptions[2].binding = 0;

attributeDescriptions[2].location = 2;

attributeDescriptions[2].format = VK_FORMAT_R32G32_SFLOAT;

attributeDescriptions[2].offset = offsetof(Vertex, texCoord);

return attributeDescriptions;

}

bool operator==(const Vertex& other) const {

return pos == other.pos && color == other.color && texCoord == other.texCoord;

}

};

namespace std {

template<> struct hash {

size_t operator()(Vertex const& vertex) const {

return ((hash<:vec3>()(vertex.pos) ^

(hash<:vec4>()(vertex.color) << 1)) >> 1) ^

(hash<:vec2>()(vertex.texCoord) << 1);

}

};

}

稍后在程序执行中,将所有Vertex元素添加到GUI_Vertex向量后,我对GUI_Vertex执行以下操作:

memcpy(GUI_VertexAllocation->GetMappedData(), GUI_Vertices.data(), sizeof(Vertex) * GUI_Vertices.size());

我正在将内存从GUI_Vertices复制到预分配的缓冲区中,Vulkan将使用该缓冲区来渲染我们的顶点。

现在,我试图弄清楚为什么将Vertex对象添加到GUI_Vertices的第一种方法始终有效,而第二种方法有时仅GUI_Vertices 。

重新编译项目后,第二种方法偶尔会起作用,所以我在这里遇到了一些未定义的行为。 我已经检查了GUI_Vertices的有效性,直到执行memcpy且数据似乎有效为止,因此我不确定发生了什么。

我想让第二种方法起作用,以便可以有条件地将新顶点添加到缓冲区中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值