In TCL 8.4, does the TclLib function Tcl_SetHashValue permit value to be NULL given #define NULL 0?

StackOverflow https://stackoverflow.com/questions/22669260

  •  22-06-2023
  •  | 
  •  

Question

I've read the documentation for Tcl_SetHashValue [1] but I have a question. Does the TclLib function Tcl_SetHashValue permit value to be NULL given #define NULL 0?

In other words, given

#define NULL 0

does a Tcl_SetHashValue invocation like the following have a defined behavior?

Tcl_SetHashValue( someHashEntry, NULL );

If the behavior is defined, what is the behavior? For example, if this code is executed

Tcl_HashTable myhash;
char *key_arg = 0;
Tcl_HashEntry *entry_arg;
ClientData val_arg;
Tcl_HashEntry *pEntry ;
char *pValue ;

Tcl_InitHashTable( &myhash, TCL_STRING_KEYS);

// store data

key_arg = "someKey";
entry_arg = Tcl_CreateHashEntry( &myhash, key_arg, &isEntryNew );
val_arg = NULL ;
Tcl_SetHashValue( entry_arg, val_arg );

// retrieve data

pEntry = Tcl_FindHashEntry( pArgs, criteria );
if (pEntry)
{
    pValue = Tcl_GetHashValue( pEntry ) ;
}

what would pEntry and pValue contain after the above code is executed?

1 - http://www.tcl.tk/man/tcl8.4/TclLib/Hash.htm

Était-ce utile?

La solution

The value set by Tcl_SetHashValue (which is a macro, for historical reasons) is cast by the macro to be of type ClientData. That in turn is a typedef for void * (except on pre-C89 systems (!) where it is a char *) and has the promise that Tcl will not poke its grubby fingers inside it; a subsequent Tcl_GetHashValue will return exactly the value that you provided.

In your sample code, pEntry will have a pointer to a valid structure or a NULL if the criteria were not matched. If pEntry is non-NULL, pValue will have the value placed in. Which might well be NULL here; NULL is a perfectly fine thing to put in, and it's up to your code to understand what that might possibly mean.


Later versions of Tcl are the same, but might have tinkered with exactly what the definitions are. And for sure we dropped pre-C89 support. (In fact, it turned out we did so for building Tcl itself in 8.4 by accident and not one user complained. We're conservative, but not that conservative!)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top