473,395 Members | 2,437 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,395 software developers and data experts.

Copying array to an array, I really thought I knew what was going on.

This is probably a very silly, and simple question. If I'm coding in
CLI, and I want to copy an array to an array, not a deep copy, just
something of the nature arr1 = arr2, what is going on?

I assumed the address of the first element is getting copied, so i
essentially have two handles to the same memory. And changes in one
would be reflected in the other.

But what if I have something like this

public value struct MyStruct
{
Int32 MyNumber;
};

array<MyStruct>^ arr1 = gcnew array<MyStruct(10);

//We fill the array with 10 MyStruct

array<MyStruct>^ arr2 = arr1;

Now what do we have here? since it's a value struct, did we
essentially do a deep copy?

Sep 10 '07 #1
2 1380
On 2007-09-10 21:11, DaTurk wrote:
This is probably a very silly, and simple question. If I'm coding in
CLI, and I want to copy an array to an array, not a deep copy, just
something of the nature arr1 = arr2, what is going on?
If you are coding in C++/CLI you are asking in the wrong group. This
group is for discussing C++ (and while C++/CLI might have a name and
syntax similar to C++ they are two very different things). I would
suggest you ask your question in either one of the microsoft.public.*
groups, microsoft.public.dotnet.languages.vc is probably the one you
want. Another good place to ask is forums.microsoft.com

--
Erik Wikström
Sep 10 '07 #2
"DaTurk" <mm******@hotmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
This is probably a very silly, and simple question. If I'm coding in
CLI, and I want to copy an array to an array, not a deep copy, just
something of the nature arr1 = arr2, what is going on?

I assumed the address of the first element is getting copied, so i
essentially have two handles to the same memory. And changes in one
would be reflected in the other.

But what if I have something like this

public value struct MyStruct
{
Int32 MyNumber;
};

array<MyStruct>^ arr1 = gcnew array<MyStruct(10);

//We fill the array with 10 MyStruct

array<MyStruct>^ arr2 = arr1;

Now what do we have here? since it's a value struct, did we
essentially do a deep copy?
Ignoring hte fact that this isn't C++ code but something else (C# maybe?)
and presuming ^ is some type of pointer...

If this code was:

public value struct MyStruct
{
Int32 MyNumber;
};

MyStruct* arr1 = new MyStruct[10];

//We fill the array with 10 MyStruct

MyStruct* arr2 = new MyStruct[10];

You would want to iterate over the array copying elements. The manual
method is:

for ( size_t i = 0; i < 10; ++i )
arr2[i] = arr1[i];

This is a very common task, however, so TPTB added it to the stl with
std::copy

std::copy( arr1.begin(), arr1.end(), std::inserter(arr2, arr2.end()) );

The syntax for the 3rd parameter of std::copy here may be wrong, but it
should work.

Other than that, ask in a newsgroup related to your language, whatever it
is.
Sep 11 '07 #3

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

Similar topics

21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
12
by: anonymous | last post by:
Hi folks, I am in a fix trying to copy data to an array which is member of a structure. What I am doing right now is: char array = {0,1,2,3,4,5,6,7}; memcpy(structure.array, array, 8); Is...
6
by: n00dle | last post by:
hi, see if i want to copy the contents of a char arrary into a equally size struct, assuming that the structure has no holes, padding etc., i can use structure = *(t_structure *) array; but...
10
by: Dennis Myrén | last post by:
Hi. I have an array of struct. My question is, if i do: STRUCT a = new STRUCT ; STRUCT s = new STRUCT(); a = s; since STRUCT is a value type, when assigning element 0 of the STRUCT array...
2
by: Dan =o\) | last post by:
Hey guys, I'm getting really annoyed with this array... After selecting some items from a list view, I take the selected text from the first column and store it in an array, I then pass this...
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
7
by: Sam Kong | last post by:
Hello! My question would not be very practical but just out of curiosity. In JavaScript, Array is a subclass of Object. Well maybe not exactly... but sort of... For normal objects, you can...
45
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
3
by: eso40043 | last post by:
Hi guys, I've asked for help here once before with great results, I thought I'd try it again. (I'm on Ubuntu 6.06 with python 2.4. Xemacs as editor.) I have an image as a scipy array and...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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,...

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.