Re: [OT] What kind of data is is returned by 'new' ?
Re: [OT] What kind of data is is returned by 'new' ?
- Subject: Re: [OT] What kind of data is is returned by 'new' ?
- From: Clark Cox <email@hidden>
- Date: Sat, 9 Jul 2005 10:10:02 -0400
2005/7/8, Dirk Stegemann <email@hidden>:
> Hello Mark & AgentM,
>
> Am 07.07.2005 um 00:11 schrieb Mark Bessey:
>
>
> > all pages allocated to your process are zero-filled by the OS when
> > they're first mapped in. You can't really depend on this in any
> > useful way though - see below.
> >
> >
> >> When I allocate a buffer using
> >>
> >> char* buffer = new char [numberOfBytes];
> >>
> >> then the buffer seems to contain random data.
> >>
> >
> > This is because this memory has previously been allocated to and
> > written on by your process. The heap that "new" allocates out of is
> > re-used as you allocate and free memory.
> >
>
> Thank you for this information.
>
> Now I set up a test project (Xcode 2.1 carbon application) and put
> several calls to 'new' into the main.c file; I recognised that only
> calls to 'new' which were issued before calling any other (system)
> function returned a zero'd buffer. Afterwards, I always saw a couple
> of bytes containing non-zero values.
It is still possible that new will return a zero filled buffer if, for
instance, there is not enough memory in the currently allocated pools
to satisfy your request.
> Actually, this is great because I thought of using such buffers as
> seed to a random function. So, I'm wondering if it's posssible for
> someone hassling around with my app to circumvent this randomising
> mechanism by somehow telling the system to *always* make 'new' return
> zero'd buffers...
They can. All they have to do is write a shared library that defines
malloc() (among others), and then insert that library by launching
your app with the DYLD_INSERT_LIBRARIES environment variable set.
> e.g., in DEBUG mode compiled applications will get
> allocated memory filled with 0xCC values (on Win32 systems).
>
> Anyway, so far this seems to work fine :-)
It might work now, but you have no guarantee that it will continue to
do so. You should *never* rely on the values of uninitialized memory.
Period.
--
Clark S. Cox III
email@hidden
http://clarkcox3.livejournal.com/
http://homepage.mac.com/clarkcox3/
_______________________________________________
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