Re: Allocating structs on the stack

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 07/27/04


Date: Tue, 27 Jul 2004 19:37:25 +0100

CharleKann <CharleKann@discussions.microsoft.com> wrote:
> I am preparing for a class this fall that teaches C# programming.
> Most of the students know Java, so I am trying to show some of the
> differences. One big academic issue to me is that use of structs in
> C#. I love that you can have stack allocation, and allocate them as
> part of an array.

Not at the same time, of course...

> However when trying to build a little application
> to show what is happening, I got some very strange and to me
> inconsistant behavior.
>
> Before being skewed in my class, I would like to know what is going
> on in this program. Apparently I cannot access properties in an
> instance of a struct until it is fully initialized (unless it is in
> an array, in which case the rule seems to not be implemented). This
> is a problem, as I wanted to make the fields private to hide them,
> and use the accessors. But I don't see to be able to.
>
> I can get around this by using the "new" operator, but once again, I
> wanted to avoid it to point out the difference between structs and
> classes.

Why should using the "new" operator be a problem? I would suggest that
using a constructor for a struct is the right way of creating an
instance.

> Anyway, the code I am working with is below. I commented out the line
> that causes the compiler to fail. Can anyone tell me what the
> justification is for it failing? And am I wrong, or are the rules
> different if it is an array?

The compiler doesn't know whether an array element has been fully
constructed, but it can check it for local variables.

-- 
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Relevant Pages

  • Re: Teaching new tricks to an old dog (C++ -->Ada)
    ... > attributes helps both the writer and the compiler. ... You can build algorithms around language provided ... > Maybe, if one gets used to template programming, the basis of the ... > Try telling someone that useful array attributes, ...
    (comp.lang.ada)
  • Re: Teaching new tricks to an old dog (C++ -->Ada)
    ... > attributes helps both the writer and the compiler. ... You can build algorithms around language provided ... > Maybe, if one gets used to template programming, the basis of the ... > Try telling someone that useful array attributes, ...
    (comp.lang.cpp)
  • Re: Teaching new tricks to an old dog (C++ -->Ada)
    ... attributes helps both the writer and the compiler. ... > catch at compile time any out of boundaries access. ... C++ array like types don't have these properties. ... Maybe, if one gets used to template programming, the basis of the ...
    (comp.lang.ada)
  • Re: Teaching new tricks to an old dog (C++ -->Ada)
    ... attributes helps both the writer and the compiler. ... > catch at compile time any out of boundaries access. ... C++ array like types don't have these properties. ... Maybe, if one gets used to template programming, the basis of the ...
    (comp.lang.cpp)
  • Re: Cheap tricks for map data structure
    ... "object oriented programming" that I saw, ... So C structs are a perfectly reasonable basis for implementing ... memory payload, and being locked into using new/delete by having one ... In the absence of padding bytes, the array of structs (or other ...
    (rec.games.roguelike.development)