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.

52 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!!

  21. افضل مكنة لغسل السجاد says:

    افضل مكنة لغسل السجاد

  22. Amerika Vizesi says:

    Amerika Vizesi

  23. İngiltere Vizesi says:

    İngiltere Vizesi

  24. Hollanda Vizesi says:

    Hollanda Vizesi

  25. Dissertation Writing Service says:

    Things are very open and intensely clear explanation of issues. was truly information. Your website is very beneficial.

  26. Dissertation Writing Help says:

    We also share some information about our business

  27. Law Assignment 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.

  28. Matlab Assignment Help says:

    This is really great work. Thank you for sharing such a good and useful information here in the blog for students.

  29. 500-701 question answer says:

    Awesome blog. I would love to see true life prepared to walk, so please share more informative updates. Great work keeps it up.

  30. Jessica Simpson Vest Dukes Of Hazzard says:

    I agree that the media is pressing this disparity between the average woman and the. think eating disorders are the only danger here but also what this media trend does to.

  31. Sons Of Anarchy Vest says:

    I agree that the term is thrown around quite freely and I know. a racist is equally as bad as someone assuming that I support Al Sharpton just because I m black. I hate.

  32. Sophie Miller says:

    tell tractorsupply.com
    I thank you for the information! I was looking for and could not find. You helped me!

  33. see page says:

    Thanks a lot for posting this

  34. see here says:

    This is very interssting

  35. Finance Assignment Help Online says:

    I Appreciate This Work Amazing Post For Us I Like It.

  36. steven3 says:

    I am agree with this article

  37. Buy Harvard Business Case Studies Solutions says:

    Good way of telling, good post to take facts regarding my presentation subject matter, which i am going to deliver in my college

  38. Case study solutions says:

    Great Information,it has lot for stuff which is informative.I will share the post with my friends.

  39. Help With Computer Science Project says:

    I personally like your post, you have shared good article. It will help me in great deal.

  40. Do My Dissertation for Me says:

    I thank you for the data! I looked and couldn't discover. You helped me!

  41. Kevin says:

    Thank you for solving this issue - it's really helped my drainage unblocking business.

  42. Dissertation help says:

    I loved the post, keep posting interesting posts. I will be a regular reader…I am offering dissertation help to students in UK at low price.

  43. Sophie Miller says:

    Je vous remercie de l'information! Je cherchais et ne pouvait pas trouver. Vous me aidé!moneypak customer service phone number

  44. Sultan Faris says:

    Very good and interesting, I appreciate your work.Tutuapp APK

  45. My Assignment Help says:

    Thank you very much for sharing this very useful information, I am very happy to have found this information that I have been looking for this long time. We also provide blog my assignment help Australia.

  46. Tutuapp says:

    Great information Technology is a better always.

  47. Online Shopping says:

    with him. It has to be very open and treated as a discussion, not an accusation sexual intimacy together so you should be able to talk about things too right? You two should not try.

  48. Leather Coat says:

    I personally like classical music For example Beethoven was so emotive. famous ninth symphony That s not the style of music you seem to appear to like at this point, but you did ask for what other.

  49. Modapk Town says:
  50. special event wishes says:
  51. CDR Report says:

    I found this is an informative and interesting blog so I think so it is very useful and knowledgeable. I would like to thank you for the efforts you have made in writing this blog. We also provide blog CDR Report Australia.

  52. activate windows error says:

    Very Interesting and wonderful information keep sharing this post kindly checkactivate windows error

Leave a Reply