Question about (double *)NULL

J

jacob navia

Rouben Rostamian a écrit :
Umfpack is a C library for computations dealing with sparse
matrices. Several examples in the User's Guide use a certain
cast that puzzles me. Here is an example.

The prototype of the function umfpack_di_symbolic() is:

int umfpack_di_symbolic
(
int n_row,
int n_col,
const int Ap [ ],
const int Ai [ ],
const double Ax [ ],
void **Symbolic,
const double Control [UMFPACK_CONTROL],
double Info [UMFPACK_INFO]
);

The header file umfpack.h has:
#define UMFPACK_CONTROL 20

Here is a sample usage:

int main (void)
{
double *null = (double *) NULL ;
...
(void) umfpack_di_symbolic (n, n, Ap, Ai, Ax, &Symbolic, null, null) ;
...
return (0) ;
}

(Code fragments are put here by "cut-and-paste"ing from the manual.)

My question is: Does the cast in (double *)NULL accomplish anything?
It seems to me that the call to umfpack_di_symbolic() equivalent to:

umfpack_di_symbolic (n, n, Ap, Ai, Ax, &Symbolic, NULL, NULL) ;

Comments?

You are right. The only difference is that you waste code space in a
useless assignment, and that afterwards you write NULL in lowercase.

A
#define null NULL
would accomplish the same thing
 
R

Rouben Rostamian

Umfpack is a C library for computations dealing with sparse
matrices. Several examples in the User's Guide use a certain
cast that puzzles me. Here is an example.

The prototype of the function umfpack_di_symbolic() is:

int umfpack_di_symbolic
(
int n_row,
int n_col,
const int Ap [ ],
const int Ai [ ],
const double Ax [ ],
void **Symbolic,
const double Control [UMFPACK_CONTROL],
double Info [UMFPACK_INFO]
);

The header file umfpack.h has:
#define UMFPACK_CONTROL 20

Here is a sample usage:

int main (void)
{
double *null = (double *) NULL ;
...
(void) umfpack_di_symbolic (n, n, Ap, Ai, Ax, &Symbolic, null, null) ;
...
return (0) ;
}

(Code fragments are put here by "cut-and-paste"ing from the manual.)

My question is: Does the cast in (double *)NULL accomplish anything?
It seems to me that the call to umfpack_di_symbolic() equivalent to:

umfpack_di_symbolic (n, n, Ap, Ai, Ax, &Symbolic, NULL, NULL) ;

Comments?
 
M

Michael Mair

Rouben said:
Umfpack is a C library for computations dealing with sparse
matrices. Several examples in the User's Guide use a certain
cast that puzzles me. Here is an example.

The prototype of the function umfpack_di_symbolic() is:

int umfpack_di_symbolic
(
int n_row,
int n_col,
const int Ap [ ],
const int Ai [ ],
const double Ax [ ],
void **Symbolic,
const double Control [UMFPACK_CONTROL],
double Info [UMFPACK_INFO]
);

The header file umfpack.h has:
#define UMFPACK_CONTROL 20

Here is a sample usage:

int main (void)
{
double *null = (double *) NULL ;
...
(void) umfpack_di_symbolic (n, n, Ap, Ai, Ax, &Symbolic, null, null) ;
...
return (0) ;
}

(Code fragments are put here by "cut-and-paste"ing from the manual.)

My question is: Does the cast in (double *)NULL accomplish anything?
It seems to me that the call to umfpack_di_symbolic() equivalent to:

umfpack_di_symbolic (n, n, Ap, Ai, Ax, &Symbolic, NULL, NULL) ;

Comments?

Educated guess:
It proactively shuts up a C++ compiler abused to compile C...

,---
$ cat doublecast.c
int main (void)
{
double *foo = (void *)0;

return 0;
}

$ gcc -std=c89 -pedantic -Wall -O doublecast.c -c
doublecast.c: In function `main':
doublecast.c:3: warning: unused variable `foo'

