Re: Returning structs



Richard Heathfield wrote:
jacob navia said:

Jon Harrop wrote:
James Kuyper wrote:
Jon Harrop wrote:
Is it ok to return structs from functions or are there ABI
issues?
The C standard requires that it be supported, it's up to the
implementation to do whatever it needs to do in the context of a
give ABI to make that happen. You don't have to worry about it.
Only if I'm writing self-contained code. If I intend to expose an
API that includes functions returning structs then I need to know
this will be implemented consistently between compilers.

Under linux, there is the gcc way, and there is no other

Just follow gcc and you will be OK.

Reasonably true, I think (BICBW).

Under windows there is the Microsoft way and there is no
other...

Well, what about Borland? OMF vs COFF and all that.

Just follow MSVC under windows and you will be OK.

...unless MSVC doesn't provide what you need. It's pretty good at Win32 API but pretty dreadful at RAD, which is why a lot of people do reach for Borland, and it's at this point (after they've already got a fair amount of MSVC code) that OMF vs COFF actually matters.


OMF is an old object file format. This means, it is a standard for
storing object files, organized in a linked list of variable size
records. The CONTENTS of those records, where the interface conventions
for returning structures is defined is completely another thing.

Obviously you can't link together a .lib (static library) of Borland
with Microsoft, since the object files differ.

In theory you *could* use a DLL linked by borland using a Microsoft
compiled program if the interfaces are the same.

But the best thing is to do as the windows API does: Do not return
structures by value but use a pointer to such a structure as an
argument.

Instead of

Mystruct function(int input1,int input2);

use:

int function(Mystruct *result,int input1,int input2);

Note that now you have the problem that the aligning of structure
members is important... Microsoft uses always

#pragma pack(1)

i.e. no alignment at all and uses structure layouts that minimize
problems of alignment in members.



--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
.



Relevant Pages

  • Re: Windows graphics problem
    ... thx for ur answers. ... want a code that will work on ALL compilers inc microsoft and borland. ... jacob at jacob point remcomp point fr ...
    (comp.lang.c)
  • Re: Limited ;) marketing idea
    ... >> has agreements with Microsoft, Cisco, Oracle, etc. ... very good incentive for Borland. ... >> etc. have in a matter of months. ... >> marketing strategy has always been crap in my opinion, ...
    (borland.public.delphi.non-technical)
  • Re: RUNDLL32.EXE
    ... 0x66800000 0x155000 Windows Shell Common Dll ... 0x75FA0000 0xA000 BSD Socket API for Windows ... 0x794D0000 0x15000 Microsoft WinSock Extension APIs ...
    (comp.security.firewalls)
  • Re: My rant about the "throw out delphi and re-write it in C#" crowd.
    ... fired across the bow of the Microsoft C++ products were AWESOME. ... Borland was fearless in putting itself out there and asking developers ... Programmers traditionally defied conventional ... don't recall seeing a Delphi ad in those for years. ...
    (borland.public.delphi.non-technical)
  • Re: Explorer keep launching at start up
    ... aka "Nutcase" - Microsoft MVP ... > "Load at next startup only"=dword:00000010 ... > Filters\Export\JPEG\Filter API] ... >> Associate Expert - WindowsXP Expert Zone ...
    (microsoft.public.windowsxp.general)