473,563 Members | 2,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array size cannot be specified in declaration?

Hi all,

I'm having a hell of a time with declaring a struct to hold some
binary data I'm trying to read from some files on disk. What I would
like to do is something like this:

public struct binHeader
{
public UInt32 Id;
public UInt32 Offset;
public byte[16] MD5Checksum;
}

In addition to the normal numeric stuff, there is a field I need to
read that's a 16-byte checksum. The problem is that when I try to do
this, I get CS0270, Array size cannot be specified in a variable
declaration (try initializing with a 'new' expression).

I'm a bit confused. I need a field that's exactly 16 bytes long
without actually instantiating 16 bytes of memory in the struct
declaration. Surely I don't have to do something silly like:

public byte MD5Checksum01;
public byte MD5Checksum02;
public byte MD5Checksum03;
...

Yes, I know, I could just use a couple of UInt64s, but what if the
field were 1024 bytes long? Would I be stuck declaring UInt64s
instead of bytes as shown above?

What is the best practice for declaring a struct with a byte array
like this?

Thanks for any help and/or advice.

P.S. Whether I use a struct or a class is irrelevant; I get the same
error.

Jul 29 '07 #1
9 7297
Nir
On Jul 29, 7:51 pm, "herob...@gmail .com" <herob...@gmail .comwrote:
Hi all,

I'm having a hell of a time with declaring a struct to hold some
binary data I'm trying to read from some files on disk. What I would
like to do is something like this:

public struct binHeader
{
public UInt32 Id;
public UInt32 Offset;
public byte[16] MD5Checksum;

}

In addition to the normal numeric stuff, there is a field I need to
read that's a 16-byte checksum. The problem is that when I try to do
this, I get CS0270, Array size cannot be specified in a variable
declaration (try initializing with a 'new' expression).

I'm a bit confused. I need a field that's exactly 16 bytes long
without actually instantiating 16 bytes of memory in the struct
declaration. Surely I don't have to do something silly like:

public byte MD5Checksum01;
public byte MD5Checksum02;
public byte MD5Checksum03;
...

Yes, I know, I could just use a couple of UInt64s, but what if the
field were 1024 bytes long? Would I be stuck declaring UInt64s
instead of bytes as shown above?

What is the best practice for declaring a struct with a byte array
like this?

Thanks for any help and/or advice.

P.S. Whether I use a struct or a class is irrelevant; I get the same
error.
Hello,

If I understand you problem correctly, than you may use one the the
following two solutions:
1)

Declare the struct like this:

public struct binHeader
{
public UInt32 Id;
public UInt32 Offset;
public byte[] MD5Checksum;
}

later in you code, you may want to set the size of your array:
binHeader a;
a.MD5Checksum = new byte[16];
2)

Alternatively you can use a class:

public class binHeader
{
public UInt32 Id;
public UInt32 Offset;
public byte[] MD5Checksum = new byte[16];
}
Hope this helps,
Nir Levy

Jul 29 '07 #2
he******@gmail. com wrote:
Hi all,

I'm having a hell of a time with declaring a struct to hold some
binary data I'm trying to read from some files on disk. What I would
like to do is something like this:

public struct binHeader
{
public UInt32 Id;
public UInt32 Offset;
public byte[16] MD5Checksum;
}

In addition to the normal numeric stuff, there is a field I need to
read that's a 16-byte checksum. The problem is that when I try to do
this, I get CS0270, Array size cannot be specified in a variable
declaration (try initializing with a 'new' expression).

I'm a bit confused. I need a field that's exactly 16 bytes long
without actually instantiating 16 bytes of memory in the struct
declaration. Surely I don't have to do something silly like:

public byte MD5Checksum01;
public byte MD5Checksum02;
public byte MD5Checksum03;
...

Yes, I know, I could just use a couple of UInt64s, but what if the
field were 1024 bytes long? Would I be stuck declaring UInt64s
instead of bytes as shown above?

What is the best practice for declaring a struct with a byte array
like this?

Thanks for any help and/or advice.

P.S. Whether I use a struct or a class is irrelevant; I get the same
error.
Try this:

public byte[] MD5Checksum = new MD5Checksum[16];

"byte[]" is a type (an array of bytes).

To load up your struct from the binary file you'd need a read method
that reads the appropriate number of bytes from the file and fills in
the struct appropriately.

