[swift-users] UnsafeMutablePointer<String> vs. UnsafeMutablePointer<Int>

Dmitri Gribenko gribozavr at gmail.com
Sat May 21 04:27:46 CDT 2016


On Sat, May 21, 2016 at 2:15 AM, Adrian Zubarev via swift-users
<swift-users at swift.org> wrote:
> So basically if I do something like this I should be on the safe side:

Yes, this code is safe.  If you just want to store a contiguous buffer
of elements of the same type, you should consider using Array.  It has
methods that will allow you to operate on the unsafe pointer to the
memory if you need that for speed, but it will do the memory
management for you.

> Does the UnsafeMutablePointer reserves me a safe portion of memory (by safe
> I mean which isn’t used by anyone at the time I will start using it)?

Yes, you will become a unique owner of the newly allocated chunk of
memory.  (Same as malloc() in C.)

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/


More information about the swift-users mailing list