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

Array copying technique

Below you will find some code I wrote to see if I could wrap array copying
(especially for multi-dimensional arrays) in a simple class and/or
function. It seems to work fine for one-dimensioned arrays as well as
two-dimensioned ones. I am sure three- or more-dimensioned array are just
as OK here.

My concern was that I couldn't use 'std::copy' to copy multi-dimensional
arrays. Perhaps in the future we'll see specialisations of 'std::copy'
function that will work with multidimensional arrays similarly to the
'arrcpy' function. [I intentionally named 'arrcpy' that way to indicate
that the arguments are similar to 'strcpy', first the destination, then
the source.]

I've not managed to make this code accept a constant array (or array of
const T), simply because it would probably require a cast for one of the
placement new operators. But in most cases it doesn't matter. Extend it
if you need to.

I am putting it here because I think you might want to comment on it. If
you think you could use it, use it. If you think you have seen something
like this before, do mention it. I am not claiming any innovation here,
just my laziness to look for something similar that has been already done.

Regards,

Victor

cut here 8<-------------------------------------------------- >8 cut here

#include <memory> // for placement new

template<class T, size_t N> struct AC
{
struct W
{
T arr[N];
};

AC(T (*ar1)[N], T (*ar2)[N]) : w1(new (ar1) W), w2(new (ar2) W) {}
~AC() { *w1 = *w2; }

W *w1, *w2;
};

template<class T, size_t N> void arrcpy(T (&ar1)[N], T (&ar2)[N])
{
AC<T,N> ac(&ar1, &ar2);
}

int main()
{
int ai[1000] = { 1,2,3 };
int bi[1000] = { 5,6,7 };
double a[100][10] = { 1,2,3 };
double b[100][10] = { 5,6,7 };

arrcpy(ai, bi);
arrcpy(a, b);

return 0;
}

cut here 8<-------------------------------------------------- >8 cut here
Dec 12 '05 #1
5 3263
On Mon, 12 Dec 2005 14:28:42 -0500, Victor Bazarov
<v.********@comAcast.net> wrote:
Below you will find some code I wrote to see if I could wrap array copying [...]I am putting it here because I think you might want to comment on it. cut here 8<-------------------------------------------------- >8 cut here

#include <memory> // for placement new

template<class T, size_t N> struct AC
{
struct W
{
T arr[N];
};

AC(T (*ar1)[N], T (*ar2)[N]) : w1(new (ar1) W), w2(new (ar2) W) {} this should probably be:
AC(T (*ar1)[N], T (*ar2)[N]) : w1 (reinterpret_cast<W*>
ar1)),w2(reinterpret_cast<W*> (ar2)) {}
~AC() { *w1 = *w2; }

W *w1, *w2;
};

template<class T, size_t N> void arrcpy(T (&ar1)[N], T (&ar2)[N])
{
AC<T,N> ac(&ar1, &ar2);
}


Otherwise it should work.

Best regards,
Roland Pibinger

Dec 13 '05 #2
Roland Pibinger wrote:
On Mon, 12 Dec 2005 14:28:42 -0500, Victor Bazarov
<v.********@comAcast.net> wrote:

Below you will find some code I wrote to see if I could wrap array copying


[...]
I am putting it here because I think you might want to comment on it.


cut here 8<-------------------------------------------------- >8 cut here

#include <memory> // for placement new

template<class T, size_t N> struct AC
{
struct W
{
T arr[N];
};

AC(T (*ar1)[N], T (*ar2)[N]) : w1(new (ar1) W), w2(new (ar2) W) {}


this should probably be:
AC(T (*ar1)[N], T (*ar2)[N]) : w1 (reinterpret_cast<W*>
ar1)),w2(reinterpret_cast<W*> (ar2)) {}

~AC() { *w1 = *w2; }

W *w1, *w2;
};

template<class T, size_t N> void arrcpy(T (&ar1)[N], T (&ar2)[N])
{
AC<T,N> ac(&ar1, &ar2);
}

Otherwise it should work.


Thanks, Roland. What do you think is the advantage of using the
reinterpret_cast over using placement new?

BTW, I found where it would not work very well (I am sure that some kind
of compile time assertion should help catch those):

if sizeof(W) != sizeof(arr)

IOW, if to create a struct W some padding is required, copying structs
will cause memory overrun, undefined behaviour.

V
Dec 13 '05 #3
On Tue, 13 Dec 2005 17:01:11 -0500, Victor Bazarov
<v.********@comAcast.net> wrote:
Roland Pibinger wrote:
On Mon, 12 Dec 2005 14:28:42 -0500, Victor Bazarov
AC(T (*ar1)[N], T (*ar2)[N]) : w1(new (ar1) W), w2(new (ar2) W) {}
this should probably be:
AC(T (*ar1)[N], T (*ar2)[N]) : w1 (reinterpret_cast<W*>
ar1)),w2(reinterpret_cast<W*> (ar2)) {}


