By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,670 Members | 1,431 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,670 IT Pros & Developers. It's quick & easy.

Initializing many elements in a structure - memcpy?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.