[GRASS-dev] Re: testing native winGRASS

Glynn Clements glynn at gclements.plus.com
Mon Mar 19 22:26:49 EDT 2007


Moritz Lennert wrote:

> First novice attempt attached.

> char *G_nulldevice (void) 
> {
> 
>   static char name[11];
> 
> #ifdef __MINGW32__
> 
>    strcpy(name, "NUL:");
> 
> #else
> 
>    strcpy(name, "/dev/null");
> 
> #endif
> 
>    return name;
> }

No need to make a copy:

	const char *G_nulldevice (void) 
	{
	#ifdef __MINGW32__
	   return "NUL:";
	#else
	   return "/dev/null";
	#endif
	}

Which reminds me of another outstanding clean-up task: library
functions should use "const" where appropriate.

As it stands, functions which take a non-const pointer but which don't
actually modify the value are so common that a lot of application code
tends to just assume that the missing "const" is an oversight and that
the value won't actually be modified.

Then it gets bitten by when the function really does modify the value
(G_find_cell() is a common culprit, as it strips off any @mapset
suffix from the map name).

We need to get into the habit of using "const" wherever possible, so
that the lack of it indicates that the string (or other value) really
will be modified (rather than simply indicating that someone forgot
the "const").

This needs to happen from the bottom up, as once a function declares a
parameter as "const", it can't pass it to a function which requires a
non-const parameter (using a type cast can hide a real problem, in the
case where the function really does modify the argument).

-- 
Glynn Clements <glynn at gclements.plus.com>




More information about the grass-dev mailing list