1

I have a function like the following:

void Direct3DRenderer::CreatePrimitive(char *name, Direct3DHelper::VERTEX vertices[])
{
    LPDIRECT3DVERTEXBUFFER9 vertex_buf;

    d3d_dev->CreateVertexBuffer(4 * sizeof(Direct3DHelper::VERTEX), 0, Direct3DHelper::FVF, D3DPOOL_MANAGED, &vertex_buf, NULL);

    VOID *pVoid;
    vertex_buf->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, vertices, sizeof(vertices));
    vertex_buf->Unlock();

    primitiveMap[name] = vertex_buf;
}

For some reason that sizeof(vertices) is not giving me the right value, or something. This is what I'm inputting:

Direct3DHelper::VERTEX verticesA[] =
{
    { 0, 0, 0, 1, D3DCOLOR_XRGB(255, 0, 0) },
    { 10, 0, 0, 1, D3DCOLOR_XRGB(0, 255, 0) },
    { 0, 10, 0, 1, D3DCOLOR_XRGB(255, 255, 0) },
    { 10, 10, 0, 1, D3DCOLOR_XRGB(0, 0, 255) }
};

If I don't pass it into the function and just replace all the "vertices" with "verticesA," it works... Please help.

  • 1
    Note that [Clang](http://coliru.stacked-crooked.com/a/472ac49e538386e6) and [GCC](http://coliru.stacked-crooked.com/a/489c2b99c1856a4f) both warn about this. – chris Oct 06 '15 at 23:01

1 Answers1

1

The use of the [] syntax does not mean you are passing an array. It's still a pointer, which makes it all the more confusing to use.

So sizeof vertices in your function will be sizeof Direct3DHelper::VERTEX*.

You'll need to pass the size of the array into the function from where it was created. Only in the scope where it was created will the size account for the whole size of the array.

I would actually recommend using something like std::array or std::vector.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125