If I have this
struct S
{
int arr[5];
};
and I do this:
S s1,s2;
....
s1 = s2;
what happens?
Normally, array assignments aren't possible/allowed. If I do s1.arr =
s2.arr I get a compiler error. If I do s1 = s2 it compiles fine. And it
even seems like the array is getting copied, not just the pointer to the
start of the array, at least with g++.
Is there any difference in semantics here betweeen C and C++?
/David 10 11401
"David Rasmussen" <da************ *@gmx.net> wrote in message
news:40a0fafd$0 $174 struct S { int arr[5]; };
s1 = s2;
what happens?
Normally, array assignments aren't possible/allowed. If I do s1.arr = s2.arr I get a compiler error. If I do s1 = s2 it compiles fine. And it even seems like the array is getting copied, not just the pointer to the start of the array, at least with g++.
Is there any difference in semantics here betweeen C and C++?
Yes, the assignment copies the array contents, in both C and C++. Same
thing for the copy constructor.
David Rasmussen wrote: If I have this
struct S { int arr[5]; };
and I do this:
S s1,s2;
...
s1 = s2;
what happens?
The entire object gets copied. The array is a subobject of that object,
which means that it gets copied too.
Normally, array assignments aren't possible/allowed. If I do s1.arr = s2.arr I get a compiler error.
Array assignment is possible. Arrays are aggregates, just like structs
are. Technically, they can be copied the same way structs are. But it is
not allowed at language level for purely historical reasons.
If I do s1 = s2 it compiles fine. And it even seems like the array is getting copied, not just the pointer to the start of the array, at least with g++.
What pointer? There's no "pointer to the start of the array" in this
example.
Is there any difference in semantics here betweeen C and C++?
No.
--
Best regards,
Andrey Tarasevich
Andrey Tarasevich wrote: Normally, array assignments aren't possible/allowed. If I do s1.arr = s2.arr I get a compiler error.
Array assignment is possible. Arrays are aggregates, just like structs are. Technically, they can be copied the same way structs are. But it is not allowed at language level for purely historical reasons.
Wow. You learn something new everyday... I'm not actually a novice C++
programmer (well, maybe I am after all).
So, just to be clear:
s1.arr = s2.arr isn't possible
but s1 = s2 is?
That's weird!
/David
Siemel Naran wrote: Yes, the assignment copies the array contents, in both C and C++. Same thing for the copy constructor.
It's really weird that the compiler will copy the array implicitly, but
not explicitly. As in s1.arr = s2.arr;
/David
David Rasmussen wrote: Siemel Naran wrote: Yes, the assignment copies the array contents, in both C and C++. Same thing for the copy constructor.
It's really weird that the compiler will copy the array implicitly, but not explicitly. As in s1.arr = s2.arr;
If I remember correctly, the same was true with structures in ancient
times (K&R C). It's just that the rules have been changed for structs
but not for arrays. I guess to much code would have been broken at
the time of change.
--
Karl Heinz Buchegger kb******@gascad .at
"David Rasmussen" <da************ *@gmx.net> wrote in message
news:40******** *************@d text02.news.tel e.dk... Siemel Naran wrote: Yes, the assignment copies the array contents, in both C and C++. Same thing for the copy constructor.
It's really weird that the compiler will copy the array implicitly, but not explicitly. As in s1.arr = s2.arr;
Not really, it copies each member of the array like this
for (int i = 0; i< 5; ++i)
s1.arr[i] = s2.arr[i];
Nothing wierd about it, that's the definition of a default copy
ctor/assignment op, they do a memberwise copy.
john
John Harrison wrote: Not really, it copies each member of the array like this
for (int i = 0; i< 5; ++i) s1.arr[i] = s2.arr[i];
Nothing wierd about it, that's the definition of a default copy ctor/assignment op, they do a memberwise copy.
Sure, but then s1.arr = s2.arr should do the same.
s1 = s2 does a memberwise copy of the structs. That is, each member is
copied. But in the case of the array, this copying is a "magical"
operator that we can't access any other way.
What happens if we copy a struct that has a member that has a private
copy constructor and a private assignment operator?
Like:
class M
{
// private copy constructor and assignment operator
};
struct S
{
M m;
};
S s1,s2;
s1 = s2;
In this case, s1.m = s2.m isn't possible. So I wouldn't expect s1 = s2
to be.
/David
"David Rasmussen" <da************ *@gmx.net> wrote in message
news:40******** *************@d text02.news.tel e.dk... John Harrison wrote: Not really, it copies each member of the array like this
for (int i = 0; i< 5; ++i) s1.arr[i] = s2.arr[i];
Nothing wierd about it, that's the definition of a default copy ctor/assignment op, they do a memberwise copy.
Sure, but then s1.arr = s2.arr should do the same.
It can't, its just history
void f(int a[6])
{
}
int main()
{
int b[6];
f(b);
}
What you are saying is that given this code array b should be copied
wholesale to the array parameter a. That would be logical, but unfortunately
since the dawn of time C (and hence C++) has defined different behaviour for
that code (a is really a pointer, array decays to pointer to the first
element etc etc). Too late to change now.
I think the original decision to treat arrays in this special way was made
because of concerns about the efficiency of copying large arrays.
But structs are different because in the original C
void f(struct S a)
{
}
int main()
{
struct S b;
f(b);
}
was just plain illegal. So when it was decided that actually being able to
copy structs was a useful thing, there wasn't a legacy of C code that
defined different behaviour.
john
David Rasmussen posted: Normally, array assignments aren't possible/allowed. If I do s1.arr = s2.arr I get a compiler error.
That's exactly like writing:
5 = 6;
"Hello" = "Goodbye";
As I'm about to explain:
unsigned int Ages[7];
Now, if I write:
unsigned int k = Ages[0];
Then the value stored in the variable entitled "Ages[0]" gets copied to the
variable entitled "k". No problemo. Now... look at the following:
k = Ages;
"Ages" written on it's own, without an array index, is exactly like writing
"&Ages[0]", ie. it's equal to the address of the first variable in the
array. Now, take your example:
s1.ar = s2.ar;
That is equal to:
&s1.ar[0] = &s2.ar[0];
That's exactly like writing:
unsigned int k = 4;
&k = 27873;
You're not changing the value of a variable at all! You're trying to change
the address of a variable! Self-explanatory I hope!
If I do s1 = s2 it compiles fine.
unsigned int a = 5;
unsigned int b = 6;
a = b;
b = a;
&a = &b; //ERROR
All you are doing is copying memory! As so:
S s1;
S s2;
s1 = s2;
s2 = s1;
&s1 = &s2; //ERROR
Now, let's try solve your problemo:
unsigned int Cats[48];
unsigned int Dogs[48];
To copy the whole lot, you'd have to do:
Cat[0] = Dog[0];
Cat[1] = Dog[1];
Cat[2] = Dog[2];
Cat[3] = Dog[3];
Why do we have to do this? Because there's no variable called "Cat", nor is
there a varible called "Dog". There's a variable called "Cat[0]" alright, as
is there a variable called "Dog[0]". So why is the following valid?:
unsigned int* pCats = Cats;
There's no reason! It makes no sense at all! Someone just decided it was
more convenient than:
unsigned int* pCats = &Cats[0];
I'll get to the point, how should we do it? Well, what you've done is
actually very clever and it's the best thing I've ever seen for doing it!
Take the following:
void RegisterName(co nst char* const pNameToRegister )
{
//Here, we need to keep a record of the name, so we copy it.
//Normally, I would use strcpy, as follows:
char NameBuffer[30];
strcpy(NameBuff er, pNameToRegister );
//But you've come up with a brilliant idea!!
struct StringCopierStr uctureBuffer
{
char NameBuffer[30];
};
StringCopierStr uctureBuffer* pInput = (StringCopierSt ructureBuffer*)
pNameToRegister ;
StringCopierStr uctureBuffer pStored;
pStored = *pInput;
//We don't need strcpy! We can use this with any sort of array!
//I wrote the above code in about 5 mins and there's likely to be a
//thousand errors in it, but you get the picture.
}
-JKop This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Roy Hills |
last post by:
When I'm reading from or writing to a network socket, I want to use a
struct to represent the structured data, but must use an unsigned char
buffer for the call to sendto() or recvfrom().
I have two questions:
1. Is it generally safe to "overlay" the structure on the buffer,
e.g.:
unsigned char buffer;
|
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 there a nicer way of doing this in a single statement?
|
by: Victor Bazarov |
last post by:
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...
|
by: Giox |
last post by:
Hello everybody I want to create a routine that mimic the fread
behavior operating on char array.
I wrote
size_t fread_char(void* buf, size_t sz, size_t n, char* string_source)
{
memcpy(buf, string_source, sz*n);
string_source += n*sz;
|
by: webfan |
last post by:
In the code below, will the assignment z= y do bitwise copying?
struct x
{
int a;
int b;
int c;
};
main()
| |
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.
Here's my flow of commands:
1. copy a single row of a 2D array
2. change that single row (either add a cell or delete a cell).
3. copy that single row back into the 2D array by the use of a temporary array.
So I'm basically copying a single row...
|
by: Mr John FO Evans |
last post by:
Am looking at adding structures to an embedded C emulator but am wondering
what the standard is for copying structures(ie structa=structb) which
contain pointers to memory and which are therefore not contiguous in memory.
When such structures are copied what should C do with these elements? Or
should copying be illegal in this case?
John
|
by: DaTurk |
last post by:
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
|
by: O.B. |
last post by:
When using Marshal to copy data from a byte array to the structure
below, only the first byte of the "other" array is getting copied from
the original byte array. What do I need to specify to get
Marshal.PtrToStructure to copy the all the data into the "other"
array?
unsafe public struct DeadReckoning
{
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |