PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 4, 2022

[FIXED] How can a reference be present in a signature of a function callable from C code?

 July 04, 2022     abi, c, c++, cdecl, pass-by-reference     No comments   

Issue

I'm a bit confused: I have a C++ API which is supposed to be called from C code and uses __cdecl in the function declarations.

There's a vtable with function pointers like this:

void (__cdecl *funptr) (const MyStruct& obj);

references are a C++ construct, are they not? How can there be a __cdecl with references?

And finally: is __cdecl equivalent to wrapping everything in an extern "C" statement? What about references in that case?

I'm a bit confused..


Solution

These are apples and oranges.

__cdecl is a non-standard keyword used to describe one of the more common x86 ABI calling conventions (together with __stdcall) which specifies how variables are passed/stacked between caller and callee. It has nothing to do with C specifically - some historic Microsoft C compiler just used this calling convention, hence the name. Many programming languages can use this calling convention and similarly, C code doesn't have to use it.

extern "C" just means that the code should be compiled "like C" by the C++ compiler, disabling various name mangling etc used internally by the C++ compiler. It's not necessarily related to compliant C, but could as well be used when sharing code between two different C++ compilers that may use different name mangling.

Neither has anything to do with how references work. In C they will not compile, in C++ they will compile. The code you posted is C++.



Answered By - Lundin
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing