assert

G

Guest

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
 
P

Peter Oliphant

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==]
 
C

Cholo Lennon

Generally I prefer to use 'assert' because is standard, but in MFC/ATL I use
ASSERT because the debugger stop on the offending line (this is not true for
assert). I've just used _ASSERT on a few occasions (when I used CRT debugging
functions to find a leak).

Be aware that:

- assert depends of NDEBUG macro.
- ASSERT and _ASSERT depend of _DEBUG macro.


Regards
 
G

Guest

Thanks Peter,


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


regards,
George

Peter Oliphant said:
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 said:
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
 
G

Guest

Thanks Cholo,


What do you mean *because the debugger stop on the offending line*? Debugger
will stop even if the value is true?


regards,
George
 
C

Cholo Lennon

No, only when the value is false.

I'm sorry, when I said "...debugger stops on...", I should have said
"...debugger breaks on...".

Try to debug a program using assert(false) and ASSERT(false) to see what
happens. In the 1st case the debugger break into an internal function. In the
2nd case it just break on the ASSERT line.

Regards
 
G

Guest

Thanks Cholo,


It is my mistake to misunderstand your points. Now I am clear now.


regards,
George
 
B

Ben Voigt [C++ MVP]

George said:
Thanks Peter,


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

That makes it a .NET class, visible from other .NET languages.

regards,
George

Peter Oliphant said:
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 said:
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
 
P

Peter Oliphant

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 said:
Thanks Peter,


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


regards,
George

Peter Oliphant said:
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 said:
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
 
B

Ben Voigt [C++ MVP]

Peter Oliphant said:
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.

Everything you wrote is true about .NET types. The C++/CLI compiler
supports .NET types of both varieties (actually four varieties, if you
include interfaces and enums), and also standard C++ classes, PODs, and
enums. So for the C++/CLI programmer, there are many different kinds.
 
G

Guest

Thanks Ben,


regards,
George

Ben Voigt said:
George said:
Thanks Peter,


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

That makes it a .NET class, visible from other .NET languages.

regards,
George

Peter Oliphant said:
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==]

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
 
G

Guest

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 said:
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 said:
Thanks Peter,


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


regards,
George

Peter Oliphant said:
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==]

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
 
G

Guest

Thanks Ben,


I am using unmanaged C++ in Visual Studio 2005. Could I use such keyword
like ref and value? If yes, could you recommmend some learning resource
please?


regards,
George
 
D

David Wilkinson

George said:
Thanks Ben,


I am using unmanaged C++ in Visual Studio 2005. Could I use such keyword
like ref and value? If yes, could you recommmend some learning resource
please?

George:

The keywords ref and value are for managed code (C++/CLI) only.

If you are using unmanaged code, you would be better posting in

microsoft.public.vc.language

The group

microsoft.public.dotnet.languages.vc

is for managed code.
 
P

Peter Oliphant

Sure!

value class valClass
{
int X ;
// NEEDS a constructor
valClass( int x) { X= x ; }
} ;
typdef array<valClass> ValClassArray ;

ref class refClass
{
int X ;
// DOESN'T need a constructor, but helpful
}
typedef array<refClass^> RefClassArray ;

main()
{
// valClass

valClass vc ;
vc.X = 1 ;
valClassArray^ vc_array = gcnew valClassArray(3) ;
for(int i=0; i < 3; i++)
{
vc_array.X=i ;
}

//same as:
//vc_array[0].X = 0;
//vc_array[1].X = 1 ;
//vc_array[2].X = 2;

//ref Class

refClass^ rc = gcnew refClass(1) ; //rc->X=1 ;
refClassArray rf_array^ = gcnew refClassArray(3) ;
for(int i = 0; i < 3; i++)
{
rf_array = gcnew refClass(i) ;
}

//same as:
//vc_array[0] = gcnew refClass(0) ;
//vc_array[1] = gcnew refClass(1) ;
//vc_array[2] = gcnew refClass(2) ;
} ;

If you study the above code some difference will start to be clear. For
example, one has to create (gcnew) EVERY single element of a ref class
array, while the value class immediately has the storage allocate when you
create the array POINTER. This shows some good advantages to value classes.

