aleix's blog

32-bit and 64-bit return values in OS X

25 April 2013 2:01 am (c | mac os x)

I've spent a couple of days with a silly compiler problem. I have to admit that it was my fault but, at least, I learned something in the process. To make a long story short: do not forget to include header files and enable all warnings (-Wall).

In C, you can use a function without including its header file. Then, depending on how you set the compiler flags, it might not complain as long as it can find the function at linking time.

Imagine you have this function (defined in foo.h and implemented in foo.c) :

unsigned long foo_add (void);

and you use it in another module bar.c without including the foo.h header:

//#include "foo.h"

unsigned long bar_add (void)
{
  return foo_add () + foo_add ();
}

Compile with: gcc -c -O2 bar.c (we just missed -Wall)

If we disassemble the code of bar.o we get:

$ otool -tV bar.o
bar.o:
(__TEXT,__text) section
_bar_add:
...
000000000000000b callq _foo_add
0000000000000010 movl  %eax, %ebx
...
0000000000000024 ret

Now, let's try to include the header foo.h and disassemble again:

$ otool -tV bar.o
bar.o:
(__TEXT,__text) section
_bar_add:
...
0000000000000009 callq _foo_add
000000000000000e movq  %rax, %rbx
...
000000000000001f ret

Note how the compiler uses two different instructions: movl (32-bit) in the first case, when it really doesn't know anything about the foo_add function, and movq (64-bit) when it knows that foo_add returns a 64-bit value.

This can lead to unexpected behavior as I have found in the code I was working on. And actually, I have only found this in OS X.

So, please do not forget to include header files and enable -Wall. It will save you from some headaches.

20 responses

  1. thesis writing service UK says:

    This is personally related with the task of sizes and arrangements to programming-dialect sorts. Another firmly related point is name disfiguring, which decides how image names in the code guide to image names utilized by the linker. Calling traditions, sort representations, and name ravaging are all part of what is known as an application twofold interface.
    There are frequently unobtrusive contrasts in how different compilers execute these traditions, so it is regularly hard to interface code which is gathered by various compilers. Then again, traditions which are utilized as an API standard, are consistently actualized.

  2. Pay Someone To Write My Essay says:

    VBA formerly did not have an indicator data style and because of this, developers used 32-bit variables to stock up pointers and lever.

  3. john mayer says:

    This is personally related with the task of sizes and arrangements to programming-dialect sorts. Another firmly related point is name disfiguring, which decides how image names in the code guide to image names utilized by the linker. Calling traditions, sort representations, and name ravaging are all part of what is known as an application twofold interface.

  4. myessayslab says:

    The header file should have some exceptional tips on how making the appropriate document.

  5. JohnWilliam says:

    Specifically, this wave to can likewise enable the track to edge cases in which a progression of lawful changes results in an illicit transformation. For instance, with this banner, the compiler will issue a notice on the off chance that you allocate a pointer to a 64-bit number, pass that pointer into a 32-bit work contention, and in this manner change over the write my dissertation uk work result once more into a pointer.

  6. Ella says:

    At itpcexpert.com can see how to made safe files transfer.

  7. Alvina Amanda says:

    Well i like your blog but till i don't understand that what is return values in OS X because i don't know about this blog Write My Essay Online share me this site for the learning and i'm trying my best to learn.

  8. sophiegooch says:

    i am so admire for this submit due to the fact you are sharing your printer enjoy on this blog i get extra records on your post you've got greater problems on printers so sharing it. I would like to distribute one stripe at the beginning of my individual understanding and appropriate paintings, keep it up thanks. Superman Smallville Jacket

  9. Online Coursework Help UK says:

    pecifically, this wave to can similarly empower the track to edge cases in which a movement of legal changes brings about an unlawful change. For example, with this standard, the compiler will issue a notice in case you distribute a pointer to a 64-bit number, pass that pointer into a 32-bit work dispute, and in this way change over the Online Coursework Help UK work result yet again into a pointer.

  10. stevewaugh says:

    This is by and by related with the undertaking of sizes and game plans to programming-vernacular sorts. Another solidly related point is name deforming, which chooses how picture names in the code manual for picture names used by the linker. Calling conventions, Write my Coursework UK sort portrayals, and name assaulting are all piece of what is known as an application twofold interface.

  11. Pay Someone to Do my Coursework says:

    There are every now and again inconspicuous complexities in how extraordinary compilers execute these conventions, so it is consistently difficult to interface code which is assembled by different compilers. Of course, customs which are used as an API standard, are reliably realized. Pay Someone to Do my Coursework

  12. UK Coursework Writing Service says:

    VBA in the past did not have a marker information style and along these lines, engineers utilized 32-bit factors to stock up pointers and lever.UK Coursework Writing Service

  13. Login says:

    I could not solve my problem for a very long time, but your article helped me a lot. Thank you192.168.0.1

  14. lukenlow says:

    This is just a perfect example of an ideal article, just like this one should be all the manuals on this site 192.168.1.1

  15. https://aquaticorlando.com/ says:

    I loved the way you discuss the topic great work thanks for the share Your informative post.

  16. https://casestudyaid.com says:

    Such a nice post, keep providing good resources.

  17. panistefanin says:

    Thank you for your work on the blog! You're doing a good job!
    I would like to read about it anymore. Prompt, what literature to study?simple mobile live person customer service

  18. Orlando Basement Water Damage says:

    I appreciate your efforts in preparing this post. I really like your blog articles.

  19. Managerial Accounting Thesis Help says:

    The leading assignment help UK firm offers state of the art services to its clients with a promise of delivering all the required work well within the deadline.

  20. Assignment help in Australia says:

    Hi buddy, your blog' s design is simple and clean and i like it. Your blog posts about Assignment help in Australia Online Dissertation Help are superb. Please keep them coming. Greets!!

Leave a Reply