Intel® Software Network Knowledge Base Wiki


Constructing Nav Tree
One Moment...

(refresh menu)



 
Welcome, Guest | Quick Login | Register

Develop for Core processor


Use Explicit Prototyping

Version 5, Changed by LINDA SWINK on 3/21/2008
Created by: KYLEX.S.LEWIS@INTEL.COM

Challenge

Avoid limitations due to the requirement in ANSI C that parameters be passed as either intor doubleif functions are not protoyped. Passing parameters as either int or double might be incorrect for your actual code logic.

The following sample code does not contain explicit prototyping:


//modulel.c<br />
extern int intitialize_buffer();<br />
void main(void)<br />
{<br />
char*pd1=malloc(1000);<br />
char*p2=malloc(2000);<br />
int size=p2-p1;<br />
initialize_buffer(size)<br />
}<br />
//module2.c<br />
char * global_buffer;<br />
int initialize_buffer( SIZE_T size)<br />
}<br />
global_buffer=(char*) malloc(size);<br />
if (global_buffer==NULL) return(-1);<br />
return(0);<br />
}


Solution

Explicitly prototype all functions; this guideline applies to all programming platforms. Explicit prototyping also provides these benefits:


It ensures that the caller and the callee have matching argument types, because the compiler will evaluate the types and generate warnings for mismatched types.
It protects against parameter misuse that can lead to parameter corruption.
The compiler can more efficiently pass parameters to called functions.
Note that you should explicitly prototype a function in a header file to save time. The following sample code is a revised version of the code given in the "Challenge" section that illustrates prototyping:


01 //modulel.c<br />
02 extern int intitialize_buffer(SIZE_T);<br />
03<br />
04 void main(void)<br />
05 {<br />
06 char*pd1=malloc(1000);<br />
07 char*p2=malloc(2000);<br />
08 SIZE_T size=p2-p1;<br />
09 initialize_buffer(size)<br />
10 }<br />
11 //module2.c<br />
12 char * global_buffer;<br />
13 int initialize_buffer( SIZE_T size)<br />
14 }<br />
15 global_buffer=(char*) malloc(size);<br />
16 if (global_buffer==NULL) return(-1);<br />
17 return(0);<br />
18 }

At line 2, the prototype explicitly specifies the kind of parameters to be received by the function. In the code sample given in the "Challenge" section, the size defaults to int, but in the code for 64-bit Intel architecture, it is a scalable type, SIZE_T, which ensures that the code can be easily compiled for either 32-bit or 64- bit architectures.

At line 8, size is specified as type int in the code without explicit prototyping. The int declaration limits the difference between two pointers to no greater than 232. This limit becomes a problem when the code is compiled for the 64-bit Intel® architecture, because values greater than the limit will be truncated when passed to size. In the revised code, a scalable type, SIZE_T, is used to ensure that the size of the difference between the pointers adapts to the architecture. When compiled for the 64-bit Intel architecture, the difference between pointers can be up to 264, ensuring that the upper 32 bits won't be lost.

At line 13, the callee expects a SIZE_T parameter, which is a 64-bit value when compiled for Windows 2000 (64-bits)*. Since the int variable in the code sample given in the "Challenge" section supplies a value only for the low order 32 bits; the upper 32 bits contain unknown information. In the revised code, the callee expects and receives a 64-bit value. When compiled for the 32-bit Intel architecture, SIZE_T will ensure that the callee receives a properly sized value.

Considerations for explicit prototyping in floating-point functions are addressed in a separate item, "How to Use Explicit Prototyping for Floating-Point Functions."

Source

Preparing Code for the IA-64 Architecture (Code Clean)



Served
23 Knowledge Bases
604 Pages
Search
Powering Up Search...


Vote on this Page

Tags For This Page
Loading Tags..

Tag This



Additional legal information