void refMethod( refClass^ rc )
{
rc->X=1 // original rc changed
}

void valMethod( valClass vc )
{
vc.X=1 // internal vc COPY changed, original vc unchanged!
}

In refMethod, by passing JUST the pointer to rc it has virtually passed all
of the fields of rc (in this case just X). in valMethod by passing vc it
must copy every field of valClass separately and put it on the stack. In
this case it also just has X, but if valClass had a LOT of fields in it ALL
of them would need to be copied and placed on the stack. Thus for classes
with a large amount of required storage space for its variables, the ref
class appraoch is far more efficient since it need only pass a single
address.

Also, in the refMethod case, actions performed on rc are being performed on
the original rc passed in. Those performed on vc by valClass are performed
on a COPY of the orginal vc, and the original vc in unaffected. There are
advantage to both depending on your needs. For example, if you want the
method to update a variable, it should be a ref class variable. If you just
want to use the value of a variable for calculations, then a value class
variable is best. In a sense, a ref class variable has read/write privileges
while a value class variable only had read privilages in terms of access
(assuming the value class variable is not passed in by pointer, in which
case I beleive you can once again manipulate the original). In affect, a
pointer allows modification, the instance itself, used as a parameter to a
method, acts as if it is read-only. And only value class variables can be
passed in by instance (in contrasr to pointer).

The BASIC language makes this VERY clear. There are two ways to pass a
variable to method in that language: byRef and ByVal. bY Ref is equivalent
to passing a variable via its pointer, allowing modification to the
original. By Val copies the variable and manipulates it leaving the original
alone.

Hope that helps a bit! : )

[==Peter==]


In valMethod
George said:
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 said:
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 said:
Thanks Peter,


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


regards,
George

:

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==]

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
 
G

Guest

Great sample! Thanks Peter!


regards,
George

Peter Oliphant said:
Sure!

value class valClass
{
int X ;
// NEEDS a constructor
valClass( int x) { X= x ; }
} ;
typdef array<valClass> ValClassArray ;

ref class refClass
{
int X ;
// DOESN'T need a constructor, but helpful
}
typedef array<refClass^> RefClassArray ;

main()
{
// valClass

valClass vc ;
vc.X = 1 ;
valClassArray^ vc_array = gcnew valClassArray(3) ;
for(int i=0; i < 3; i++)
{
vc_array.X=i ;
}

//same as:
//vc_array[0].X = 0;
//vc_array[1].X = 1 ;
//vc_array[2].X = 2;

//ref Class

refClass^ rc = gcnew refClass(1) ; //rc->X=1 ;
refClassArray rf_array^ = gcnew refClassArray(3) ;
for(int i = 0; i < 3; i++)
{
rf_array = gcnew refClass(i) ;
}

//same as:
//vc_array[0] = gcnew refClass(0) ;
//vc_array[1] = gcnew refClass(1) ;
//vc_array[2] = gcnew refClass(2) ;
} ;

If you study the above code some difference will start to be clear. For
example, one has to create (gcnew) EVERY single element of a ref class
array, while the value class immediately has the storage allocate when you
create the array POINTER. This shows some good advantages to value classes.

void refMethod( refClass^ rc )
{
rc->X=1 // original rc changed
}

void valMethod( valClass vc )
{
vc.X=1 // internal vc COPY changed, original vc unchanged!
}

In refMethod, by passing JUST the pointer to rc it has virtually passed all
of the fields of rc (in this case just X). in valMethod by passing vc it
must copy every field of valClass separately and put it on the stack. In
this case it also just has X, but if valClass had a LOT of fields in it ALL
of them would need to be copied and placed on the stack. Thus for classes
with a large amount of required storage space for its variables, the ref
class appraoch is far more efficient since it need only pass a single
address.

