• 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
Re: malloc was optimized out
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: malloc was optimized out


  • Subject: Re: malloc was optimized out
  • From: Dmitry Markman <email@hidden>
  • Date: Mon, 04 Jul 2016 16:37:44 -0400

thanks Carl

yes I saw that in my  experiments

compiler think that

printf("OKAY data != NULL: %p\n",data)

is usage of data


but

if(data == NULL)   is not


and therefore malloc was optimized out


I submitted bug report to clang and one of the comment mentioned very useful thread:

https://groups.google.com/forum/#!topic/llvm-dev/lV30rcmF0ss


by malloc documentation malloc should set error code (and I’m talking about Apple supplied documentation)

and clearly setting error code is side effect


I’d understand that code like

data = malloc(0x100000000000);
free data;

is optimized out, but if code checks data for anything or check data[n] then it can’t be optimized out

dm





> On Jul 4, 2016, at 4:20 PM, Carl Hoefs <email@hidden> wrote:
>
>
>> On Jul 4, 2016, at 12:58 PM, Clark Cox <email@hidden> wrote:
>>
>> Malloc effectively *never* returns NULL.
>
> It does seem that malloc returns NULL on error...
>
> #include <stdlib.h>
> #include <stdio.h>
>
> int main(int argc, const char * argv[]) {
>   size_t need_size = 0x1000000000000;
>
>   char *data = "dummy";      // data ptr is not NULL
>   data = malloc(need_size);  // data ptr overwritten
>
>   if(data == NULL) {
>       printf("ERROR data == %p\n",data);  // <----- data is NULL
>       return 1;
>   } else {
>       printf("OKAY data != NULL: %p\n",data);
>   }
>   data[0] = 'c';
>
>   free(data);
>
>   return 0;
> }
>
> mtest(3008,0x7fff786d1300) malloc: *** mach_vm_map(size=281474976710656) failed (error code=3)
> *** error: can't allocate region
> *** set a breakpoint in malloc_error_break to debug
> ERROR data == 0x0
>
>

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


References: 
 >malloc was optimized out (From: Dmitry Markman <email@hidden>)
 >Re: malloc was optimized out (From: Clark Cox <email@hidden>)
 >Re: malloc was optimized out (From: Carl Hoefs <email@hidden>)

  • Prev by Date: Re: malloc was optimized out
  • Next by Date: Re: malloc was optimized out
  • Previous by thread: Re: malloc was optimized out
  • Next by thread: Re: malloc was optimized out
  • Index(es):
    • Date
    • Thread