TIP 494: More use of size_t in Tcl 9

Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2019 Conference, Houston/TX, US, Nov 4-8
Send your abstracts to tclconference@googlegroups.com
or submit via the online form by Sep 9.
Author:         Jan Nijtmans <jan.nijtmans@gmail.com>
State:          Final
Type:           Project
Vote:           Done
Created:        29-Dec-2017
Keywords:       tcl
Tcl-Version:    9.0
Tcl-Branch:     memory-API


This TIP describes the non-controversial part of the Tcl 9 changes: Make Tcl 9 ready for the 64-bit era.


Many Tcl API functions and struct fields have int parameters, which don't provide sufficient room on 64-bit platforms.


On 32-bit platforms, this is all 100% upwards binary compatible, provided no internal API is used (since some internal structs might have incompatible but externally invisible changes)

On 64-bit platforms, those changes cause binary incompatibility. Therefore the TCL_STUB_MAGIC value needs to change, so extensions compiled using Tcl 9 headers will not load in Tcl 8 and reverse.


Although those changes are binary compatible on 32-bit platforms, there is a small potential source incompatibility. There are 10 functions which previously had an int return argument, which is changed to size_t (for now, see below). This signed/unsigned change might lead to subtle difference in behavior.

The 10 functions are:

If the return value of such function is directly used in a compare, this could lead to the use of an unsigned compare in stead of a signed compare. If you compile your extension with -DTCL_8_COMPAT, those 10 functions will be changed to wrapper macro's which makes everything behave as if those functions return Tcl_WideInt. That's the easiest way to resolve this potential problem.

There are 2 other ways to make this change, which can do it without the use of the TCL_8_COMPAT macro:

Tk and sqlite are already converted to make full use of TIP #494. Other extensions included with Tcl (e.g. tdbc) are unaffected.

Open issues

There has been discussion, whether the return-type of these 10 functions (and possibly other functions) should actually be size_t or maybe some other type. A Tcl-specific type Tcl_Size could also be defined for this. Actually, this TIP provides 2 possibilities: Without -DTCL_8_COMPAT, the type is size_t, with -DTCL_8_COMPAT it is Tcl_WideInt. This gives enough room for experimenting which one is best, maybe it would be better to make it ssize_t or ptrdiff_t.

There - for sure - will be more future TIP's, which propose more API changes for 9.0. This one - definitely - is not the last one. So, this TIP doesn't make a final decision yet what will be the final type returned by those 10 functions.


The implementation of this TIP can be found in the memory-API branch.


This document has been placed in the public domain.