$ g++ -std=c++98 -pedantic -Wall -O doublecast.c -c
doublecast.c: In function `int main()':
doublecast.c:3: error: invalid conversion from `void*' to `double*'
doublecast.c:3: warning: unused variable 'foo'
`---
<OT>The correct C++ solution is to use "0" instead of "NULL".</OT>

As most library headers nowadays test whether C or C++ is the
language mode they are processed with, you usually have NULL
defined to be 0 for C++ and (void *)0 for C.


Cheers
Michael
 
A

A. Bolmarcich

Umfpack is a C library for computations dealing with sparse
matrices. Several examples in the User's Guide use a certain
cast that puzzles me. Here is an example.

The prototype of the function umfpack_di_symbolic() is:

int umfpack_di_symbolic
(
int n_row,
int n_col,
const int Ap [ ],
const int Ai [ ],
const double Ax [ ],
void **Symbolic,
const double Control [UMFPACK_CONTROL],
double Info [UMFPACK_INFO]
);

The header file umfpack.h has:
#define UMFPACK_CONTROL 20

Here is a sample usage:

int main (void)
{
double *null = (double *) NULL ;
...
(void) umfpack_di_symbolic (n, n, Ap, Ai, Ax, &Symbolic, null, null) ;
...
return (0) ;
}

(Code fragments are put here by "cut-and-paste"ing from the manual.)

My question is: Does the cast in (double *)NULL accomplish anything?
It seems to me that the call to umfpack_di_symbolic() equivalent to:

umfpack_di_symbolic (n, n, Ap, Ai, Ax, &Symbolic, NULL, NULL) ;

Comments?

The cast is not needed in C (but is in C++).

If the package was originally written before C had function prototypes,
then the sample usage (with arguments of type double *) was needed for
compilers that used different representations for pointers to different
types. With function prototypes, the compiler, if necessary, converts
arguments with a value of NULL to the null pointer of the type of the
corresponding function parameter.
 
J

Jack Klein

Umfpack is a C library for computations dealing with sparse
matrices. Several examples in the User's Guide use a certain
cast that puzzles me. Here is an example.

The prototype of the function umfpack_di_symbolic() is:

int umfpack_di_symbolic
(
int n_row,
int n_col,
const int Ap [ ],
const int Ai [ ],
const double Ax [ ],
void **Symbolic,
const double Control [UMFPACK_CONTROL],
double Info [UMFPACK_INFO]
);

The header file umfpack.h has:
#define UMFPACK_CONTROL 20

Here is a sample usage:

int main (void)
{
double *null = (double *) NULL ;
...
(void) umfpack_di_symbolic (n, n, Ap, Ai, Ax, &Symbolic, null, null) ;
...
return (0) ;
}

(Code fragments are put here by "cut-and-paste"ing from the manual.)

My question is: Does the cast in (double *)NULL accomplish anything?
It seems to me that the call to umfpack_di_symbolic() equivalent to:

umfpack_di_symbolic (n, n, Ap, Ai, Ax, &Symbolic, NULL, NULL) ;

Comments?

The cast is not needed in C (but is in C++).

No it is not.

<OT>
The C++ standard requires that the macro NULL be defined as an integer
constant expression that evaluates to a value of 0. It does not allow
it to be defined as such an expression cast to void *, as C does.
</OT>
 
P

Peter Nilsson

Rouben said:
Umfpack is a C library for computations dealing with sparse
matrices. Several examples in the User's Guide use a certain
cast that puzzles me. Here is an example.

The prototype of the function umfpack_di_symbolic() is:

int umfpack_di_symbolic
(
int n_row,
int n_col,
const int Ap [ ],
const int Ai [ ],
const double Ax [ ],
void **Symbolic,
const double Control [UMFPACK_CONTROL],
double Info [UMFPACK_INFO]
);

The header file umfpack.h has:
#define UMFPACK_CONTROL 20

Here is a sample usage:

int main (void)
{
double *null = (double *) NULL ;
...
(void) umfpack_di_symbolic (n, n, Ap, Ai, Ax, &Symbolic, null, null) ;
...
return (0) ;
}

My question is: Does the cast in (double *)NULL accomplish anything?
No.

It seems to me that the call to umfpack_di_symbolic() equivalent to:

umfpack_di_symbolic (n, n, Ap, Ai, Ax, &Symbolic, NULL, NULL) ;

Comments?

It's the same if umfpack_di_symbolic is actually prototyped before its
use.
If the function is not prototyped, then under C90 it will have an
implicit
declaration of...

int umfpack_di_symbolic();

The absense of a prototype would mean that NULL and (double *) NULL
might be different types and/or have different representations when
passed
as an argument. In either case, such a call would invoke undefined
behaviour.

With a prototype in scope the conversion from NULL to (double *) NULL
is implicit.

C99 removed implicit function declarations, but it did not add the
requirement
that a named function be prototyped before use.

Sensible programmers will turn on the 'require function prototypes'
option
on their compilers (if the compiler has one).
 

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

Members online

Forum statistics

Threads
474,183
Messages
2,570,964
Members
47,511
Latest member
svareza

Latest Threads

Top