Vector.push_back 问题
Vector.push_back Issue
下面的问题是关于vector和memcpy的使用。使用的矢量函数是,.push_back、.data()、.size().
关于消息的信息。
一)
#define BUFFERSIZE 8<<20
char* msg = new char[(BUFFERSIZE / 4)];
问题:为什么代码块 b) 不起作用?
当我 运行 下面的代码在 for 循环中使用 vector.push_back 时,它会导致我正在使用的软件停止工作。我不是在发送 "msg" 也不是在阅读它,我只是在创建它。
b)
mVertex vertex;
vector<mVertex>mVertices;
for (int i = 0; i < 35; i++)
{
vertex.posX = 2.0;
vertex.posY = 2.0;
vertex.posZ = 2.0;
vertex.norX = 2.0;
vertex.norY = 2.0;
vertex.norZ = 2.0;
vertex.U = 0.5;
vertex.V = 0.5;
mVertices.push_back(vertex);
}
memcpy(msg, // destination
mVertices.data(), // content
(mVertices.size() * sizeof(mVertex))); // size
Screenshot of the error message from the software
通过在最后一行向 mVertices.size() 添加 +1,软件可以正常运行。请参阅下面的示例代码。
c)
mVertex vertex;
vector<mVertex>mVertices;
for (int i = 0; i < 35; i++)
{
vertex.posX = 2.0;
vertex.posY = 2.0;
vertex.posZ = 2.0;
vertex.norX = 2.0;
vertex.norY = 2.0;
vertex.norZ = 2.0;
vertex.U = 0.5;
vertex.V = 0.5;
mVertices.push_back(vertex);
}
memcpy(msg, // destination
mVertices.data(), // content
(mVertices.size()+1 * sizeof(mVertex))); // size
如果我删除 for 循环,代码也可以工作。
d)
mVertex vertex;
vector<mVertex>mVertices;
vertex.posX = 2.0;
vertex.posY = 2.0;
vertex.posZ = 2.0;
vertex.norX = 2.0;
vertex.norY = 2.0;
vertex.norZ = 2.0;
vertex.U = 0.5;
vertex.V = 0.5;
mVertices.push_back(vertex);
memcpy(msg, // destination
mVertices.data(), // content
(mVertices.size() * sizeof(mVertex))); // size
定义并没有按照您的想法进行。
#define BUFFERSIZE 8<<20
产量 BUFFERSIZE / 4 == 8 << 20 / 4 == 8 << 5 == 256
。所以 msg
中分配的内存太小,无法容纳 mVertices
和
memcpy(msg, mVertices.data(), (mVertices.size() * sizeof(mVertex)));
写入错误的内存。这会产生运行时错误。
您应该使用 constexpr
而不是 define
来避免此类问题。
问题是一个基本的宏问题:宏定义 text 替换,而不是逻辑或算术表达式。
#define BUFFERSIZE 8<<20
创建文本宏。当您在此表达式中使用它时(我删除了多余的括号):
char* msg = new char[BUFFERSIZE / 4];
预处理器用8 << 20
替换了BUFFERSIZE
,所以就好像你写了
char* msg = new char[8 << 20 / 4];
问题是 8 << 20 / 4
是 256。那是因为表达式被计算为 8 << (20/4)
,而您可能希望它是 (8 << 20) / 4
。要解决这个问题(你应该总是用宏来做这个),在宏本身的表达式周围加上括号:
#define BUFFERSIZE (8<<20)
顺便说一句,这就是为什么使用命名变量(无论是 constexpr
还是其他)会使问题消失:变量获得 value 8 << 20
,不是文字,所以一切都很好。
下面的问题是关于vector和memcpy的使用。使用的矢量函数是,.push_back、.data()、.size().
关于消息的信息。
一)
#define BUFFERSIZE 8<<20
char* msg = new char[(BUFFERSIZE / 4)];
问题:为什么代码块 b) 不起作用?
当我 运行 下面的代码在 for 循环中使用 vector.push_back 时,它会导致我正在使用的软件停止工作。我不是在发送 "msg" 也不是在阅读它,我只是在创建它。
b)
mVertex vertex;
vector<mVertex>mVertices;
for (int i = 0; i < 35; i++)
{
vertex.posX = 2.0;
vertex.posY = 2.0;
vertex.posZ = 2.0;
vertex.norX = 2.0;
vertex.norY = 2.0;
vertex.norZ = 2.0;
vertex.U = 0.5;
vertex.V = 0.5;
mVertices.push_back(vertex);
}
memcpy(msg, // destination
mVertices.data(), // content
(mVertices.size() * sizeof(mVertex))); // size
Screenshot of the error message from the software
通过在最后一行向 mVertices.size() 添加 +1,软件可以正常运行。请参阅下面的示例代码。
c)
mVertex vertex;
vector<mVertex>mVertices;
for (int i = 0; i < 35; i++)
{
vertex.posX = 2.0;
vertex.posY = 2.0;
vertex.posZ = 2.0;
vertex.norX = 2.0;
vertex.norY = 2.0;
vertex.norZ = 2.0;
vertex.U = 0.5;
vertex.V = 0.5;
mVertices.push_back(vertex);
}
memcpy(msg, // destination
mVertices.data(), // content
(mVertices.size()+1 * sizeof(mVertex))); // size
如果我删除 for 循环,代码也可以工作。
d)
mVertex vertex;
vector<mVertex>mVertices;
vertex.posX = 2.0;
vertex.posY = 2.0;
vertex.posZ = 2.0;
vertex.norX = 2.0;
vertex.norY = 2.0;
vertex.norZ = 2.0;
vertex.U = 0.5;
vertex.V = 0.5;
mVertices.push_back(vertex);
memcpy(msg, // destination
mVertices.data(), // content
(mVertices.size() * sizeof(mVertex))); // size
定义并没有按照您的想法进行。
#define BUFFERSIZE 8<<20
产量 BUFFERSIZE / 4 == 8 << 20 / 4 == 8 << 5 == 256
。所以 msg
中分配的内存太小,无法容纳 mVertices
和
memcpy(msg, mVertices.data(), (mVertices.size() * sizeof(mVertex)));
写入错误的内存。这会产生运行时错误。
您应该使用 constexpr
而不是 define
来避免此类问题。
问题是一个基本的宏问题:宏定义 text 替换,而不是逻辑或算术表达式。
#define BUFFERSIZE 8<<20
创建文本宏。当您在此表达式中使用它时(我删除了多余的括号):
char* msg = new char[BUFFERSIZE / 4];
预处理器用8 << 20
替换了BUFFERSIZE
,所以就好像你写了
char* msg = new char[8 << 20 / 4];
问题是 8 << 20 / 4
是 256。那是因为表达式被计算为 8 << (20/4)
,而您可能希望它是 (8 << 20) / 4
。要解决这个问题(你应该总是用宏来做这个),在宏本身的表达式周围加上括号:
#define BUFFERSIZE (8<<20)
顺便说一句,这就是为什么使用命名变量(无论是 constexpr
还是其他)会使问题消失:变量获得 value 8 << 20
,不是文字,所以一切都很好。