VS.NET 2005 'array' seems more complex to me than '__gc[]'

P

Peter Oliphant

While it does look like 2005 does use a better syntax in general for garbage
collection than 2003, here is something I think went the other way. arrays.
Do people really think that:

array<MyClass ^> ^ MyArray = gcnew array<MyClass ^>(100);

is a better syntax than:

MyClass* MyArray __gc [] = new MyClass* __gc [100] ;

[==P==]
 
B

beginthreadex

My preference is to keep "__gc". I think it makes things more clear.
While it does look like 2005 does use a better syntax in general for
garbage collection than 2003, here is something I think went the other
way. arrays. Do people really think that:

array<MyClass ^> ^ MyArray = gcnew array<MyClass ^>(100);

is a better syntax than:

MyClass* MyArray __gc [] = new MyClass* __gc [100] ;

[==P==]
 
A

adebaene

Peter Oliphant a écrit :
While it does look like 2005 does use a better syntax in general for garbage
collection than 2003, here is something I think went the other way. arrays.
Do people really think that:

array<MyClass ^> ^ MyArray = gcnew array<MyClass ^>(100);

is a better syntax than:

MyClass* MyArray __gc [] = new MyClass* __gc [100] ;

If you're used to native STL (std::vector<Foo>), it certainly looks
clearer IMHO.

Arnaud
MVP - VC
 
D

David Wilkinson

Peter Oliphant a écrit :

While it does look like 2005 does use a better syntax in general for garbage
collection than 2003, here is something I think went the other way. arrays.
Do people really think that:

array<MyClass ^> ^ MyArray = gcnew array<MyClass ^>(100);

is a better syntax than:

MyClass* MyArray __gc [] = new MyClass* __gc [100] ;


If you're used to native STL (std::vector<Foo>), it certainly looks
clearer IMHO.

Arnaud
MVP - VC

Arnaud:

I am only just starting to look at managed code (don't have the VC8
compiler yet!), but that is what I thought too. The old form does not
look like legal standard C++, but the new one is like

std::vector<MyClass *> * pMyArray = new std::vector<MyClass *>(100);

This is legal standard C++, though not what I would use (never use new
unless you have to). I would do

std::vector<MyClass *> myArray(100);

Now I read that C++/CLI has optional "stack semantics". So can I do

array<MyClass ^> myArray(100);

? Or is this not allowed? If not, why not?

David Wilkinson
 
R

RalphTheExpert

You said "never use new unless you have to".

You and I share the same feelings about coding style.

I, too, am new to MC++ and I can tell you that I both like it and
despise it.

It seems that MC++ turns our kind of coding on its head and everything
ends up on the heap. Destructors don't get executed when you want them
to under MC++. (Maybe I'm missing something.)

Anyway, I've only got MC++ 2003. Does the stack semantics apply to
that version?
 
A

Arnaud Debaene

David said:
I am only just starting to look at managed code (don't have the VC8
compiler yet!), but that is what I thought too. The old form does not
look like legal standard C++, but the new one is like

std::vector<MyClass *> * pMyArray = new std::vector<MyClass *>(100);

This is legal standard C++, though not what I would use (never use new
unless you have to). I would do

std::vector<MyClass *> myArray(100);
I definitely agree with you in native C++ (I almost never write a "new"
anymore in native code). Anyway, in the managed world, newing an object is
the only way to create it and register it with the GC (btw, this is the same
thing in Java). Now, as MC++ and C++/CLI are designed to be closer to the
metal than C# or VB, the syntax is a bit clumsy.
Now I read that C++/CLI has optional "stack semantics". So can I do

array<MyClass ^> myArray(100);

? Or is this not allowed? If not, why not?

It is not, and I don't know why :-( It seems that stack semantic is not
available for arrays...
Anyway, this is not really necessary here : The main goal of stack semantic
is not to ease syntax, it is to provide RAII. RAII has a meaning only if
there is some ressource to free, but an array does not hold any such
ressource (except memory of course, which is taken care of by the GC).

Arnaud
MVP - VC
 
D

David Wilkinson

Arnaud said:
I definitely agree with you in native C++ (I almost never write a "new"
anymore in native code). Anyway, in the managed world, newing an object is
the only way to create it and register it with the GC (btw, this is the same
thing in Java). Now, as MC++ and C++/CLI are designed to be closer to the
metal than C# or VB, the syntax is a bit clumsy.




It is not, and I don't know why :-( It seems that stack semantic is not
available for arrays...
Anyway, this is not really necessary here : The main goal of stack semantic
is not to ease syntax, it is to provide RAII. RAII has a meaning only if
there is some ressource to free, but an array does not hold any such
ressource (except memory of course, which is taken care of by the GC).

Arnaud
MVP - VC

Hi Arnaud:

Yes I understand that the reference class is really always allocated on
the GC heap (though Herb Sutter has said that even that could be relaxed
in future versions in some circumstances), and that the stack semantics
is intended for use with RAII. And a great improvement that is!!

But once stack semantics has been introduced, I fail to see why it might
not be universally used. Surely a handle and a "reference to a handle"
must be interchangeable as far as the compiler is concerned (except that
the latter triggers a destructor call when it goes out of scope). Isn't
it just like the relationship between bare pointer and auto-ptr?. For
non-RAII classes, the presence of an empty destructor cannot be any more
of a problem than it is in standard C++.

David Wilkinson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top