Thread overview
A couple of questions re structs
Jun 25, 2005
Mike
Jun 25, 2005
Chris Sauls
Jun 25, 2005
Mike
Jun 27, 2005
David Medlock
June 25, 2005
1. Are structs allocated on the stack? I'm assuming so, since otherwise you'd have to pin them before passing their addresses to external APIs in case a compacting GC collection moved them, but AFAICS the spec doesn't actually come out and say so.

2. Why don't protection attributes ('private' etc) work with structs? The
compiler doesn't complain about them, but doesn't stop outside code (in another
module) modifying private members directly.


June 25, 2005
Mike wrote:
> 1. Are structs allocated on the stack?
Yes.  Yes they are.  And I agree that it ought to be explicitly stated in the docs.  I could've sworn that it was said somewhere, but I couldn't find it just now.  Walter? Something to update... :)

> 2. Why don't protection attributes ('private' etc) work with structs? The
> compiler doesn't complain about them, but doesn't stop outside code (in another
> module) modifying private members directly.
I thought they did... but I just tested it, and sure enough, they don't.  Which is strange since the D paradigm is that all protection attributes (except 'protected') are really on the /module/ level, not the aggragate level.  If there is a special rule for structs, it also should be said somewhere...

-- Chris Sauls
June 25, 2005
Chris, thanks for the reply.

In article <d9kcgm$2vlf$1@digitaldaemon.com>, Chris Sauls says...
>
>If there is a special rule for structs, it also should be said somewhere...

If there was a special rule, I'd expect a compiler error when attempting to use a protection  attribute in a struct. This feels like an oversight.

And I'd _much_ prefer protection to be implemented for structs. Just because an aggregate is lightweight doesn't mean it doesn't have invariants that need protection.


June 27, 2005
Mike wrote:
> 1. Are structs allocated on the stack? I'm assuming so, since otherwise you'd
> have to pin them before passing their addresses to external APIs in case a
> compacting GC collection moved them, but AFAICS the spec doesn't actually come
> out and say so.
> 

They dont have to be. just use

struct MyStruct  { int a = 1; }
MyStruct* ptr = new MyStruct();
MyStruct* many = new MyStruct[100];