Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > array copy via "struct-trickery"

Reply
Thread Tools

array copy via "struct-trickery"

 
 
anon.asdf@gmail.com
Guest
Posts: n/a
 
      08-28-2007
Hi!

OK, lets try "array-copy":

{
char arrayA[3];
arrayA = (char[]){1, 2, 3};
}
it does *not* work since we're trying to make a fixed array-pointer
arrayA, point to another location/address (where there is an
"anonymous" array holding 1, 2 and 3)



hmmm.... but have a look at this:

/*** trickery-candy ****/
#define LEN 3

struct mystruct {
char arr[LEN];
};

{
char arrayA[LEN];
char arrayB[LEN] = {1, 2, 3};
*((struct mystruct *)arrayA) = (struct mystruct){{1, 2, 3}};
*((struct mystruct *)arrayA) = *((struct mystruct *)arrayB);
}
/***************/

->it works! Using no memcpy (from string.h) or any other "normal
method"!!

Would the code for this be very similar to
memcpy(arrayA, arrayB, sizeof(arrayA));
??? (How does it compare?)

(Any other ways of doing this?)

Regards -Albert

 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      08-28-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> #define LEN 3
>
> struct mystruct {
> char arr[LEN];
> };
>
> {
> char arrayA[LEN];
> char arrayB[LEN] = {1, 2, 3};
> *((struct mystruct *)arrayA) = (struct mystruct){{1, 2, 3}};
> *((struct mystruct *)arrayA) = *((struct mystruct *)arrayB);
> }


I'd recommend not doing that. For one thing, the compiler is
allowed to insert padding at the end of 'struct mystruct', so
that the structure assignment will actually write beyond the end
of the array. For another, even in the absence of trailing
padding, I suspect that the behavior is undefined given C's
aliasing rules.
--
"Your correction is 100% correct and 0% helpful. Well done!"
--Richard Heathfield
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      08-28-2007
(E-Mail Removed) writes:
> OK, lets try "array-copy":

[snip]
>
> hmmm.... but have a look at this:
>
> /*** trickery-candy ****/
> #define LEN 3
>
> struct mystruct {
> char arr[LEN];
> };
>
> {
> char arrayA[LEN];
> char arrayB[LEN] = {1, 2, 3};
> *((struct mystruct *)arrayA) = (struct mystruct){{1, 2, 3}};
> *((struct mystruct *)arrayA) = *((struct mystruct *)arrayB);
> }
> /***************/
>
> ->it works! Using no memcpy (from string.h) or any other "normal
> method"!!


Sure. You can't assign arrays, but you can assign structures, even if
the structures have members that are arrays.

> Would the code for this be very similar to
> memcpy(arrayA, arrayB, sizeof(arrayA));
> ??? (How does it compare?)


There's no reason to assume that the generated code would be
different, since it's doing exactly the same thing. The struct
assignment could well be implemented as a call to memcpy() -- or the
memcpy() call could be implemented as a sequence of MOVE instructions
(or whatever your CPU provides).

I'd just use memcpy() because it's clearer.

> (Any other ways of doing this?)


Undoubtedly, but how many ways do you need?

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      08-29-2007
On Aug 29, 8:33 am, Ben Pfaff <(E-Mail Removed)> wrote:
> (E-Mail Removed) writes:
> > {
> > char arrayA[LEN];
> > char arrayB[LEN] = {1, 2, 3};
> > *((struct mystruct *)arrayA) = (struct mystruct){{1, 2, 3}};
> > *((struct mystruct *)arrayA) = *((struct mystruct *)arrayB);
> > }

>
> I'd recommend not doing that. For one thing, the compiler is
> allowed to insert padding at the end of 'struct mystruct', so
> that the structure assignment will actually write beyond the end
> of the array. For another, even in the absence of trailing
> padding, I suspect that the behavior is undefined given C's
> aliasing rules.


The aliasing rules allow anything to be aliased as char,
I think. However, the behaviour would be undefined if
arrayA is not correctly aligned for a struct mystruct.

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      08-29-2007
Old Wolf <(E-Mail Removed)> writes:

> On Aug 29, 8:33 am, Ben Pfaff <(E-Mail Removed)> wrote:
>> (E-Mail Removed) writes:
>> > {
>> > char arrayA[LEN];
>> > char arrayB[LEN] = {1, 2, 3};
>> > *((struct mystruct *)arrayA) = (struct mystruct){{1, 2, 3}};
>> > *((struct mystruct *)arrayA) = *((struct mystruct *)arrayB);
>> > }

