NULL equivalent in freeBASIC

New to FreeBASIC? Post your questions here.
NormJ
Posts: 23
Joined: May 29, 2007 14:53
Location: San Antonio, Texas

NULL equivalent in freeBASIC

Postby NormJ » Jul 23, 2013 15:14

What would be the best way to translate NULL from c/c++ to fb?

Norm
fxm
Posts: 9182
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: NULL equivalent in freeBASIC

Postby fxm » Jul 23, 2013 15:21

Code: Select all

#Ifndef NULL
  #Define NULL 0
#Endif
NormJ
Posts: 23
Joined: May 29, 2007 14:53
Location: San Antonio, Texas

Re: NULL equivalent in freeBASIC

Postby NormJ » Jul 23, 2013 15:22

Thanx. Greatly appreciated!

Norm
D.J.Peters
Posts: 7826
Joined: May 28, 2005 3:28

Re: NULL equivalent in freeBASIC

Postby D.J.Peters » Jul 23, 2013 15:51

Code: Select all

#ifndef NULL
  #define NULL cptr(any ptr, 0)
#endif
Last edited by D.J.Peters on Jul 23, 2013 23:54, edited 1 time in total.
fxm
Posts: 9182
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: NULL equivalent in freeBASIC

Postby fxm » Jul 23, 2013 16:14

D.J.Peters wrote:

Code: Select all

#ifndef NULL
  #define NULL cptr(any ptr, 0)
#endif

Yes, but if you want to use 'NULL' with other variables than any pointer, a compiler warning occurs: 'Implicit conversion'.
My definition is less "pure C" but more convenient (no warning with any type of variable)!
counting_pine
Site Admin
Posts: 6173
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: NULL equivalent in freeBASIC

Postby counting_pine » Jul 23, 2013 16:39

fxm wrote:(no warning with any type of variable)!

That's a disadvantage. For a NULL pointer value, it would be safer to have a warning if you assign it to a non-pointer variable.
(By the way D.J.Peters, it should be 'any ptr', not 'anyptr'.)

My suggested definition is the following:

Code: Select all

#ifndef NULL
const NULL as any ptr = 0
#endif

The #ifndef/#endif lines prevent an error if NULL is defined elsewhere. You can remove them if you want to be sure that this is the definition of NULL in use.
fxm
Posts: 9182
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: NULL equivalent in freeBASIC

Postby fxm » Jul 23, 2013 16:54

I thought that the code:
int x = NULL;
is legal C++.
Because of this and the ensuing confusion, C++11 now has a keyword 'nullptr' representing a null pointer:
int* ptr = nullptr;
fxm
Posts: 9182
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: NULL equivalent in freeBASIC

Postby fxm » Jul 23, 2013 18:04

http://en.allexperts.com/q/C-1040/NULL-value-C-C.htm wrote:
NULL is a macro in C defined in the header <stddef.h>, and C++ inherits it (and of course places it in the header <cstddef>, as per the usual mapping between C header names and C++ header names). Note that at one time C seems to have placed NULL in <stdio.h>

The definition of NULL for C and C++ differs; here is an example from the stddef.h header supplied with MSVC++ 8 (note: cstddef wraps and includes stddef.h in this implementation):

    /* Define NULL pointer value */
    #ifndef NULL
    #ifdef __cplusplus
    #define NULL 0
    #else
    #define NULL ((void *)0)
    #endif
    #endif
As you can see in C++ NULL is just defined as 0, whereas in C it is defined as 0 cast to a void * pointer.
counting_pine
Site Admin
Posts: 6173
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: NULL equivalent in freeBASIC

Postby counting_pine » Jul 23, 2013 18:57

I think my NULL definition is the closest we have to C++'s nullptr.
I don't know why C++'s NULL isn't a 'void *', but I know that it's not exactly the same thing as an 'any ptr'. Perhaps C++ doesn't like casting 'void *' to 'T *'.
Munair
Posts: 836
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: NULL equivalent in freeBASIC

Postby Munair » Sep 07, 2019 10:49

In order to prevent any warnings, a solution might be two different definitions:

Code: Select all

#ifndef NULL
  #define NULL 0
#endif

#ifndef nil
   const nil as any ptr = 0
#endif
caseih
Posts: 1379
Joined: Feb 26, 2007 5:32

Re: NULL equivalent in freeBASIC

Postby caseih » Sep 07, 2019 16:46

I think I'd prefer it the other way around. NULL would be a pointer, and nil would be a zero value.
deltarho[1859]
Posts: 2003
Joined: Jan 02, 2017 0:34
Location: UK

Re: NULL equivalent in freeBASIC

Postby deltarho[1859] » Sep 08, 2019 2:31

I agree with caseih.

However, if we use either then the parameter becomes zero so why bother to differentiate.

So, for my money fxm got it right in the first place with

Code: Select all

#Ifndef NULL
  #Define NULL 0
#Endif

At MSDN many APIs use NULL when a pointer to a structure is not required. Take the BCryptEncrypt function for example. The fourth parameter is only used for asymmetric keys otherwise NULL. In PoweBASIC I would use Byval %NULL where %NULL = 0 in it's include files. In FreeBASIC's bi files the fourth parameter is 'byval pPaddingInfo as any ptr'. I just use 0, but I could use fxm's definition for clarity and in line with MSDN. When I use 0 I don't get any warnings.

Return to “Beginners”

Who is online

Users browsing this forum: grindstone, MSN [Bot] and 47 guests