Re: malloc was optimized out
Re: malloc was optimized out
- Subject: Re: malloc was optimized out
- From: Jens Alfke <email@hidden>
- Date: Mon, 04 Jul 2016 23:27:46 -0700
On Jul 4, 2016, at 7:11 PM, Dmitry Markman < email@hidden> wrote:
I’m asking for 0x1000000000000 or 281474976710656B which is ~250TB on my MacBook Pro with 1TB SSD and 16GB RAM and for release build it doesn’t return NULL
No, in the release build the compiler optimized away the malloc call entirely.
malloc itself has no idea whether your app is “release” or “debug” or whatever. It’s a system call and it behaves the same way for all processes.
What you’re seeing is that the compiler has some knowledge of how malloc behaves, and decided that your code effectively didn’t do anything so it just optimized it all away. This is a common problem when trying to write benchmarks, too. If you want to find the actual behavior, you need to make your code a bit more complex so the compiler realizes you’re using the actual result of malloc. (I think this has already been explained multiple times in this thread so far.)
If you absolutely need to preflight a huge memory allocation, I suggest creating a temporary file, resizing it to the desired size by setting its EOF, then using mmap to map the file into address space. (You should then unlink the file, ensuring it’ll be cleaned up when your process exits.)
—Jens |
_______________________________________________
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