0

I'm learning C++ and graphics programming, and following a tutorial, but I can't seem to be able to find any solutions to this problem.

This is the code:

struct CUSTOMVERTEX { FLOAT X, Y, Z; DWORD COLOR; };

void writetovram(struct CUSTOMVERTEX *verticies)
{
    ...
    memcpy(pVoid, verticies, sizeof(verticies));
    ...
    return;
}

void createshapes() 
{
    //simple square
    CUSTOMVERTEX verticies[] =
    {
        { -3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255), },
        { 3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0), },
        { -3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(255, 0, 0), },
        { 3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 255), },

    };
    writetovram(&verticies);
}

I need to be able to pass this struct array in its entirety as a pointer to the function. I've found that if I havn't used pointers then the data doesn't get transferred properly.

I've tried:

void writetovram(struct CUSTOMVERTEX *verticies)
void writetovram(struct CUSTOMVERTEX verticies)
void writetovram(struct CUSTOMVERTEX &verticies)
void writetovram(struct CUSTOMVERTEX *verticies[])
void writetovram(struct CUSTOMVERTEX verticies[])

and

writetovram(&verticies);
writetovram(*verticies);
writetovram(verticies);

Edit: When I put the code like this, it works:

void writetovram()
{
    CUSTOMVERTEX verticies[] =
    {
        { -3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255), },
        { 3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0), },
        { -3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(255, 0, 0), },
        { 3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 255), },

    };
    ...
    memcpy(pVoid, verticies, sizeof(verticies));
    ...
    return;
}

Basically, all the combinations of pointers and syntax I could think of. But none have worked.

Can anyone help?

I've also searched online and found none having this problem.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
J. Doe
  • 171
  • 4
  • 11
  • `writetovram(verticies);` is correct. You should also pass the number of items in the array since it cannot be determined from just the pointer. Particularly `sizeof(verticies)` is incorrect. You might consider using `std::vector` for this. – Retired Ninja Jul 27 '21 at 21:20
  • C stye code is acceptable. However, you have marked this post as C++, so you should consider that a "struct CUSTOMVERTEX;" is, when compiled with a c++, actually a class CUSTOMERVERTEX, (with minor differences in default private - public) Now review std::array, and there are many reasons I prefer std::vector. – 2785528 Jul 27 '21 at 21:27

1 Answers1

1

Inside of writetovram(), sizeof(verticies) doesn't return what you think it does. You can't get the size of an array from just a pointer. There are TONS of questions on StackOverflow on this issue.

Your simplest option is to just pass the array size as another parameter, eg:

struct CUSTOMVERTEX { FLOAT X, Y, Z; DWORD COLOR; };

void writetovram(CUSTOMVERTEX *verticies, int num_verticies)
{
    ...
    memcpy(pVoid, verticies, sizeof(*verticies) * num_verticies);
    ...
}

void createshapes() 
{
    //simple square
    CUSTOMVERTEX verticies[] =
    {
        { -3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255), },
        { 3.0f, 3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0), },
        { -3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(255, 0, 0), },
        { 3.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 255), },

    };

    writetovram(verticies, 4);
    // or:
    // writetovram(verticies, sizeof(verticies)/sizeof(verticies[0]));
    // or:
    // writetovram(verticies, std::size(verticies));
}

Other options include:

  • pass the array by reference, but only if the size is known at compile-time. But if multiple different-sized arrays need to be used, then writetovram() would have to be made into a template function so the array size can be deduced at compile-time for each call site.

  • changing the array to use std::array or std:vector instead, passed to the function by reference, and then it can use their size() methods.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770