471,309 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,309 software developers and data experts.

Initializing many elements in a structure - memcpy?

I am trying to initialise a structure with random values for all its
fields. It is a large structure so I do not want to do it for each
element in turn (it would take ages).

Please note my structure has the qualification:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=1,
Size=512)] so it is effectively a block of memory of size 512 bytes
although it has integer and string fields.

What I would like to do is something like this:

Random rnd = new Random();
Byte[] temp = new Byte[Marshal.SizeOf(this)];
rnd.NextBytes(temp);

and then copy the array of bytes to my structure, but I do not know
how to do this last bit.

Or maybe there is a way directly to "cast" my structure to an array of
bytes and use the rnd.NextByte function which only works for byte
arrays.

Please help!!

Jul 2 '08 #1
2 2144
Martin Payne <ma****@microcosm.co.ukwrote:
I am trying to initialise a structure with random values for all its
fields. It is a large structure so I do not want to do it for each
element in turn (it would take ages).
Structures shouldn't generally be large, or mutable. Are you sure you
really want a structure instead of a class?

Either way, I'd use reflection to populate it with random values. Bear
in mind that string fields will just be references to strings elsewhere
on the heap - and that making those string references random numbers
would be a very bad thing indeed. Using reflection will let you
populate fields with random values which are appropriate to the
particular field type.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 2 '08 #2
On Jul 2, 4:02*pm, mar...@microcosm.co.uk (Martin Payne) wrote:
I am trying to initialise a structure with random values for all its
fields. It is a large structure so I do not want to do it for each
element in turn (it would take ages).

Please note my structure has the qualification:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=1,
Size=512)] so it is effectively a block of memory of size 512 bytes
although it has integer and string fields.

What I would like to do is something like this:

Random rnd = new Random();
Byte[] temp = new Byte[Marshal.SizeOf(this)];
rnd.NextBytes(temp);

and then copy the array of bytes to my structure, but I do not know
how to do this last bit.

Or maybe there is a way directly to "cast" my structure to an array of
bytes and use the rnd.NextByte function which only works for byte
arrays.
If you do not mind dealing with unsafe code, then you can just cast a
pointer to your struct (on the stack, so it doesn't need to be fixed)
to byte*, and use that. But, what is the purpose of filling string
fields with random bytes?
Jul 2 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Doug C via .NET 247 | last post: by
15 posts views Thread by Sourcerer | last post: by
12 posts views Thread by anonymous | last post: by
7 posts views Thread by Paminu | last post: by
3 posts views Thread by Bill Pursell | last post: by
2 posts views Thread by subramanian100in | last post: by
14 posts views Thread by Carramba | last post: by
38 posts views Thread by sam_cit | last post: by

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.