>>
>> I'd recommend not doing that. For one thing, the compiler is
>> allowed to insert padding at the end of 'struct mystruct', so
>> that the structure assignment will actually write beyond the end
>> of the array. For another, even in the absence of trailing
>> padding, I suspect that the behavior is undefined given C's
>> aliasing rules.

>
> The aliasing rules allow anything to be aliased as char,
> I think.


Sure. I am not enough of an expert on C's aliasing rules to know
whether a struct encapsulating an array of char would follow the
same aliasing rules as an array of char. I suspect that it would
not.

> However, the behaviour would be undefined if arrayA is not
> correctly aligned for a struct mystruct.


That's a third reason not to do this. Thank you.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1utchar(a[i&15]);break;}}}
 
Reply With Quote
 
anon.asdf@gmail.com
Guest
Posts: n/a
 
      08-29-2007
On Aug 29, 2:18 am, Old Wolf <(E-Mail Removed)> wrote:
<snip>
> However, the behaviour would be undefined if
> arrayA is not correctly aligned for a struct mystruct.


Could it ever happen that arrayA is not correctly aligned with struct
mystruct, given the following:
#define LEN 3

struct mystruct {
char arr[LEN];
};

char arrayA[LEN];

Might the struct have padding? Would a union change the situation?

By the way - is a struct (or union) aligned according to its size, or
according to the largest component it contains?

Thanks -Albert

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-29-2007
(E-Mail Removed) writes:
> On Aug 29, 2:18 am, Old Wolf <(E-Mail Removed)> wrote:
> <snip>
>> However, the behaviour would be undefined if
>> arrayA is not correctly aligned for a struct mystruct.

>
> Could it ever happen that arrayA is not correctly aligned with struct
> mystruct, given the following:
> #define LEN 3
>
> struct mystruct {
> char arr[LEN];
> };
>
> char arrayA[LEN];


Yes. Note that compilers are likely to allocate objects on stricter
alignment boundaries than they really need to, so code that assumes
arrayA is strictly aligned may happen to work (until it breaks at the
most inconvenient possible moment).

> Might the struct have padding?


Yes.

> Would a union change the situation?


No.

> By the way - is a struct (or union) aligned according to its size, or
> according to the largest component it contains?


The alignment for a struct or union is at least the alignment for its
most strictly aligned member. It may be more strict. For any type,
the size must be a whole multiple of the alignment.

For example char always has one-byte alignment. An implementation
might require, say, 4-byte alignment for all structures. In that
case, this structure:

struct foo {
char c;
};

would have 4-byte alignment; it would therefore have to have at least
3 bytes of padding after 'c'. This is just one possibility; a
compiler is also free to set the size and alignment of 'struct foo' to
1 byte.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      08-31-2007
On Wed, 29 Aug 2007 02:19:02 -0700, (E-Mail Removed) wrote:

>On Aug 29, 2:18 am, Old Wolf <(E-Mail Removed)> wrote:
><snip>
>> However, the behaviour would be undefined if
>> arrayA is not correctly aligned for a struct mystruct.

>
>Could it ever happen that arrayA is not correctly aligned with struct
>mystruct, given the following:
>#define LEN 3
>
>struct mystruct {
> char arr[LEN];
>};
>
>char arrayA[LEN];
>
>Might the struct have padding? Would a union change the situation?


Yes. No, a union may also
have padding.

>
>By the way - is a struct (or union) aligned according to its size, or
>according to the largest component it contains?


A struct is aligned to insure that every member is also properly
aligned.

Since all the members of a union overlap, it is aligned according the
strictest alignment of its members.


Remove del for email
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-31-2007
Barry Schwarz <(E-Mail Removed)> writes:
[...]
> A struct is aligned to insure that every member is also properly
> aligned.


At least.

> Since all the members of a union overlap, it is aligned according the
> strictest alignment of its members.


At least.

A struct or union may be more strictly aligned than any of its
members. For example, an implementation might choose to give all
structs and union 4-byte alignment, even if they contain only
character members.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
copy smaller array into bigger array? Marcus C++ 25 08-19-2009 11:23 AM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM
[help] How to copy fast an array to another array? stelladiary2004@hotmail.com C++ 4 08-13-2004 01:48 AM
How do copy Strings from a single dimensional array to double dimensional array Venkat C++ 4 12-05-2003 09:23 AM



Advertisments