473,378 Members | 1,152 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,378 software developers and data experts.

memmove on array of objects

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 how many of
those objects are in use. The one cravat is that the there are no unused
objects (gaps) between the first element of the array to the cursor.

When I remove an object I want to fill the gap by shifting the objects (on
the right) to the left by one. I currently use a for-loop to in which the
objects's operator=() is used.

I would like to replace the for-loop with something like this:

memmove(data+i, data+i+1, (sz-i-1) * sizeof(Object));

Where,
data is the array
i is the index of the object remove
sz is the number of used objects

I believe using memmove should be ok, but I thought it would be best to get
a 2nd opinion.

Thanks,

--
Chris
Jun 27 '08 #1
3 3647
In article <Cra4k.940$sg6.849@edtnps91>,
ch***@thisisnotanemailaddress.ca says...
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?
Yes, if they're PODs. Otherwise, no. From a practical viewpoint, you can
probably get away with it on some non-POD objects as well.

I, however, would have to wonder why you're doing things this way at
all. I'd use something like a vector...

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #2
Jerry Coffin wrote:
In article <Cra4k.940$sg6.849@edtnps91>,
ch***@thisisnotanemailaddress.ca says...
>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?

Yes, if they're PODs. Otherwise, no. From a practical viewpoint, you can
probably get away with it on some non-POD objects as well.

I, however, would have to wonder why you're doing things this way at
all. I'd use something like a vector...
No, the Objects are not PODs.

I thought about using std::vector, but task I am doing is fairly trivial and
so far I haven't needed to bring the STL into the project. I think I will
check out the source for std::vector and see how it removes an element. I
suspect it would be via some kind of for-loop.

In the past I got burnt from using memset() on an array of Objects. After
some debugging I discovered that though the primitive members of the
Objects were being set to 0, so was the vtable! I learned a valuable lesson
there. I just wanted to determine if memmove() might cause similar
problems.

Now that I think of it and read the replies, I can see where memmove() would
cause problems for classes especially with the dtors (clean up of pointers,
etc.) of the moved objects.

Thanks

--
Chris
Jun 27 '08 #3
Chris wrote:
Jerry Coffin wrote:
>In article <Cra4k.940$sg6.849@edtnps91>,
ch***@thisisnotanemailaddress.ca says...
>>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?
Yes, if they're PODs. Otherwise, no. From a practical viewpoint, you can
probably get away with it on some non-POD objects as well.

I, however, would have to wonder why you're doing things this way at
all. I'd use something like a vector...

No, the Objects are not PODs.

I thought about using std::vector, but task I am doing is fairly trivial and
so far I haven't needed to bring the STL into the project. I think I will
check out the source for std::vector and see how it removes an element. I
suspect it would be via some kind of for-loop.

In the past I got burnt from using memset() on an array of Objects. After
some debugging I discovered that though the primitive members of the
Objects were being set to 0, so was the vtable! I learned a valuable lesson
there. I just wanted to determine if memmove() might cause similar
problems.

Now that I think of it and read the replies, I can see where memmove() would
cause problems for classes especially with the dtors (clean up of pointers,
etc.) of the moved objects.

Thanks
Actually, in C++ things such as memmove() and memcpy() (C-style) are not
used, but rather std::copy() and such (C++-style). The main difference,
in an informal way of speaking, is that in C++ you write _what_ you want
to do (implicit), and in C you'd write _how_ you'd want something done
(explicit).

For example, using std::copy() would be safer than hardcoding an
memmove(). Then, std::copy() will figure out for you how to do that best
- possibly with a memmove(), maybe otherwise. Using a vector would be
even better.

As you have yourself indicated, doing things "C-style" is not very safe,
at least not in an otherwise C++-environment. In your position, I would
have used the STL, and enjoyed writing safe code with relatively little
effort. Looking into the vector class' code will only tell you what your
current platform does, other implementations might be different, so it
won't give you much useful information.

The "C++" solution: delegate the work to the STL, and just have faith
that it will work. Trying to outsmart the std library is unlikely to
succeed. :-)

cheers,
wateenellende
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

71
by: ROSY | last post by:
1. How would you use the functions memcpy(), memset(), memmove()?
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"
1
by: kyo guan | last post by:
Hi : python list object like a stl vector, if insert a object in the front or the middle of it, all the object after the insert point need to move backward. look at this code ( in python...
18
by: Kobu | last post by:
Hi, The addresses seens in C's "layer" aren't necessarily the same as the actual physical addresses in computer core (in fact, this is almost always true for big OSes because of memory...
5
by: xdevel | last post by:
Hi, anyone can make me an example where memmove does not cause a memory overlapping and where memcpy do it? Thanks
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> ...
4
by: gsi | last post by:
Hi all, memmove() is guranteed to work correctly if the objects overlap, I am not sure why this can't be done without any extra condition check in code for the memmove() implementation or...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.