Home arrow C++ arrow Page 2 - Function Pointers, part 3
C++

Function Pointers, part 3


In previous articles, Jun Nakamura introduced using regular function pointers, C++ class member function pointers, and declaring pointers to the members of classes. In this article, he writes about calling conventions, callback functions, and begins to talk about using functors.

Author Info:
By: J. Nakamura
Rating: 5 stars5 stars5 stars5 stars5 stars / 15
June 07, 2005
TABLE OF CONTENTS:
  1. · Function Pointers, part 3
  2. · Windows Calling Conventions
  3. · Application of Callback Functions
  4. · Functors Encapsulate Function Pointers

print this article
SEARCH DEVARTICLES

Function Pointers, part 3 - Windows Calling Conventions
(Page 2 of 4 )

Alright, here is an overview of how different some Windows calling conventions can be (__pascal, __fortran and __syscall are no longer supported):

__cdecl
Argument Passing: right to left
Stack Maintenance: Calling function pops arguments from the stack
Name Decoration (C only): ‘_’ prefixed to function names (e.g. ‘_foo’)

__stdcall
Argument Passing: right to left
Stack Maintenance: Called function pops its own arguments from the stack
Name Decoration (C only): ‘_’ prefixed to function name, ‘@’ appended followed by the number of decimal bytes in the argument list. (e.g. ‘_foo@10’)

__fastcall (applies to Intel CPUs, this is the default calling convention for Borland)
Argument Passing: First two DWORD arguments are passed in ECX and EDX, the rest is passed right to left
Stack Maintenance: Called function pops its own arguments from the stack
Name Decoration (C only): ‘@’ is prefixed to the name, ‘@’ appended followed by the number decimal bytes in the argument list. (e.g. ‘@foo@10’)

thiscall (used automatically by C++ code)
Argument Passing: ‘this’ pointer put in ECX, arguments passed right to left
Stack Maintenance: Calling function pops arguments from the stack
Name Decoration: None

You see that there are many ways leading to Rome, and this is just on a Windows machine. What is interesting for this article though, is how we declare a function pointer to the following function definition:

void __stdcall Convention(int value, char const *string)
{
  (void)printf(“%d, ‘%s’\n”, value, string);
}

Again, the answer here is simple; we simply integrate the calling convention we need into the function pointer declaration:

void (__stdcall *ConvPtr)(int, char const*) = &Convention;

The principle remains the same whether it is a member function, a function argument, etc. Just bolt what you need into the declaration.

When your linker is giving you errors that it cannot resolve external symbols while you know that you are linking with the correct libraries; be sure to check which calling conventions are being used.


blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials