Search code examples
c++arraysvisual-c++direct3dmemset

How to create an array of D3DFORMAT in C++?


D3DFORMAT *arr = NULL;

This is the reference to the array which is supposed to hold D3DFORMAT typed values. How do I allocate the memory for this array whose size is defined by a variable

unsigned int arrsize;

Should I calculate the size of array in bytes as = sizeof(D3DFORMAT)*arrsize;

And than use memset().?

Plz correct me if I am wrong. Or if tere is a cleaner better C++ method to do this. ?

IN advance. Thanks


Solution

  • "cleaner better C++ method" is to use a std::vector

    unsigned int arrsize = 10;
    std::vector <D3DFORMAT> array (arrsize );
    

    to cast it to void*

    void* p = (void*)(&array[0]);
    

    you will also want to have your values be 0 by default, so just add one more parameter to the constructor

    unsigned int arrsize = 10;
    std::vector <D3DFORMAT> array (arrsize, 0);