Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2007

Tip: Looking for answers? Try searching our database.

assert

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
George - 22 Oct 2007 09:40 GMT
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
Peter Oliphant - 22 Oct 2007 16:22 GMT
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,
>
[quoted text clipped - 17 lines]
> thanks in advance,
> George
George - 23 Oct 2007 06:53 GMT
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
[quoted text clipped - 71 lines]
> > thanks in advance,
> > George
Ben Voigt [C++ MVP] - 23 Oct 2007 16:34 GMT
> 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
[quoted text clipped - 78 lines]
>> > thanks in advance,
>> > George
George - 24 Oct 2007 04:32 GMT
Thanks Ben,

regards,
George

> > Thanks Peter,
> >
[quoted text clipped - 85 lines]
> >> > thanks in advance,
> >> > George
Peter Oliphant - 23 Oct 2007 16:38 GMT
> 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,
>
[quoted text clipped - 83 lines]
>> > thanks in advance,
>> > George
Ben Voigt [C++ MVP] - 23 Oct 2007 16:52 GMT
>> 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.

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.
George - 24 Oct 2007 04:58 GMT
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

> >> I have a further question, why do you add ref before class?
> >
[quoted text clipped - 6 lines]
> include interfaces and enums), and also standard C++ classes, PODs, and
> enums.  So for the C++/CLI programmer, there are many different kinds.
David Wilkinson - 24 Oct 2007 13:47 GMT
> 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.

Signature

David Wilkinson
Visual C++ MVP

George - 25 Oct 2007 10:00 GMT
Thanks for your clarification, David!

regards,
George

> > Thanks Ben,
> >
[quoted text clipped - 15 lines]
>
> is for managed code.
George - 24 Oct 2007 04:51 GMT
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
[quoted text clipped - 3 lines]
> 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

> > I have a further question, why do you add ref before class?
>
[quoted text clipped - 156 lines]
> >> > thanks in advance,
> >> > George
Peter Oliphant - 25 Oct 2007 15:49 GMT
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[i].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[i] = 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
> Thanks Peter,
>
[quoted text clipped - 196 lines]
>> >> > thanks in advance,
>> >> > George
George - 26 Oct 2007 10:13 GMT
Great sample! Thanks Peter!

regards,
George

> Sure!
>
[quoted text clipped - 279 lines]
> >> >> > Which one is the most correct to use? I saw people always define
> >> >> > this
Ben Voigt [C++ MVP] - 30 Oct 2007 14:51 GMT
> Sure!
>
[quoted text clipped - 4 lines]
>     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 ;
>
[quoted text clipped - 11 lines]
>    valClass vc ;
>    vc.X = 1 ;

can't work, X is private

>    valClassArray^ vc_array = gcnew valClassArray(3) ;
>    for(int i=0; i < 3; i++)
[quoted text clipped - 280 lines]
>>> >> > thanks in advance,
>>> >> > George
Cholo Lennon - 22 Oct 2007 16:29 GMT
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

--
Cholo Lennon
Bs.As.
ARG

> Hello everyone,
>
[quoted text clipped - 16 lines]
> thanks in advance,
> George
George - 23 Oct 2007 07:20 GMT
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

> 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
[quoted text clipped - 33 lines]
> > thanks in advance,
> > George
Cholo Lennon - 23 Oct 2007 13:49 GMT
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

--
Cholo Lennon
Bs.As.
ARG

> Thanks Cholo,
>
[quoted text clipped - 41 lines]
> > > thanks in advance,
> > > George
George - 23 Oct 2007 15:38 GMT
Thanks Cholo,

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

regards,
George

> No, only when the value is false.
>
[quoted text clipped - 59 lines]
> > > > thanks in advance,
> > > > George

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.