Re: malloc was optimized out
Re: malloc was optimized out
- Subject: Re: malloc was optimized out
- From: Dmitry Markman <email@hidden>
- Date: Mon, 04 Jul 2016 16:48:23 -0400
another observation
the following code
char *data = (char *)malloc(need_size);
if(data == NULL) {
printf("data == NULL\n");
return 1;
} else {
for(size_t i = 0; i < need_size; ++i) {
#if 0
size_t c = i % 64;
c += 63;
data[i] = c;
#else
data[i] = 'A';
#endif
}
printf("data != NULL <<%c>>\n", data[0x10000]);
}
free(data);
returns
data != NULL <<A>>
Program ended with exit code: 0
but if you replace #if 0 to #if 1
it started to behave correctly (data = NULL)
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