473,486 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

questions about resize() and memcpy()

1 New Member
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 8066
Banfa
9,065 Recognized Expert Moderator Expert
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

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

Similar topics

4
3379
by: Peter Mrosek | last post by:
Hello, I have the following declaration in an header file (abbreviated version): typedef struct { unsigned char rgbBlue; unsigned char rgbGreen; unsigned char rgbRed; unsigned char...
45
2315
by: Pat | last post by:
Hi, 1. Any faster method (other than for-loop) to initialize array? 2. Does there have similar C++ function to replace C's "fprintf"? Thanks. Pat
6
4785
by: ziwu | last post by:
std::copy() is a function from C++ library. Is memcpy() a function from C library, or is it re-implemented in C++ library? Why people say "In C++, don't use memcpy for non-POD types? What is...
14
1958
by: MSR | last post by:
I have a couple of questions. 1. Copy Constructor. class A { private: int a1; double d1; char *ptr;
7
2185
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
21
5197
by: Method Man | last post by:
Just a few theoretical questions I had on 'memmove': 1. Is there ever a good reason to use memcpy instead of memmove? 2. Is memmove useful to copy structs (as opposed to = operator)? 3. In...
11
1264
by: Alexander Adam | last post by:
Hi there! I've got a few (more generic) questions for those C++ experts out there. 1. I am trying to store any kind of data (custom PODs, numbers, strings etc.) within some kind of stream...
0
1192
by: Nomen Nescio | last post by:
SQLITE Blob writing error wizard said: Yeah, no problem. We love SQLITE around here. You may need to include the header for unlink(). On a POSIX system, this would be unistd.h.
16
1776
by: Borked Pseudo Mailed | last post by:
Error handling in C Nick Keighley said: ... ... To the OP, please be aware that Nick Keighley is a known troll and is deliberately providing you with unsound advice. You asked for error...
0
7099
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5430
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4864
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.