Sign in to follow this  

Pointers To Arrays

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok so im usually spot on with pointers but since i dont use pointers to arrays much this is niggling me. I have a class im writing which produces an array of vertices depicting a quad on the screen and stores them as an array as a member of the class, in the class definition/interface. What i want to now do is have some external code take these vertices and memcpy() them to the vertex buffer i have created. I'm aware of the locking unlocking process BTW. I have a member function in the class that returns: &verts[0]; to the calling function. I thought this would be all i need to access the array of verts, it goes to 54 (dont ask why). When i sizeof() what is pointed to by the pointer it only comes the the size of one of the vertex structures and not the size of the entire array. So my question is, if i want to access the whole array from this pointer OUTSIDE of the class, what type does the pointer need to be. Does it have to be of type: VERTSTRUCT* pVerts[]; or something like that? thanks in advance, ace

Share this post


Link to post
Share on other sites
&verts[0] returns the address of the first element of the array.
VERTSTRUCT* pVerts[]; defines an array of pointers to VERTSTRUCTs.

Thus, to get a pointer to the start of the array, either return &verts[0] or simply verts. However, note that you're just returning a POINTER, and NOT an array! Since it's a pointer and can really point to ANY region of memory, the computer doesn't know how many VERTSTRUCT objects come after the pointer location since you can point it anywhere! For objects like strings, they are terminated by a NULL character which indicates their end, but VERTSTRUCTs placed after each other in memory have no special terminator. Thus, since you can really make the pointer to point ANYWHERE in memory, you cannot simply determine the size of an array of objects that a pointer points to. An ARRAY object, however, is a special kind of pointer which you cannot assign an address (except at initialization) -- the advantage to this is that you CAN determine the size of all of the objects after the first object in memory.

If you need clarification, just reply!

Share this post


Link to post
Share on other sites
Quote:
Original post by ace_lovegrove
I thought this would be all i need to access the array of verts, it goes to 54 (dont ask why).

When i sizeof() what is pointed to by the pointer it only comes the the size of one of the vertex structures and not the size of the entire array.

Are you saying 54 is the size of the array? Is that 54 known at compile-time or runtime (actually, are you dynamically allocating the array with new [] or is the array just a datamember of the class). If the array itself is not dynamically allocated then prefer a reference or pointer to the array instead of a pointer to the first element wherever possible. A reference to an array will allow you to use it as though you were directly working with the array including direct access to accurate size data for the entire array (since the size is known with the type-data of the reference). Your memcpy can be done like with any other type.

your_vertex_type ( &your_array_reference )[ your_size ] = your_array;


properly will define and initialize a reference to your array type. If you need a function to return a reference to the array, then the function header would be:

your_vertex_type ( &your_function( your_parameters ) )[ your_size ]


or you could use typedefs:


typedef your_vertex_type your_array_type[ your_size ];

your_array_type& your_function( your_parameters )


when working with the array reference or directly with the array you can use sizeof intuitively and there is no need to store the array size separately.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this