468,512 Members | 1,088 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

questions about resize() and memcpy()

1
I'm using some old code that won't compile any more. I have 2 questions about it, first the code snippet:

Expand|Select|Wrap|Line Numbers
  1. #define TM_VERTEX_STRIDE 4   
  2. vector<float> tm_vertices;
  3.  
  4. appendVertexData(float * vertex/* = NULL*/, float * normal/* = NULL*/, 
  5.          float * color/* = NULL*/, float * texture_coord/* = NULL*/)
  6. {
  7.   unsigned int vi = 0;
  8.   unsigned int ret = 0;
  9.   if(vertex != NULL){
  10.     vi = tm_vertices.size();
  11.     tm_vertices.resize(vi+TM_VERTEX_STRIDE);
  12.      memcpy(tm_vertices.begin() + vi, vertex, 
  13.     sizeof(float)*TM_VERTEX_STRIDE);
  14.    ret = vi / TM_VERTEX_STRIDE;
  15.   }
  16. .....
  17.  
The error : error: no matching function for call to `memcpy(__gnu_cxx::__normal_iterator<float*, std::vector<float, std::allocator<float> > >)'
/usr/include/string.h:40: note: candidates are: void* memcpy(void*, const void*, size_t)

My first question is how can i fix it?
i have tried changing the memcpy() to
Expand|Select|Wrap|Line Numbers
  1.    std::memcpy((float*)(*(tm_vertices.begin() + vi)), vertex, 
  2.                        sizeof(float)*TM_VERTEX_STRIDE);
  3.  
The error being: cannot convert '....' from type `float' to type `float*'

My second question is, what costs would be associated with changing the resize() and memcpy() lines to use a push_back() instead?

Any help is greatly appreciated 8).

cheers,

pj
Oct 12 '06 #1
1 6013
Banfa
9,056 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. #define TM_VERTEX_STRIDE 4   
  2. vector<float> tm_vertices;
  3.  
  4. appendVertexData(float * vertex/* = NULL*/, float * normal/* = NULL*/, 
  5.          float * color/* = NULL*/, float * texture_coord/* = NULL*/)
  6. {
  7.   unsigned int vi = 0;
  8.   unsigned int ret = 0;
  9.   if(vertex != NULL){
  10.     vi = tm_vertices.size();
  11.     tm_vertices.resize(vi+TM_VERTEX_STRIDE);
  12.      memcpy(tm_vertices.begin() + vi, vertex, 
  13.     sizeof(float)*TM_VERTEX_STRIDE);
  14.    ret = vi / TM_VERTEX_STRIDE;
  15.   }
  16. .....
  17.  
This is extremely bad, it is making an assumption about the way a vector works internally, that is that the memory for consecutive entries in the vector is contiguous. While you might hope that this is the case there is nothing in the standard that requires this as far as I am aware. This is a short cut hack to filling the vector to try to increase speed (I guess) but is very very poor form as it is not guaranteed to work.

Expand|Select|Wrap|Line Numbers
  1.    std::memcpy((float*)(*(tm_vertices.begin() + vi)), vertex, 
  2.                        sizeof(float)*TM_VERTEX_STRIDE);
  3.  
Your problem is this

(float*)(*(tm_vertices.begin() + vi))

*(tm_vertices.begin() + vi) has type float (that is how vectors work) and you are trying to cast it to float * which just doesn't work, hence the error

cannot convert '....' from type `float' to type `float*'

My second question is, what costs would be associated with changing the resize() and memcpy() lines to use a push_back() instead?
Well it is almost bound to be a little slower. A slightly better plan is to leave in the resize() because that will speed things up by getting the extra memory required in a single chunk. The memcpy has to be replaced because of what I have said above, I would replace it with a simple loop using the array operator []

LOOP
tm_vertices[vi+LoopCounter] = vertex[LoopCounter];
LOOP END

[] assumes the memory is already available so wont be checking to see if more memory is required making it faster than push_back but necessitating the resize call first.
Oct 12 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Peter Mrosek | last post: by
45 posts views Thread by Pat | last post: by
6 posts views Thread by ziwu | last post: by
14 posts views Thread by MSR | last post: by
21 posts views Thread by Method Man | last post: by
11 posts views Thread by Alexander Adam | last post: by
reply views Thread by Nomen Nescio | last post: by
16 posts views Thread by Borked Pseudo Mailed | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.