Also, in the refMethod case, actions performed on rc are being performed on
the original rc passed in. Those performed on vc by valClass are performed
on a COPY of the orginal vc, and the original vc in unaffected. There are
advantage to both depending on your needs. For example, if you want the
method to update a variable, it should be a ref class variable. If you just
want to use the value of a variable for calculations, then a value class
variable is best. In a sense, a ref class variable has read/write privileges
while a value class variable only had read privilages in terms of access
(assuming the value class variable is not passed in by pointer, in which
case I beleive you can once again manipulate the original). In affect, a
pointer allows modification, the instance itself, used as a parameter to a
method, acts as if it is read-only. And only value class variables can be
passed in by instance (in contrasr to pointer).

The BASIC language makes this VERY clear. There are two ways to pass a
variable to method in that language: byRef and ByVal. bY Ref is equivalent
to passing a variable via its pointer, allowing modification to the
original. By Val copies the variable and manipulates it leaving the original
alone.

Hope that helps a bit! : )

[==Peter==]


In valMethod
George said:
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 said:
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==]


Thanks Peter,


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


regards,
George

:

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==]

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
 
B

Ben Voigt [C++ MVP]

Peter Oliphant said:
Sure!

value class valClass
{
int X ;
// NEEDS a constructor
valClass( int x) { X= x ; }
} ;

value classes aren't required to have a non-default constructor (and the
default constructor is system-defined to zero-fill and cannot be changed).
However since your member variable X is private, there'd be no other way to
change it which might give you compiler warnings.

typdef array<valClass> ValClassArray ;

ref class refClass
{
int X ;
// DOESN'T need a constructor, but helpful
}
typedef array<refClass^> RefClassArray ;

main()
{
// valClass

valClass vc ;
vc.X = 1 ;

can't work, X is private

valClassArray^ vc_array = gcnew valClassArray(3) ;
for(int i=0; i < 3; i++)
{
vc_array.X=i ;
}

//same as:
//vc_array[0].X = 0;
//vc_array[1].X = 1 ;
//vc_array[2].X = 2;

//ref Class

refClass^ rc = gcnew refClass(1) ; //rc->X=1 ;
refClassArray rf_array^ = gcnew refClassArray(3) ;
for(int i = 0; i < 3; i++)
{
rf_array = gcnew refClass(i) ;
}

//same as:
//vc_array[0] = gcnew refClass(0) ;
//vc_array[1] = gcnew refClass(1) ;
//vc_array[2] = gcnew refClass(2) ;
} ;

If you study the above code some difference will start to be clear. For
example, one has to create (gcnew) EVERY single element of a ref class
array, while the value class immediately has the storage allocate when you
create the array POINTER. This shows some good advantages to value
classes.

void refMethod( refClass^ rc )
{
rc->X=1 // original rc changed
}

void valMethod( valClass vc )
{
vc.X=1 // internal vc COPY changed, original vc unchanged!
}

In refMethod, by passing JUST the pointer to rc it has virtually passed
all of the fields of rc (in this case just X). in valMethod by passing vc
it must copy every field of valClass separately and put it on the stack.
In this case it also just has X, but if valClass had a LOT of fields in it
ALL of them would need to be copied and placed on the stack. Thus for
classes with a large amount of required storage space for its variables,
the ref class appraoch is far more efficient since it need only pass a
single address.

Also, in the refMethod case, actions performed on rc are being performed
on the original rc passed in. Those performed on vc by valClass are
performed on a COPY of the orginal vc, and the original vc in unaffected.
There are advantage to both depending on your needs. For example, if you
want the method to update a variable, it should be a ref class variable.
If you just want to use the value of a variable for calculations, then a
value class variable is best. In a sense, a ref class variable has
read/write privileges while a value class variable only had read
privilages in terms of access (assuming the value class variable is not
passed in by pointer, in which case I beleive you can once again
manipulate the original). In affect, a pointer allows modification, the
instance itself, used as a parameter to a method, acts as if it is
read-only. And only value class variables can be passed in by instance (in
contrasr to pointer).

The BASIC language makes this VERY clear. There are two ways to pass a
variable to method in that language: byRef and ByVal. bY Ref is equivalent
to passing a variable via its pointer, allowing modification to the
original. By Val copies the variable and manipulates it leaving the
original alone.

Hope that helps a bit! : )

[==Peter==]


In valMethod
George said:
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 said:
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==]


Thanks Peter,


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


regards,
George

:

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==]

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
 

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