--
-glenn-
Jul 29 '07 #3
Nir wrote:
On Jul 29, 7:51 pm, "herob...@gmail .com" <herob...@gmail .comwrote:
>Hi all,

I'm having a hell of a time with declaring a struct to hold some
binary data I'm trying to read from some files on disk. What I would
like to do is something like this:

public struct binHeader
{
public UInt32 Id;
public UInt32 Offset;
public byte[16] MD5Checksum;

}

In addition to the normal numeric stuff, there is a field I need to
read that's a 16-byte checksum. The problem is that when I try to do
this, I get CS0270, Array size cannot be specified in a variable
declaration (try initializing with a 'new' expression).

I'm a bit confused. I need a field that's exactly 16 bytes long
without actually instantiating 16 bytes of memory in the struct
declaration. Surely I don't have to do something silly like:

public byte MD5Checksum01;
public byte MD5Checksum02;
public byte MD5Checksum03;
...

Yes, I know, I could just use a couple of UInt64s, but what if the
field were 1024 bytes long? Would I be stuck declaring UInt64s
instead of bytes as shown above?

What is the best practice for declaring a struct with a byte array
like this?

Thanks for any help and/or advice.

P.S. Whether I use a struct or a class is irrelevant; I get the same
error.

Hello,

If I understand you problem correctly, than you may use one the the
following two solutions:
1)

Declare the struct like this:

public struct binHeader
{
public UInt32 Id;
public UInt32 Offset;
public byte[] MD5Checksum;
}

later in you code, you may want to set the size of your array:
binHeader a;
a.MD5Checksum = new byte[16];
2)

Alternatively you can use a class:

public class binHeader
{
public UInt32 Id;
public UInt32 Offset;
public byte[] MD5Checksum = new byte[16];
}
Hope this helps,
Nir Levy
Brain cramp! Nir's right. My previous reply only works if it is a class
and not a struct. Another way to keep it as a struct is like this:

public struct binHeader
{
public UInt32 Id;
public UInt32 Offset;
public byte[] MD5Checksum;

public binHeader(uint id, uint offset, byte[] checksum)
{
Id = id;
Offset = offset;
MD5Checksum = checksum;
}
}

--
-glenn-
Jul 29 '07 #4
he******@gmail. com <he******@gmail .comwrote:
I'm having a hell of a time with declaring a struct to hold some
binary data I'm trying to read from some files on disk. What I would
like to do is something like this:

public struct binHeader
{
public UInt32 Id;
public UInt32 Offset;
public byte[16] MD5Checksum;
}
If you really need the byte array to be "inline" with the rest of the
struct, you'll have to use unsafe code and the "fixed" modifier. This
is only available in C# 2 (and higher) by the way - hopefully that
won't be an issue for you.

