HP OpenVMS Systems Documentation

Content starts here

Run-Time Library Reference Manual for OpenVMS Systems

Previous Contents Index Example---Difference Between Stream Mode and Record Mode

Example 1-1 demonstrates the difference between stream mode and record mode access.

Example 1-1 Differences Between Stream Mode and Record Mode Access

/*      CHAP_1_STREAM_RECORD.C                       */

/* This program demonstrates the difference between  */
/* record mode and stream mode input/output.         */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void process_records(const char *fspec, FILE * fp);

    FILE *fp;

    fp = fopen("example-fixed.dat", "w", "rfm=fix", "mrs=40", "rat=none");
    if (fp == NULL) {
    printf("Record mode\n");
    process_records("example-fixed.dat", fp);

    printf("\nStream mode\n");
    fp = fopen("example-streamlf.dat", "w");
    if (fp == NULL) {
    process_records("example-streamlf.dat", fp);

void process_records(const char *fspec, FILE * fp)
    int i,

    char buffer[40];

    /* Write records of all 1's, all 2's and all 3's */
    for (i = 0; i < 3; i++) {
        memset(buffer, '1' + i, 40);
        sts = fwrite(buffer, 40, 1, fp);
        if (sts != 1) {

    /* Rewind the file and write 10 characters of A's, then 10 B's, */
    /* then 10 C's.                                                 */
    /*                                                              */
    /* For stream mode, each fwrite call outputs 10 characters      */
    /* and advances the file position 10 characters                 */
    /* characters.                                                  */
    /*                                                              */
    /* For record mode, each fwrite merges the 10 characters  into  */
    /* the existing 40-character record, updates the record and     */
    /* advances the file position 40 characters to the next record. */
    for (i = 0; i < 3; i++) {
        memset(buffer, 'A' + i, 10);
        sts = fwrite(buffer, 10, 1, fp);
        if (sts != 1) {

    /* Now reopen the file and output the records. */

    fp = fopen(fspec, "r");
    for (i = 0; i < 3; i++) {
        sts = fread(buffer, 40, 1, fp);
        if (sts != 1)
            printf("%.40s\n", buffer);


Running this program produces the following output:

Record Mode

Stream mode

1.9 Specific Portability Concerns

One of the last tasks in preparing to use the HP C RTL, if you are going to port your source programs across systems, is to be aware of specific differences between the HP C RTL and the run-time libraries of other implementations of the C language. This section describes some of the problems that you might encounter when porting programs to and from an OpenVMS system. Although portability is closely tied to the implementation of the HP C RTL, this section also contains information on the portability of other HP C for OpenVMS constructs.

The HP C RTL provides ANSI C defined library functions as well as many commonly available APIs and a few OpenVMS extensions. See Section 1.5 for specific standards, portions of which are implemented by the HP C RTL. Attempts have been made to maintain complete portability in functionality whenever possible. Many of the Standard I/O and UNIX I/O functions and macros contained in the HP C RTL are functionally equivalent to those of other implementations.

The RTL function and macro descriptions elaborate on issues presented in this section and describe concerns not documented here.

The following list documents issues of concern if you wish to port C programs to the OpenVMS environment:

  • HP C for OpenVMS Systems does not implement the global symbols end , edata , and etext .
  • There are differences in how OpenVMS and UNIX systems lay out virtual memory. In some UNIX systems, the address space between 0 and the break address is accessible to your program. In OpenVMS systems, the first page of memory is not accessible.
    For example, if a program tries to reference location 0 on an OpenVMS system, a hardware error (ACCVIO) is returned and the program terminates abnormally. OpenVMS systems reserve the first page of address space to catch incorrect pointer references, such as a reference to a location pointed to by a null pointer. For this reason, some existing programs that run on some UNIX systems may fail and you should modify them, as necessary. (Tru64 UNIX and OpenVMS, however, are compatible in this regard.)
  • Some C programmers code all external declarations in #include files. Then, specific declarations that require initialization are redeclared in the relevant module. This practice causes the HP C compiler to issue a warning message about multiply declared variables in the same compilation. One way to avoid this warning is to make the redeclared symbols extern variables in the #include files.
  • HP C does not support asm calls on OpenVMS VAX and I64 systems. They are supported on OpenVMS Alpha systems. See the HP C User's Guide for OpenVMS Systems for more information on intrinsic functions.
  • Some C programs call the counted string functions strcmpn and strcpyn . These names are not used by HP C for OpenVMS Systems. Instead, you can define macros that expand the strcmpn and strcpyn names into the equivalent, ANSI-compliant names strncmp and strncpy .
  • The HP C for OpenVMS compiler does not support the following initialization form:

    int  foo  123;

    Programs using this form of initialization must be changed.
  • HP C for OpenVMS Systems predefines several compile-time macros such as __vax , __alpha , __ia64 __32BITS , __vms , __vaxc , __VMS_VER , __DECC_VER , __D_FLOAT , __G_FLOAT , __IEEE_FLOAT , __X_FLOAT , and others. These predefined macros are useful for programs that must be compatible on other machines and operating systems. For more information, see the predefined macro chapter of the HP C User's Guide for OpenVMS Systems.
  • The ANSI C language does not guarantee any memory order for the variables in a declaration. For example:

    int  a, b, c;
  • Depending on the type of external linkage requested, extern variables in a program may be treated differently using HP C on OpenVMS systems than they would on UNIX systems. See the HP C User's Guide for OpenVMS Systems for more information.
  • The dollar sign ($) is a legal character in HP C for OpenVMS identifiers, and can be used as the first character.
  • The ANSI C language does not define any order for evaluating expressions in function parameter lists or for many kinds of expressions. The way in which different C compilers evaluate an expression is only important when the expression has side effects. Consider the following examples:

    a[i]  =  i++;

    x = func_y() + func_z();

    f(p++, p++)

    Neither HP C nor any other C compiler can guarantee that such expressions evaluate in the same order on all C compilers.
  • The size of a HP C variable of type int is 32 bits on OpenVMS systems. You will have to modify programs that are written for other machines and that assume a different size for a variable of type int . A variable of type long is the same size (32 bits) as a variable of type int .
  • The C language defines structure alignment to be dependent on the machine for which the compiler is designed. On OpenVMS VAX systems, HP C aligns structure members on byte boundaries, unless #pragma member_alignment is specified. On OpenVMS Alpha systems, HP C aligns structure members on natural boundaries, unless #pragma nomember_alignment is specified. Other implementations may align structure members differently.
  • References to structure members in HP C cannot be vague. For more information, see the HP C Language Reference Manual.
  • Registers are allocated based upon how often a variable is used, but the register keyword gives the compiler a strong hint that you want to place a particular variable into a register. Whenever possible, the variable is placed into a register. Any scalar variable with the storage class auto or register can be allocated to a register as long as the variable's address is not taken with the ampersand operator (&) and it is not a member of a structure or union.

1.9.1 Reentrancy

The HP C RTL supports an improved and enhanced reentrancy. The following types of reentrancy are supported:

  • AST reentrancy uses the _BBSSI built-in function to perform simple locking around critical sections of RTL code, but it may also disable asynchronous system traps (ASTs) in locked regions of code. This type of locking should be used when AST code contains calls to HP C RTL I/O routines.
    Failure to specify AST reentrancy might cause I/O routines to fail, setting errno to EALREADY.
  • MULTITHREAD reentrancy is designed to be used in threaded programs such as those that use the POSIX threads library. It performs threads locking and never disables ASTs. The POSIX Threads library must be available on your system to use this form of reentrancy.
  • TOLERANT reentrancy uses the _BBSSI built-in function to perform simple locking around critical sections of RTL code, but ASTs are not disabled. This type of locking should be used when ASTs are used and must be delivered immediately.
  • NONE gives optimal performance in the HP C RTL, but does absolutely no locking around critical sections of RTL code. It should only be used in a single-threaded environment when there is no chance that the thread of execution will be interrupted by an AST that would call the HP C RTL.

For nonthreaded processes, the default reentrancy type is TOLERANT. When the threads library is loaded, the reentracy level is implicitly set to C$C_MULTITHREAD, and the application cannot change it after that.

You can set the reentrancy type by compiling with the /REENTRANCY command-line qualifier or by calling the decc$set_reentrancy function. This function must be called exclusively from non-AST level.

When programming an application using multiple threads or ASTs, consider three classes of functions:

  • Functions with no internal data
  • Functions with thread-local internal data
  • Functions with processwide internal data

Most functions have no internal data at all. For these functions, synchronization is necessary only if the parameter is used by the application in multiple threads or in both AST and non-AST contexts. For example, although the strcat function is ordinarily safe, the following is an example of unsafe usage:

extern char buffer[100];
void routine1(char *data) {
    strcat( buffer, data );

If routine1 executed concurrently in multiple threads, or if routine1 is interrupted by an AST routine that calls it, the results of the strcat call are unpredictable.

The second class of functions are those that have thread-local static data. Typically, these are routines in the library that return a string where the application is not permitted to free the storage for the string. These routines are thread-safe but not AST-reentrant. This means they can safely be called concurrently, and each thread will have its own copy of the data. They cannot be called from AST routines if it is possible that the same routine was executing in non-AST context. The routines in this class are:

asctime        stat
ctermid        strerror
ctime          strtok
cuserid        VAXC$ESTABLISH
gmtime         the errno variable
localtime      wcstok

All the socket functions are also included in this list if the TCP/IP product in use is thread-safe.

The third class of functions are those that affect processwide data. These functions are neither thread-safe nor AST-reentrant. For example, sigsetmask establishes the processwide signal mask. Consider a routine like the following:

void update_data
    int old_mask;

    old_mask = sigsetmask( 1 << (SIGINT - 1));
        /* Do work here that should not be aborted. */
    sigsetmask( old_mask );

If update_database was called concurrently in multiple threads, thread 1 might unblock SIGINT while thread 2 was still performing work that should not be aborted.

The routines in this class are:

  • All the signal routines
  • All the exec routines
  • The exit , _exit , nice , system , wait , getitimer , setitimer , and setlocale routines.


Generally speaking, UTC-based time functions can affect in-memory time-zone information, which is processwide data. However, if the system time zone remains the same during the execution of the application (which is the common case) and the cache of time-zone files is enabled (which is the default), then the _r variant of the time functions asctime_r , ctime_r , gmtime_r and localtime_r is both thread-safe and AST-reentrant.

If, however, the system time zone can change during the execution of the application or the cache of time-zone files is not enabled, then both variants of the UTC-based time functions belong to the third class of functions, which are neither thread-safe nor AST-reentrant.

Some functions remain inherently nonthread-safe regardless of the reentrancy type. They are:

execl      exit
execle     _exit
execlp     nice
execv      system
execve     vfork

1.9.2 Multithread Restrictions

Mixing the multithread programming model and the OpenVMS AST programming model in the same application is not recommended. The application has no mechanism to control which thread gets interrupted by an AST. This can result in a resource deadlock if the thread holds a resource that is also needed by the AST routine. The following functions use mutexes. To avoid a potential resource deadlock, do not call them from AST functions in a multithreaded application.

  • All the I/O functions
  • All the socket functions
  • All the signal functions
  • vfork , exec , wait , system
  • catgets
  • set_new_handler (C++ only)
  • getenv
  • rand and srand
  • exit and _exit
  • clock
  • nice
  • times
  • ctime , localtime , asctime , mktime

1.10 64-Bit Pointer Support (ALPHA, I64)

This section is for application developers who need to use 64-bit virtual memory addressing on OpenVMS Alpha Version 7.0 or higher.

OpenVMS Alpha 64-bit virtual addressing support makes the 64-bit virtual address space defined by the Alpha architecture available to both the OpenVMS operating system and its users. It also allows per-process virtual addressing for accessing dynamically mapped data beyond traditional 32-bit limits.

The HP C Run-Time Library on OpenVMS Alpha Version 7.0 systems and higher includes the following features in support of 64-bit pointers:

  • Guaranteed binary and source compatibility of existing programs
  • No impact on applications that are not modified to exploit 64-bit support
  • Enhanced memory allocation routines that allocate 64-bit memory
  • Widened function parameters to accommodate 64-bit pointers
  • Dual implementations of functions that need to know the pointer size used by the caller
  • New information available to the DEC C Version 5.2 compiler or higher to seamlessly call the correct implementation
  • Ability to explicitly call either the 32-bit or 64-bit form of functions for applications that mix pointer sizes
  • A single shareable image for use by 32-bit and 64-bit applications

1.10.1 Using the HP C Run-Time Library

The HP C Run-Time library on OpenVMS Alpha Version 7.0 systems and higher can generate and accept 64-bit pointers. Functions that require a second interface to be used with 64-bit pointers reside in the same object libraries and shareable images as their 32-bit counterparts. No new object libraries or shareable images are introduced. Using 64-bit pointers does not require changes to your link command or link options files.

The HP C 64-bit environment allows an application to use both 32-bit and 64-bit addresses. For more information about how to manipulate pointer sizes, see the /POINTER_SIZE qualifier and #pragma pointer_size and #pragma required_pointer_size preprocessor directives in the HP C User's Guide for OpenVMS Systems.

The /POINTER_SIZE qualifier requires you to specify a value of 32 or 64. This value is used as the default pointer size within the compilation unit. You can compile one set of modules using 32-bit pointers and another set using 64-bit pointers. Care must be taken when these two separate groups of modules call each other.

Use of the /POINTER_SIZE qualifier also influences the processing of HP C RTL header files. For those functions that have a 32-bit and 64-bit implementation, specifying /POINTER_SIZE enables function prototypes to access both functions, regardless of the actual value supplied to the qualifier. In addition, the value specified to the qualifier determines the default implementation to call during that compilation unit.

The #pragma pointer_size and #pragma required_pointer_size preprocessor directives can be used to change the pointer size in effect within a compilation unit. You can default pointers to 32-bit pointers and then declare specific pointers within the module as 64-bit pointers. You would also need to specifically call the _malloc64 form of malloc to obtain memory from the 64-bit memory area.

1.10.2 Obtaining 64-Bit Pointers to Memory

The HP C RTL has many functions that return pointers to newly allocated memory. In each of these functions, the application owns the memory pointed to and is responsible for freeing that memory.

Functions that allocate memory are:


Each of these functions have a 32-bit and a 64-bit implementation. When the /POINTER_SIZE qualifier is used, the following functions can also be called:

_malloc32 , _malloc64
_calloc32 , _calloc64
_realloc32 , _realloc64
_strdup32 , _strdup64

When /POINTER_SIZE=32 is specified, all malloc calls default to _malloc32 .

When /POINTER_SIZE=64 is specified, all malloc calls default to _malloc64 .

Regardless of whether the application calls a 32-bit or 64-bit memory allocation routine, there is still a single free function. This function accepts either pointer size.

Be aware that the memory allocation functions are the only ones that return pointers to 64-bit memory. All HP C RTL structure pointers returned to the calling application (such as a FILE, WINDOW, or DIR) are always 32-bit pointers. This allows both 32-bit and 64-bit callers to pass these structure pointers within the application.

Previous Next Contents Index