473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

memmove in C++ code

I am working on a C++ application which uses STL vectors heavily. When I profile the code using gprof it shows that 21% of the time is spent during memmoves. The code itself does not have any explicit memove calls.

(portion of flat profile produced using gprof)

% cumulative self self total
time seconds seconds calls s/call s/call name
21.17 52.23 21.21 memmove

(portion of call graph produced using gprof)

<spontaneous>
[8] 20.0 20.17 0.00 memmove [8]


It seems that memmove calls arise due to dynamic memory allocation. But its not clear how to exactly pinpoint which (STL) operations in the code are responsible for this. Any ideas on how to locate the cause of memmoves or which STL operations might cause memmoves.

Thanks,
Himanshu
Dec 29 '06 #1
4 2941
Banfa
9,065 Expert Mod 8TB
The problem is the vectors. A vector keeps a contiguous block of memory (very much like an array), what this means is that if you insert or delete data anywhere except the end of the vector it has to move the following data to make room for the new item (or move all the data to overwrite the old item).

If you are using a vector and making a lot of these type of insertion/deletion then prehaps you should consider a different type of STL container, say a list.
Jan 4 '07 #2
The problem is the vectors. A vector keeps a contiguous block of memory (very much like an array), what this means is that if you insert or delete data anywhere except the end of the vector it has to move the following data to make room for the new item (or move all the data to overwrite the old item).

If you are using a vector and making a lot of these type of insertion/deletion then prehaps you should consider a different type of STL container, say a list.

Hi there,

Actually, I take care to NOT insert or delete elements from anywhere expect the ends of a vector. The operations that I use on vectors are reserve, resize, push_back, range insert at the end, clear, swap.

Can the above functions lead to memmoves?

Thanks,
Himanshu
Jan 8 '07 #3
Banfa
9,065 Expert Mod 8TB
Hi there,

Actually, I take care to NOT insert or delete elements from anywhere expect the ends of a vector. The operations that I use on vectors are reserve, resize, push_back, range insert at the end, clear, swap.

Can the above functions lead to memmoves?

Thanks,
Himanshu
They can, anything that causes a resize of the vectors memory will cause a memmove. If you push_back and the vector needs 1 extra slot to hold the new sized vector then it allocates a block of memory that is the current size + 1 and copies the current contents to the new block of memory. Using resize helps to reduce this by automatically sizing the vectors memory to the right size before doing several push_backs but you will still end up with some memmoves.

vectors inherently have this feature because the have to maintain a contiguous block of memory. If you don't need that specific feature then don't use a vector.
Jan 8 '07 #4
They can, anything that causes a resize of the vectors memory will cause a memmove. If you push_back and the vector needs 1 extra slot to hold the new sized vector then it allocates a block of memory that is the current size + 1 and copies the current contents to the new block of memory. Using resize helps to reduce this by automatically sizing the vectors memory to the right size before doing several push_backs but you will still end up with some memmoves.

vectors inherently have this feature because the have to maintain a contiguous block of memory. If you don't need that specific feature then don't use a vector.
Thanks for answering to my post. Based on your suggestion I looked at stl_vector.h. Most of the routines there call unitialized_copy which in turn calls memmove. I reduced some of the vector related operations (in a critical loop) and that did lead to reduction in the time spent in memmoves.
Jan 9 '07 #5

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

Similar topics

21
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...
12
by: Ian | last post by:
I read the FAQ about the differences between memcpy() and memmove(). Apparently memmove() is supposed to be safer. Just to make sure I understand the concept of memmove(), can someone tell me if...
6
by: novice | last post by:
Please explain with an example whts the DIFFERENCE between "memcpy" and "memmove"
14
by: somenath | last post by:
Hi All, I am trying to understand the behavior of the memcpy and memmove. While doing so I wrote a program as mentioned bellow . #include<stdio.h> #include<stdlib.h> #include<string.h> ...
3
by: Chris | last post by:
Hello all, Not sure if this is off topic, if it is I apologise in advance. Is it safe to use memmove() on an array of objects? I create an array of Objects and I have a cursor to indicate...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...
0
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...

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.