C/C++ Programmer's Guide (G06.25+)

Mixed-Language Programming for TNS Programs
HP C/C++ Programmer’s Guide for NonStop Systems429301-008
7-16
Sharing Data
This example shows how to share C data with a TAL module. The TNS C module
passes the addresses of two C arrays to a TAL routine. The TAL routine assigns the
array addresses to TAL pointers.
C Code
#include <stdioh> nolist
short arr[5]; /* C data to share with TAL */
char charr[5]; /* C data to share with TAL */
_tal void INIT_TAL_PTRS ( short _far *, char _far * )
_tal _alias ("tal^name") void C_Name (void);
void example_func( int *x )
{
printf("x before TAL = %d\n", x[2] );
C_Name( );
printf("x after TAL = %d\n", x[2] );
}
main ()
{
INIT_TAL_PTRS ( &arr[0], &charr[0] ); /* initialize ptrs */
/* test pointer values */
arr[0] = 8;
example_func( arr );
arr[2] = 18;
charr[2] = 'B';
}
TAL Code
INT .EXT tal_int_ptr; !Pointer to C data
STRING .EXT tal_char_ptr; !Pointer to C data
PROC init_tal_ptrs (c_addr1, c_addr2); !Called from C
INT .EXT c_addr1;
STRING .EXT c_addr2;
BEGIN
@tal_int_ptr := @c_addr1;
@tal_char_ptr := @c_addr2;
END;
PROC tal^name;
BEGIN
tal_int_ptr[0] := 10;
tal_int_ptr[2] := 20;
tal_char_ptr[2] := "A";
END;
Sharing TAL Data With TNS C Using BLOCK Declarations
As of the D20 release, TAL modules can share global data with TNS C modules by
declaring each shared variable in its own BLOCK declaration and giving both the
variable and the BLOCK the same name. The TNS C modules must also declare each
shared variable; the layout of the variable must match in both the TAL and C modules.