Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 400133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:54:53+00:00 2026-05-12T16:54:53+00:00

In the course of this discussion about casting the return value of malloc many

  • 0

In the course of this discussion about casting the return value of malloc many people have claimed that the implicit declaration of malloc would cause the return value to be converted to int then reconverted back to T* possibly resulting in truncation of the pointer in situations where:

sizeof(int) < sizeof(void*)

This would imply that the compiler does the following:

  1. Links to and calls the correct object code defining malloc
  2. Generates object code to convert the return value to the shorter int type
  3. Generates object code to convert back to the larger destination pointer type

Could someone actually prove that this happens? Say with some example code on 64bit Linux?

I’d do it myself, but I don’t have access to a 64 bit machine.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:54:53+00:00Added an answer on May 12, 2026 at 4:54 pm

    The problem with your description of what happens is in step 2. With an implicit declaration, the code at the calling site doesn’t “convert” the return value of the function, really.

    What happens is that the code at the calling site extracts the return value (typically from a register, or off the stack) by assuming that it’s of type “int”. The procedure to do this is different for different OSes and compilers, and is typically specified by an ABI document.

    For the most common ABIs, the return location and sizes of int and void* are the same, so you actually won’t have any problem doing this, even though it’s incorrect. This is true for Linux, Windows, and Mac OS X on both 32- and 64-bit platforms, I believe 32-bit platforms.

    On 64-bit platforms, it’s more common for “long” and “void *” to be the same size, so if you have an implicit declaration for malloc(), the return value will be truncated. There are several popular 64-bit programming models, though.

    Back in the “good old days” of DOS development, it was possible to create programs that ran in a mode where “int” was 16 bits, and pointers were 32 bits (actually, 24). In those cases, calling malloc() with an implicit prototype would truncate the returned value.

    Note that even in the cases where the return value is truncated, you still might not have a runtime problem, depending on the whether the value is actually outside the valid range of an int.


    On Mac OS X, in 64-bit mode, this code:

    #include <stdio.h>
    
    int main (int argc, const char * argv[]) {
        int x = malloc(128);
        void *p = malloc(128);
        printf("Hello, World!\nsizeof(int)=%d,sizeof(void*)=%d,x=0x%xd,p=%p\n", sizeof(int), sizeof(void *), x, p);
        return 0;
    }
    

    prints:

    Hello, World!
    sizeof(int)=4,sizeof(void*)=8,x=0x1001c0d,p=0x100100240

    Note that the “x” value has fewer digits than the “p” value, having silently dropped the most-significant 32 bits of the value. The actual assembly code at the two calls to malloc looks like this:

    LM2:
        movl    $128, %edi
        call    _malloc
        movl    %eax, -12(%rbp)
    LM3:
        movl    $128, %edi
        call    _malloc
        movq    %rax, -8(%rbp)
    

    So, the right value is being returned by malloc (in %rax), but the movl instruction truncates it as it’s being moved into variable “x”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that looks like this for about ~30 students: StudentID Course*
There is this way, of course: OuterClass.this . But that's very clumsy. Ideally, there'd
In a German programming forum we currently have a discussion about events and what
So, this question may be a little vague, but I have constant discussions about
In the course of a discussion in chat, I wrote this console application. Code:
As an exercise for my Java course in Uni this morning, I had to
Of course we can do this, but is it alright to do so? Are
(Came up with this question in the course of trying to answer this other
Of course when developing IE is causing me headaches. This page with rounded corner
This is the code (Euclid's algorithm for GCD). Of course there is Prelude.gcd but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.