How many instances of this struct will you have though? If it's not a
*huge* amount, I'd stick with the more straightforward practice of
creating the byte array separately and storing a reference in the
struct. Aside from anything else, that way you don't need to deal with
unsafe code, which can be messier to work with *and* is less well
understood in the community. (I certainly don't know much about unsafe
code, as I very, *very* rarely use it.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 29 '07 #5
Maybe I'm asking the wrong question.

As per an earlier post, I am trying to read binary data from a file in
the most efficient manner possible. Ideally, something like this
would work. (Please keep in mind this is pseudo-ish code, meant
mainly to convey the idea.)

public struct binHeader
{
public UInt32 Id;
public UInt32 Offset;
public byte[16] MD5Checksum;
}

Now, to read it from the file, I would do something like this:

FileStream fs = File.OpenRead(" myfile.bin");
BinaryReader reader = new BinaryReader(fs );
binHeader header = new binHeader;
header = reader.ReadByte s(sizeof header);

Or, if I wanted to read a thousand of them, something like this:

FileStream fs = File.OpenRead(" myfile.bin");
BinaryReader reader = new BinaryReader(fs );
binHeader[] header = new binHeader[1000];
header = reader.ReadByte s(sizeof header * 1000);

I know, I know, that's more C-ish than C Sharp-ish, but such is my
background. I'm trying to learn the New Way™; really, I am. So
instead of asking about a specific implementation, I'll just ask for
more along the lines of advice.

I need to read a large number of fixed-size data structures, some of
which contain byte array fields, not just primitive types, quickly and
efficiently from a formatted binary file, some of which are over a
gigabyte. Right now, I've got some classes that read the data
piecemeal (get me a 32-bit integer... now get me another 32-bit
integer... now get me a 16-byte checksum...), and they work, but the
performance is dreadful.

Isn't there some good way of pulling, for example, 2,016 bytes of data
out of a file and ending up with, say, an array of 84 C# data
structures as shown above (24 bytes each), each one addressable as
myObject.Id, myObject.Offset , and myObject.MD5Che cksum, in one
operation? You know, without having to read a record, copy it into a
structure, read another record, copy it into a structure, and so on?
And definitely without having to read a UInt32 and copy it over, read
another UInt32 and copy it over, then read a byte array and copy it
over? That's what I'm doing now, and like I said, the performance is
awful. There's just got to be a better way.

Jul 29 '07 #6
he******@gmail. com wrote:
Maybe I'm asking the wrong question.

As per an earlier post, I am trying to read binary data from a file in
the most efficient manner possible. Ideally, something like this
would work. (Please keep in mind this is pseudo-ish code, meant
mainly to convey the idea.)

public struct binHeader
{
public UInt32 Id;
public UInt32 Offset;
public byte[16] MD5Checksum;
}

Now, to read it from the file, I would do something like this:

FileStream fs = File.OpenRead(" myfile.bin");
BinaryReader reader = new BinaryReader(fs );
binHeader header = new binHeader;
header = reader.ReadByte s(sizeof header);

Or, if I wanted to read a thousand of them, something like this:

FileStream fs = File.OpenRead(" myfile.bin");
BinaryReader reader = new BinaryReader(fs );
binHeader[] header = new binHeader[1000];
header = reader.ReadByte s(sizeof header * 1000);

I know, I know, that's more C-ish than C Sharp-ish, but such is my
background. I'm trying to learn the New Way™; really, I am. So
instead of asking about a specific implementation, I'll just ask for
more along the lines of advice.

I need to read a large number of fixed-size data structures, some of
which contain byte array fields, not just primitive types, quickly and
efficiently from a formatted binary file, some of which are over a
gigabyte. Right now, I've got some classes that read the data
piecemeal (get me a 32-bit integer... now get me another 32-bit
integer... now get me a 16-byte checksum...), and they work, but the
performance is dreadful.

Isn't there some good way of pulling, for example, 2,016 bytes of data
out of a file and ending up with, say, an array of 84 C# data
structures as shown above (24 bytes each), each one addressable as
myObject.Id, myObject.Offset , and myObject.MD5Che cksum, in one
operation? You know, without having to read a record, copy it into a
structure, read another record, copy it into a structure, and so on?
And definitely without having to read a UInt32 and copy it over, read
another UInt32 and copy it over, then read a byte array and copy it
over? That's what I'm doing now, and like I said, the performance is
awful. There's just got to be a better way.
In that case, you have to fill in your struct with the appropriate
methods from the binary reader. Like:

myObject.MD5Che cksum = new byte[16];
reader.ReadByte s(myObject.MD5C hecksum, 16);

(Or something like that, I'm writing this from memory.)

In other words, the binary reader will let you get at the information in
the file in binary format--you have to map those binary numbers to the
appropriate high-level structure or whatever. It sounds like you are
actually doing this already which is the way it is done.

--
-glenn-
Jul 29 '07 #7
he******@gmail. com wrote:
gigabyte. Right now, I've got some classes that read the data
piecemeal (get me a 32-bit integer... now get me another 32-bit
integer... now get me a 16-byte checksum...), and they work, but the
performance is dreadful.
What makes you think the performance is dreadful? Do you have numbers
showing how long it takes? And if so, what are you comparing those
numbers to?
--
-glenn-
Jul 29 '07 #8
he******@gmail. com <he******@gmail .comwrote:
Maybe I'm asking the wrong question.

As per an earlier post, I am trying to read binary data from a file in
the most efficient manner possible.
It would be nice to get a bit more information on this before we go too
much further.

1) What kind of efficiency are you after? Memory or speed?
2) How many of these will you be reading in real life?
3) How many do you want to have in memory at any one time?

*Often* performance comes at the cost of elegance or readability. It's
always worth having a clear idea of just how tight you need something
to be before you start.

I'm sure that with the use of unsafe code and P/Invoke we can get it
all fiendishly fast - at the cost of readability, flexibility,
maintainability etc. Alternatively, we can chip away at things to keep
as much of the "goodness" as possible until we hit the target
performance, only optimising the points which are really significant.

