Search code examples
c++for-loopvectormemcpypush-back

Vector.push_back Issue


The following question concerns the use of a vector and memcpy. Vector functions being used are, .push_back, .data(), .size().

Information about the msg.

a)

#define BUFFERSIZE 8<<20
char* msg = new char[(BUFFERSIZE / 4)];

Question: Why doesn't code block b) work?

When I run the code below, using vector.push_back in a for loop, it causes the software I'm working with to stop working. I'm not sending the "msg" nor am I reading it, I'm just creating it.

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

By adding +1 to mVertices.size() at the very last row, the software works fine. See the example code below.

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

The code also work, if I remove the for loop.

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

Solution

  • The problem is a basic macro issue: macros define text replacements, not logical or arithmetic expressions.

    #define BUFFERSIZE 8<<20
    

    creates a text macro. When you use it in this expression (I've removed the redundant parentheses):

    char* msg = new char[BUFFERSIZE / 4];
    

    the preprocessor replaces BUFFERSIZE with 8 << 20, so it's as if you had written

    char* msg = new char[8 << 20 / 4];
    

    and the problem is that 8 << 20 / 4 is 256. That's because the expression is evaluated as 8 << (20/4), where presumably you intended it to be (8 << 20) / 4. To fix that (and you should always do this with macros), put parentheses around the expression in the macro itself:

    #define BUFFERSIZE (8<<20)
    

    Incidentally, that's why using a named variable (whether constexpr or otherwise) makes the problem go away: the variable gets the value 8 << 20, not the text, so all is good.