www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - A couple of questions re structs

reply Mike <Mike_member pathlink.com> writes:
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.
Jun 25 2005
next sibling parent reply Chris Sauls <ibisbasenji gmail.com> writes:
Mike wrote:
 1. Are structs allocated on the stack?

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.

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
Jun 25 2005
parent Mike <Mike_member pathlink.com> writes:
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.
Jun 25 2005
prev sibling parent David Medlock <noone nowhere.com> writes:
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];
Jun 27 2005