Thanks, Roland. What do you think is the advantage of using the
reinterpret_cast over using placement new?


You actually perform a reinterpret_cast with placement new :-), it's
even less safe than reinterpret_cast. You can cast any type to any
other type in C and C++ (not that I recommend that). Why not use the
'idiomatic' approach?
BTW, I found where it would not work very well (I am sure that some kind
of compile time assertion should help catch those):

if sizeof(W) != sizeof(arr)

IOW, if to create a struct W some padding is required, copying structs
will cause memory overrun, undefined behaviour.


Yes, in theory. Padding can only occur at the end in this case. Are
compilers allowed to padd at the end of a struct?

Best wishes,
Roland Pibinger

Dec 13 '05 #4

"Roland Pibinger" <rp*****@yahoo.com> wrote in message
news:43**************@news.utanet.at...
On Tue, 13 Dec 2005 17:01:11 -0500, Victor Bazarov
<v.********@comAcast.net> wrote:

BTW, I found where it would not work very well (I am sure that some kind
of compile time assertion should help catch those):

if sizeof(W) != sizeof(arr)

IOW, if to create a struct W some padding is required, copying structs
will cause memory overrun, undefined behaviour.


Yes, in theory. Padding can only occur at the end in this case. Are
compilers allowed to padd at the end of a struct?


Yes, padding is allowed after any struct member (but not before the first).

For some implementations this behavior is configurable (e.g. with
a #pragma).

-Mike
Dec 13 '05 #5
Roland Pibinger wrote:
On Tue, 13 Dec 2005 17:01:11 -0500, Victor Bazarov
<v.********@comAcast.net> wrote:

Roland Pibinger wrote:
On Mon, 12 Dec 2005 14:28:42 -0500, Victor Bazarov

AC(T (*ar1)[N], T (*ar2)[N]) : w1(new (ar1) W), w2(new (ar2) W) {}

this should probably be:
AC(T (*ar1)[N], T (*ar2)[N]) : w1 (reinterpret_cast<W*>
ar1)),w2(reinterpret_cast<W*> (ar2)) {}
Thanks, Roland. What do you think is the advantage of using the
reinterpret_cast over using placement new?

You actually perform a reinterpret_cast with placement new :-), it's
even less safe than reinterpret_cast.


Could you please elaborate on safety, since you've mentioned it? Thanks!
You can cast any type to any
other type in C and C++ (not that I recommend that). Why not use the
'idiomatic' approach?


Why would 'reinterpret_cast' be more idiomatic than 'placement new'?
I try to always follow the rule: when constructing an object, even if it
is a POD object, use 'new' unless there is a compelling reason not to.
So, if functionally they are equivalent, I don't see the reason to prefer
the cast.
BTW, I found where it would not work very well (I am sure that some kind
of compile time assertion should help catch those):

if sizeof(W) != sizeof(arr)

IOW, if to create a struct W some padding is required, copying structs
will cause memory overrun, undefined behaviour.

Yes, in theory. Padding can only occur at the end in this case. Are
compilers allowed to padd at the end of a struct?


What's so special about the end? 5.3.3 speaks about any padding needed
for placing objects of that type in an array, so if there are special
alignment requirements for a struct (can there be?), then we may end up
with some additional space between objects (added after the end of 'arr'
member)...

V
Dec 13 '05 #6

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

Similar topics

3
by: rl | last post by:
Hi out there, I'd like to know sth about the costs of a function call in php and the handling of character arrays (init size, enlargement steps of allocated memory, technique on enlargement ->...
2
by: Patrick G. | last post by:
Greetings all: ASP VB, SQL Svr 2000 I am pulling data from 3 tables. table1 holds item details table2 holds publication types and the item id from table1 table3 holds category types and...
1
by: Mark Smith | last post by:
I'm trying to copy data from a 1D array to a 2D array. The obvious thing doesn't work: int twoDee = new int; int oneDee = new int { 1, 2 }; Array.Copy(oneDee, 2, twoDee, 2, 2); This causes a...
5
by: Tales Normando | last post by:
The title says it all. Anyone?
25
by: Rainmaker | last post by:
Hi, Can anyone tell me an efficient algorithm to sort an array of strings? Keep in mind that this array is HUGE and so the algorithm should me efficient enough to deal with it. Thanks
1
by: RonLandreth | last post by:
I am writing an accounting system for a class I'm taking at SLU. I need help figuring out the best way to go about copying a 2D array to a temporary 2D array, for eventually copying it back. ...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
17
by: Ron Peterson (012ED25E) | last post by:
I ran into something like the following while looking into how the GNU MP library implements its mpz_t type. typedef struct { int len; char *buf; } foo; This is an interesting technique. ...
35
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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...
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
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...
0
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...

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.