473,513 Members | 2,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2241
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
8135
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
38062
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
9808
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
2081
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
5862
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
2267
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
13001
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
4259
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
2199
by: sam_cit | last post by:
Hi, I have the following Struct, Struct Sample { int i; char *p; };
0
7379
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
7535
jinu1996
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...
0
7521
tracyyun
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...
1
5084
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4745
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...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1591
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 ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
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...

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.