Re: assert

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Thanks Peter,


Your reply is so comprehensive!

I have a question about your following comments,

Value classes have some major restrictions about what methods it can have.
But their main usefulness comes in arrays. If you create an array of
reference class instances the array can only contain pointers to the
pointers of the instances, so each instance must be created independently
(ala 'gcnew'). An array of value class instances need only have code which
allocates memory for the array pointers itself, which in turn automatically
allocates the memory for its elements.

Could you show some simple pseudo code please about the advantage of value
class compared with ref class please? I am not 100% sure about your above
comments. But I am interested to learn from you.


regards,
George

"Peter Oliphant" wrote:

I have a further question, why do you add ref before class?

There are two basic kinds of classes (or structs, since structs are now just
classes that are internally default public): reference classes and value
classes.

Reference classes must be created ala gcnew and saved in pointers. Value
classes, on the other hand, can be created as 'concrete' instances, much
like 'long' or 'int'. That is, one can just create an 'int' without a
pointer:

int i ;

So, if I define a class as a reference class I put the keyword 'ref' in
front of it. If it is a value class I put the keyword 'value' in front of
it. Here is some sample code to make it clearer:

ref class refClass
{
// code
} ;

value class valClass
{
//code
} ;

refClass^ refClassPTR = gcnew refClass( ) ;

valClass valClassINSTANCE ;

The major advantage to a reference class is it can be passed into a method
via just its pointer. It also frees up its own memory once nobody is
referencing it (garbage collection, hence the 'gc' in 'gcnew')..

Value class instances can be passed by pointer, but they can also be passed
'by value'. That is, the method then is working with a new copy of the
instance, so the orginal stays unchanged. However, to do this, all of the
members of the class are also passed and copied, which uses up more
resources (memory and time) than just passing by reference.

In contrat, any method which is passed a parameter via a pointer can change
that parameter (the external value as well as the value internal to the
method).

If you are use to old-school structs, a valClass is more like one of these
old struct's. That's because it can be thought as a 'concrete' entitity
holding the values of its components, in contrast to a pointer to memory
containing this data. A pointer can change what it is pointing to, a struct
retains its values unless manually changed directly.

Value classes have some major restrictions about what methods it can have.
But their main usefulness comes in arrays. If you create an array of
reference class instances the array can only contain pointers to the
pointers of the instances, so each instance must be created independently
(ala 'gcnew'). An array of value class instances need only have code which
allocates memory for the array pointers itself, which in turn automatically
allocates the memory for its elements.

I typically use 'ref' classes more since they are more flexible, both in
terms of features and moveability (passing a pointer is typically faster
than passing all the data the pointer is pointing to). And now that we have
great 'garbage collection', it is nice that an instance of a reference class
will free up its own memory when no longer needed (i.e., if no longer
pointed to then no longer referenced and thus no longer needed). In
contrast, a value class instance often requires intentional deletion in
order to free up its resources (unless only used as a reference, in which
case a reference class might have been a better choice for its definition).

Hope that helps more than confuses! :)

[==Peter==]


"George" <George@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:F7F3302C-C41E-4764-840C-8287505F0E3D@xxxxxxxxxxxxxxxx
Thanks Peter,


The class is very useful. I have a further question, why do you add ref
before class?


regards,
George

"Peter Oliphant" wrote:

I created and use this class, you might find it useful. It contains some
useful methods, pretty easy to figure out from their names. Note that I
use
'assert' to answer your question:

#include 'assert.h'

#define MY_DEBUG (true)

ref class My_Debug
{
public:

static void Assert( bool condition )
{
Assert( condition, "<no text>" ) ;
}

static void Show( String^ comment )
{
Console::WriteLine( comment ) ;
}

static void Assert( bool condition, String^ info )
{
if ( condition ) { return ; }
Console::WriteLine() ;
Console::WriteLine( info ) ;
Console::WriteLine() ;
assert(!MY_DEBUG) ; // when debug is on then assert false
}

static void Deny( bool condition )
{
Assert( !condition ) ;
}

static void Deny( bool condition, String^ info )
{
Assert( !condition, info ) ;
}

static void Abort( String^ info )
{
Assert( false, info ) ;
}

static void Abort()
{
Abort( "*abort*" ) ;
}
} ;

// [==Peter==]

"George" <George@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:C12D5DAD-5D70-41CC-8CC3-3423482E0B41@xxxxxxxxxxxxxxxx
Hello everyone,


I saw a couple of form of assert in code on Windows,

1. ASSERT;
2. assert;
3. _ASSERT;
4. _assert.

Which one is the most correct to use? I saw people always define this
to
that, and I want to find the root one which is defined by Windows.

I also saw people manually define assert to NULL if macro DEBUG or
_DEBUG
is
not defined, is that necessary?

I feel assert is in a mess and I want to find a clear and unified way
for
this item.


thanks in advance,
George






.



Relevant Pages

  • Re: "Must instantiate controlled types at library level." Why?
    ... > The array bounds become unknown. ... > just the size of an pointer. ... It also show that operators attached to a reference type act ... > the compiler will stop creating copy constructors and warn you if you ...
    (comp.lang.ada)
  • Re: assert
    ... value class valClass ... create the array POINTER. ... by passing JUST the pointer to rc it has virtually passed ... classes that are internally default public): reference classes and value ...
    (microsoft.public.dotnet.languages.vc)
  • Re: assert
    ... Reference classes must be created ala gcnew and saved in pointers. ... via just its pointer. ... static void Assert(bool condition) ...
    (microsoft.public.dotnet.languages.vc)
  • Re: pass an array throuh a function
    ... >Doesn't this just demonstrate a way of using a reference ... >to the pointer. ... You have still not passed the actual array ... If you call show2 passing an actual, honest-to-goodness float *, however ...
    (alt.comp.lang.learn.c-cpp)
  • Re: assert
    ... value class valClass ... create the array POINTER. ... by passing JUST the pointer to rc it has virtually passed all ... classes that are internally default public): reference classes and value ...
    (microsoft.public.dotnet.languages.vc)