473,405 Members | 2,282 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,405 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 2234
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Doug C via .NET 247 | last post by:
Using C#... I am pulling shared memory in to my app that is in the form of apredefined structure. I have arrays in 2 sub-structures. Onearray is an array of another predefined structure, and the...
15
by: Sourcerer | last post by:
Hi all. Can I do this to duplicate the contents of struct a in struct b struct myStruct a, b, *aptr, *bptr; aptr = a; bptr = b; Do something to initialize contents of structure a...
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...
7
by: Paminu | last post by:
In the following code I am trying to initialize a pointer that is located in a struct. #include <stdlib.h> #include <stdio.h> #define KIDS 4 typedef struct test { void *content;
3
by: Bill Pursell | last post by:
I've found myself wanting to do this: int *x = {1,2,3,4,5}; Obviously, I can't do that. I can certainly non-portably hack it like int *x = (int *)"\x01\x00\x00\x00\x02\x00...", but that's...
12
by: Mik0b0 | last post by:
Hallo. Let's say, there is a structure struct struct10{ int a1; int a2; int a3; int a4; }count={ {10,20,30,40}, {50,60,70,80}
2
by: subramanian100in | last post by:
Suppose I need to assign one structure to another. Should each member of the structure be assigned individually or can memcpy be used ? Since the compiler can add padding bytes, which approach...
14
by: Carramba | last post by:
Hi! I have a big struct and I want to initialize it at once, but I get parse error before "{" compiler error. can't it by done? #include <stdio.h> #include <stdlib.h> typedef struct{ int b;...
38
by: sam_cit | last post by:
Hi, I have the following Struct, Struct Sample { int i; char *p; };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.