Re: How to do "Compile time" initialization of record array?



It should be noted that there is proposed syntax for C# 3.0 which will
allow initialization with the following syntax:

public struct MyStruct
{
public double L;
public double M;
public double S;
}

MyStruct[] lengthArray[] = new MyStruct[]
{
{ L = 1.267004226, M = 49.988884080, S = 0.053112191},
{ L = 0.511237696, M = 52.695975300, S = 0.048692684},
{ L = -0.452244460, M = 56.628428550, S = 0.044116830}
}

I might have not gotten it completely correct, but the syntax is
something like that.

What actually gets compiled is the same array initialization as there is
now, but code that will actually populate the properties/fields like regular
assignments. It's been a while since I used the C# 3.0 preview, but the
proposed spec amendments go into much more detail.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> wrote in message
news:MPG.1e1f96f3a864606598cc2c@xxxxxxxxxxxxxxxxxxxxxxx
> John Kelsey <kelseyj@xxxxxxxxxx> wrote:
>> For example, one of my "C" programs has
>>
>> struct
>> {
>> double L;
>> double M;
>> double S;
>> } LengthArray[] = {
>> { 1.267004226, 49.988884080, 0.053112191}, // 1
>> { 0.511237696, 52.695975300, 0.048692684}, // 2
>> { -0.452244460, 56.628428550, 0.044116830}, // 3
>>
>> This is some statistical data (there are 76 records per array) and I have
>> Length, Weight, and Circumference arrays.
>>
>> I typically am targeting some low "horsepower" handheld devices.
>>
>> Even in C, I could run time initialize my arrays but I didn't for at
>> least a
>> couple of reasons...
>> 1) Performance, why waste runtime cycles building an array when it could
>> be
>> pre-built by the compiler
>> 2) Run time building meant that I carried two copies of my data in the
>> "execution footprint". One copy in code segment and the other copy in
>> the
>> data segment.
>
> In both of these cases, I'd expect the time and memory taken to be
> really pretty tiny. You'll be wasting a few K and a couple of
> milliseconds (once), even on a handheld device. These days, that's not
> likely to be a significant problem.
>
>> I understand that this really doesn't present a challenge to a modern
>> device, especially in a desktop environment, it was just a style I
>> have/had.
>
> Sure.
>
>> I don't understand C# the way I do C. Things that used to be important,
>> no
>> longer seem to matter as much.
>
> Indeed. However, the things which were a real pain in C are much easier
> in C# and .NET. Modern languages tend to value the time of the
> developer over the time of the processor, on the grounds that it's
> generally a lot more expensive :)
>
>> I've appreciated all of the responses and I'm certain I'll ask more "old
>> fashioned" questions as I learn.
>
> Goodo. Do keep 'em coming.
>
> --
> Jon Skeet - <skeet@xxxxxxxxx>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too


.



Relevant Pages

  • Re: Initialisation of derived type array
    ... but I want to initialise each array element to a different ... special syntax for any of this stuff. ... The syntax of initialization in a type declaration, ... It is a syntax for writing a derived type value wherever ...
    (comp.lang.fortran)
  • Re: Initialization of character array in derived type
    ... In fact, if the array is large enough for the time to be measurable, ... be faster than doing it with static initialization. ... I see that your init-mat_data is a pointer. ... allocate or assign the pointer somewhere. ...
    (comp.lang.fortran)
  • Re: It Hurts When I Do This
    ... We do have a syntax that means the entire array - the array name. ... I disagree that it helps to use a special notation to indicate that ... I think it a mistake to throw all this kind of thing in the language. ...
    (comp.lang.fortran)
  • Re: What does this script means?
    ... Richard Cornford wrote: ... Will add a new array object to the object "window" under the name foo, ... it is perfectly valid to use a function reference as the operand ... But that error is the syntax error that you introduced by moving the ...
    (comp.lang.javascript)
  • Re: IVF is a wonderful compiler (was IVF continues to disappoint (relatively))
    ... In quite a few of these cases g95 is still unable to detect ... array syntax. ... Lots of competing compilers rewrite all array syntax into F77 ...
    (comp.lang.fortran)