# Count vertex array elements

This topic is 1932 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a problem with counting the elements in a vertex array.

I have a vertex array like this

Vertex temp = {
{ x,y,z,norm,u,v,color}
---||----

Here i can use sizeof to get the right number of elements to create my vertexbuffer.
But when i declare my xertex array like this:

Vertex* temp;
Temp = new vertex[count];
Foreach
.....

Then i cant use size of?

Any tips?

##### Share on other sites
 size = count*sizeof(vertex) for(int i=0; i<count<i++) { Temp.x = 1337; }  Edited by Tispe

##### Share on other sites
Thanks, but i need to solve count from an array of vertex. I have the array but cant get the count of it.

##### Share on other sites
Maybe you could use std::vector instead of array.

##### Share on other sites
You need to either manually keep track of the array size or use sonething like std::vector. Is there any reason why you aren't using the latter?

##### Share on other sites
Dont know how i can sent data to my buffer otherwise?
this is the geometry i want to calculate count on.

VERTEX OurVertices[] =
{
{-1.0f, -1.0f, 1.0f, D3DXVECTOR3(0.0f, 0.0f, 1.0f), 0.0f, 0.0f}, // side 1
{1.0f, -1.0f, 1.0f, D3DXVECTOR3(0.0f, 0.0f, 1.0f), 0.0f, 1.0f},
{-1.0f, 1.0f, 1.0f, D3DXVECTOR3(0.0f, 0.0f, 1.0f), 1.0f, 0.0f},
{1.0f, 1.0f, 1.0f, D3DXVECTOR3(0.0f, 0.0f, 1.0f), 1.0f, 1.0f},
{-1.0f, -1.0f, -1.0f, D3DXVECTOR3(0.0f, 0.0f, -1.0f), 0.0f, 0.0f}, // side 2
{-1.0f, 1.0f, -1.0f, D3DXVECTOR3(0.0f, 0.0f, -1.0f), 0.0f, 1.0f},
{1.0f, -1.0f, -1.0f, D3DXVECTOR3(0.0f, 0.0f, -1.0f), 1.0f, 0.0f},
{1.0f, 1.0f, -1.0f, D3DXVECTOR3(0.0f, 0.0f, -1.0f), 1.0f, 1.0f},
{-1.0f, 1.0f, -1.0f, D3DXVECTOR3(0.0f, 1.0f, 0.0f), 0.0f, 0.0f}, // side 3
{-1.0f, 1.0f, 1.0f, D3DXVECTOR3(0.0f, 1.0f, 0.0f), 0.0f, 1.0f},
{1.0f, 1.0f, -1.0f, D3DXVECTOR3(0.0f, 1.0f, 0.0f), 1.0f, 0.0f},
{1.0f, 1.0f, 1.0f, D3DXVECTOR3(0.0f, 1.0f, 0.0f), 1.0f, 1.0f},
{-1.0f, -1.0f, -1.0f, D3DXVECTOR3(0.0f, -1.0f, 0.0f), 0.0f, 0.0f}, // side 4
{1.0f, -1.0f, -1.0f, D3DXVECTOR3(0.0f, -1.0f, 0.0f), 0.0f, 1.0f},
{-1.0f, -1.0f, 1.0f, D3DXVECTOR3(0.0f, -1.0f, 0.0f), 1.0f, 0.0f},
{1.0f, -1.0f, 1.0f, D3DXVECTOR3(0.0f, -1.0f, 0.0f), 1.0f, 1.0f},
{1.0f, -1.0f, -1.0f, D3DXVECTOR3(1.0f, 0.0f, 0.0f), 0.0f, 0.0f}, // side 5
{1.0f, 1.0f, -1.0f, D3DXVECTOR3(1.0f, 0.0f, 0.0f), 0.0f, 1.0f},
{1.0f, -1.0f, 1.0f, D3DXVECTOR3(1.0f, 0.0f, 0.0f), 1.0f, 0.0f},
{1.0f, 1.0f, 1.0f, D3DXVECTOR3(1.0f, 0.0f, 0.0f), 1.0f, 1.0f},
{-1.0f, -1.0f, -1.0f, D3DXVECTOR3(-1.0f, 0.0f, 0.0f), 0.0f, 0.0f}, // side 6
{-1.0f, -1.0f, 1.0f, D3DXVECTOR3(-1.0f, 0.0f, 0.0f), 0.0f, 1.0f},
{-1.0f, 1.0f, -1.0f, D3DXVECTOR3(-1.0f, 0.0f, 0.0f), 1.0f, 0.0f},
{-1.0f, 1.0f, 1.0f, D3DXVECTOR3(-1.0f, 0.0f, 0.0f), 1.0f, 1.0f},
};
DWORD OurIndices[] =
{
0, 1, 2, // side 1
2, 1, 3,
4, 5, 6, // side 2
6, 5, 7,
8, 9, 10, // side 3
10, 9, 11,
12, 13, 14, // side 4
14, 13, 15,
16, 17, 18, // side 5
18, 17, 19,
20, 21, 22, // side 6
22, 21, 23,
};
[source lang="cpp"][/source]

##### Share on other sites
If you're hard coding your geometry like that then you know how many vertices and indices you have, unless I'm missing something?

##### Share on other sites
I think, that this would be a method:

 const ULONG_PTR ulSize = sizeof(OurVertices) / sizeof(VERTEX) 

##### Share on other sites

I think, that this would be a method:

 const ULONG_PTR ulSize = sizeof(OurVertices) / sizeof(VERTEX) 

Consider replacing the calculation with the following function:
 template<typename T, size_t N> size_t number_of_array_elements(T (&)[N]) { return N; } 
It will return the number of elements for an array, and will error out at compile time if you don't pass it an array. Your code, for example, will fail silently and calculate an incorrect size value if you try to use it with a pointer instead of an array.

##### Share on other sites
const ULONG_PTR ulSize = sizeof(OurVertices) / sizeof(VERTEX) = perfect

Thank you!

##### Share on other sites

template
size_t number_of_array_elements(T (&)[N])
{
return N;
}

##### Share on other sites

[quote name='Brother Bob' timestamp='1349622939' post='4987693']
template
size_t number_of_array_elements(T (&)[N])
{
return N;
}

[/quote]
I can interpret your question in two ways:

1. you want to know how to use it, or
2. you want to know how it works and how it is able to determine the size of an array.

For 1, it is just a function where you pass the array and it returns its size.
 int foo[42]; int size = number_of_array_elements(foo); 
The variable size is assigned the value 42, because the array foo contains 42 elements.

For 2, you need to know a bit about references to arrays. Normally when you pass an array to a function, you pass a pointer to its the first value in the array along with the size of the array. This way, the function can take any array, but since the array is decayed into a pointer, the size information about the array is also lost which is why you also have to pass its size to the function. Such a function prototype typically looks like this:
 void myFunction(int *p, int n); 
where p is the pointer and n is the size. The function now knows how many elements that is pointed to by p. You can pass any pointer to any array of any size to this function, but you have to manually track the size of the array.

Another way to pass an array to a function is by reference. The syntax is a little strange if you're not used to it:
 void myFunction(int (&a)[42]); 
The function now takes a reference to an array of 42 elements. This way, you can only pass an array of 42 elements, but the type of the array (which includes its size) is preserved entirely. In the previous function, the type of the array is partially lost: the type of the elements is implied by the pointer's type, but the array's size is lost in the pointer decay.

Now, a template is introduced to allow the type to vary so that you can pass an array of any type. Furthermore, the size of the array can also be made a template parameter. So replace the type (int in this case) and the size (42 in this case) with generic template parameters.
 template<typename T, size_t N> void myFunction(T (&a)[N]); 

Next step is to ask: what do we want the function to do with the array? In this case, we don't want to do anything with the array itself, we just want the function to return the size of the array.
 template<typename T, size_t N> void myFunction(T (&a)[N]) { return N; } 

Last step to match the function I presented above is to remove the name of the parameter. Since we don't use the parameter a anywhere in the function, there is no need to give it a name so we can leave it as an unnamed parameter.
 template<typename T, size_t N> void myFunction(T (&)[N]) { return N; } 
And there it is, the function number_of_array_elements.