Standard Library Functions (STDLIB.H)

Chapter 6 - Working with Data

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

pascal, cdecl, near, far, and huge Modifiers
The first two modifiers, pascal and cdecl, are used most frequently in advanced applications. Microsoft Visual C/C++ allows you to write programs that can easily call other routines written in different languages. The opposite of this also holds true. For example, you can write a Pascal program that calls a C++ routine. When you mix languages this way, you have to take two very important issues into consideration: identifier names and the way parameters are passed.
When Microsoft Visual C/C++ compiles your program, it places all of the program’s global identifiers (functions and variables) into the resulting object code file for linking purposes. By default, the compiler saves those identifiers using the same case in which they were defined (uppercase, lowercase, or mixed). Additionally, the compiler appends an underscore (_) to the front of the identifier. Since Microsoft Visual C/C++’s integrated linking (by default) is case sensitive, any external identifiers you declare in your program are also assumed to be in the same form with a prepended underscore and the same spelling and case as defined.
pascal
The Pascal language uses a different calling sequence than C and C++. Pascal (along with FORTRAN) passes function arguments from left to right and does not allow variable-length argument lists. In Pascal, it is also the called function’s responsibility to remove the arguments from the stack, rather than having the invoking function do so when control returns from the invoked function.
A C and C++ program can generate this calling sequence in one of two ways. First, it can use the compile-time switch /Gc, which makes the Pascal calling sequence the default for all enclosed calls and function definitions. Second, the C program can override the default C calling sequence explicitly by using the pascal keyword in the function definition.
As mentioned earlier, when C generates a function call, by default it appends an underscore to the function name and declares the function as external. It also preserves the casing of the name. However, when the pascal keyword is used, the underscore is not prepended and the identifier (function or variable) is converted to all uppercase.
The following code segment demonstrates how to use the pascal keyword on a function. (The same keyword can be used to ensure FORTRAN code compatibility.)
float pascal pfcalculate(int iscore, int iweight)
{
  .
  .
  .
}
Of course, variables can also be given a Pascal convention, as seen in this next example:
#define TABLESIZE 30

float pascal pfcalculate(int iscore, int iweight)
{
  .
  .
  .
}

float pascal pfscore_table[TABLESIZE];

int main( )
{
 int iscore 95, iweight = 10;
   
 pfscore_table[0] = pfcalculate(iscore,iweight);
 
 return(0);
}
In this example, pfscore_table has been globally defined with the pascal modifier. Function main( ) also shows how to make an external reference to a pascal function type. Since both functions, main( ) and pfcalculate( ), are in the same source file, the function pfcalculate( ) is global to main( ).
cdecl
If the /Gz compile-time switch was used to compile your C or C++ program, all function and variable references were generated matching the Pascal calling convention. However, there may be occasions when you want to guarantee that certain identifiers you are using in your program remain case sensitive and keep the underscore at the front. This is most often the case for identifiers being used in another C file.
To maintain this C compatibility (preserving the case and having a leading underscore prepended), you can use the cdecl keyword. When the cdecl keyword is used in front of a function, it also affects how the parameters are passed.
Note that all C and C++ functions prototyped in the header files of Microsoft Visual C/C++—for example, STDIO.H—are of type cdecl. This ensures that you can link with the library routines, even when you are compiling using the /Gz option. The following example was compiled using the /Gz option and shows how you would rewrite the previous example to maintain C compatibility:
#define TABLESIZE 30

float cdecl cfcalculate(int iscore, int iweight)
{
  .
  .
  .
}

float cdecl cfscore_table[TABLESIZE];

int main( )
{
 int iscore 95, iweight = 10;
   
 cfscore_table[0] = cfcalculate(iscore,iweight);
 
 return(0);
}
near, far, and huge
  Note These old-style C/C++ keywords are only mentioned here in case you run across them in an older text or program. They are 16-bit C/C++ compiler specific and are no longer needed by today’s state-of-the-art 32-bit C/C++ compilers.
However, this is an appropriate time to caution you. C and C++ are continuing to evolve, even as you read this text. Therefore, any time you peruse other C/C++ literature, beware: there are Historic C, C, ANSI C, Historic C++, C++, ANSI C++, and the flavor-of-the-month languages out there! Case in point: the old-style keywords near, far, and huge.
(You use the three modifiers—near, far, and huge—to affect the action of the indirection operator (*); in other words, they modify pointer sizes to data objects. A near pointer is only 2 bytes long, a far pointer is 4 bytes long, and a huge pointer is also 4 bytes long. The difference between the far pointer and the huge pointer is that the latter has to deal with the form of the address.)

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net