If I/O is the problem, then using BinaryReader's FillBuffer method may
well make a huge difference - fill the buffer with as much data as you
need, then convert it appropriately. That means temporarily having two
copies of the data in memory of course, but you can strike a balance
between the amount to buffer and the speed involved.

You might also look at Buffer.BlockCop y, but as I said before you'd
need to use unsafe code to get the fixed sized buffer "inline".

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 29 '07 #9
Maybe I'm asking the wrong question.
>
As per an earlier post, I am trying to read binary data from a file in
the most efficient manner possible. Ideally, something like this
would work. (Please keep in mind this is pseudo-ish code, meant
mainly to convey the idea.)

Now, to read it from the file, I would do something like this:

FileStream fs = File.OpenRead(" myfile.bin");
BinaryReader reader = new BinaryReader(fs );
binHeader header = new binHeader;
header = reader.ReadByte s(sizeof header);
<snip>

Try something like this, perhaps:

***
public struct binHeader {
public UInt32 Id;
public UInt32 Offset;
public byte[] MD5Checksum;

// Checksum size
public const int MD5ChecksumSize = 16;

// Initialise struct from stream
public binHeader(Binar yReader inStream) {
if (inStream != null) {
Id = inStream.ReadUI nt32();
Offset = inStream.ReadUI nt32();
MD5Checksum = inStream.ReadBy tes(MD5Checksum Size);
} else { // Populate with default data
Id = 0;
Offset = 0;
MD5Checksum = new byte[MD5ChecksumSize];
}
}

// Read single binHeader structure from stream
public static binHeader FromStream(Bina ryReader inStream) {
return new binHeader(inStr eam);
}

// Read multiple binHeader structures from stream
public static binHeader[] FromStream(Bina ryReader inStream, int inCount) {
if ((inCount 0) && (inStream != null)) {
binHeader[] RetArr = new binHeader[inCount];

for (int i = 0; i < inCount; i++)
RetArr[i] = new binHeader(inStr eam);

return RetArr;
} else
return null;
}
}
***

Then you can use it like:

***
FileStream fs = File.OpenRead(" myfile.bin");
BinaryReader reader = new BinaryReader(fs );
binHeader header = new binHeader(reade r);
***

Or to read multiple structures:

***
FileStream fs = File.OpenRead(" myfile.bin");
BinaryReader reader = new BinaryReader(fs );
binHeader[] headers = binHeader.FromS tream(reader, 1000);
***

Hope this helps,

Mike
- Microsoft Visual Basic MVP -
E-Mail: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Jul 29 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
3698
by: prashna | last post by:
Hi Guru's, Here are my questions... 1)Why does c allows an extra "," in array intialiser?Is there any advantage of this? ex: int arr={1,2,3,4,5,}; ^^Compiler does not give error for this! 2)How to determine the size of the array which is passes as a
16
41751
by: herbertF | last post by:
Hi guys, In a program (not my own) I encountered the declaration of a constant pointer to an array consisting of two other const pointers to arrays. Not quite sure why they do it so complicated, but is it legal? Most compilers accept it, but one doesn't recognize the rhs as a constant. What are the requirements for the rhs in the...
4
2646
by: Gopi Sundaram | last post by:
I have the following code: const int num_segments = 16; int some_function(void) { int key; ... }
11
4434
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
11
2249
by: Geoff Cox | last post by:
Hello, I am trying to get a grip on where to place the initialization of two arrays in the code below which was created using Visual C++ 2005 Express Beta 2... private: static array<String^>^ LHSquestions = gcnew array<String^> {"question 1","question 2"}; private: static array<String^>^ RHSquestions = gcnew array<String^> {"question 1",
9
9390
by: joshc | last post by:
Hi, I have an array defined in one file with an intializer as follows: int arr = {0, 1, 2, 3}; I have a declaration of the array in another file as follows: extern int arr;
8
10150
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */ unsigned short int array_size = 25; /*Declare an array named "numbers" using the variable initialized above. */ unsigned short int numbers;
17
7226
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need to show the array data to the end user. Can I do that? How?
4
8095
by: robert.waters | last post by:
I have to parse a binary file having a number of fixed records, each record containing datas in fixed positions. I would like to parse this binary file into an array of structures having members that represent those fields, so that I can access the records in a meaningful way. Using C, I would have defined a struct that I could cast a byte...
0
7659
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8103
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7945
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5481
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5208
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3634
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3618
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2079
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 we have to send another system
0
916
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.