• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
malloc was optimized out
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

malloc was optimized out


  • Subject: malloc was optimized out
  • From: Dmitry Markman <email@hidden>
  • Date: Mon, 04 Jul 2016 14:09:02 -0400

Hi all

in the Xcode 7.3.1 the following simple code

#include <iostream>

int main(int argc, const char * argv[]) {
    size_t need_size = 0x1000000000000;

    void *data = malloc(need_size);

    if(data == NULL) {
        std::cout << "data == NULL" << std::endl;
        return 1;
    } else {
        std::cout << "data != NULL" << std::endl;
    }
    free(data);

    return 0;
}

reports for the release build

data != NULL
Program ended with exit code: 0


which is clearly wrong, because right answer would be

data == NULL
Program ended with exit code: 1


g++ behaves correctly

recently I found out that we reported that problem to Apple

and we got an answer

""Engineering has determined that this issue behaves as intended based on the following information:

The compiler "knows" how malloc works, and is allowed to optimize as if it never fails.

We are now closing this bug report.”

clearly in my situation clang knows nothing :((

clang can’t optimize malloc here, because result of the malloc is used.

we have much more complex use case where

malloc was optimized out,

I just simplified the code

Note: -fno-builtin flag solve the problem


also if I use

        std::cout << "data != NULL" << data << std::endl;

malloc wasn’t optimized



Dmitry Markman


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: malloc was optimized out
      • From: Clark Cox <email@hidden>
    • Re: malloc was optimized out
      • From: Jens Alfke <email@hidden>
    • Re: malloc was optimized out
      • From: Quincey Morris <email@hidden>
  • Prev by Date: Re: Open Xcode and agree to the license agreement
  • Next by Date: Re: malloc was optimized out
  • Previous by thread: Ctrl-drag question
  • Next by thread: Re: malloc was optimized out
  • Index(es):
    • Date
    • Thread