Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Mac OS X Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-26-2009, 11:13 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 148
rraagg is on a distinguished road
Default Memory allocated using malloc, does not clear data with free()

Hi All

if i allocated memory using malloc
and then place data into it,
freeing it up using free() just marks the memory as free? ie it does not clear
the data in the memory, I could still use the pointer to get the data. Is this correct?
rraagg is offline   Reply With Quote
Old 07-26-2009, 11:46 AM   #2 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
FlyingDiver will become famous soon enough
Default

Quote:
Originally Posted by rraagg View Post
Hi All

if i allocated memory using malloc
and then place data into it,
freeing it up using free() just marks the memory as free? ie it does not clear
the data in the memory, I could still use the pointer to get the data. Is this correct?
You could, maybe, until it's overwritten by something else. Not a good idea.

joe
FlyingDiver is offline   Reply With Quote
Old 07-26-2009, 11:49 AM   #3 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Originally Posted by rraagg View Post
Hi All
if i allocated memory using malloc
and then place data into it,
freeing it up using free() just marks the memory as free? ie it does not clear
the data in the memory, I could still use the pointer to get the data. Is this correct?
It's correct but dangerous. free() tells the OS that you are done with the block of memory and it may re-use it for something else. If you still have a pointer to the freed memory it may contain your data now, but your data could be erased or corrupted at any time, even in the middle of one of your statements.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 07-26-2009, 11:55 AM   #4 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 148
rraagg is on a distinguished road
Default

Thanks thats what i thought
Thanks for the confirmation mate :-)
rraagg is offline   Reply With Quote
Old 07-30-2009, 11:38 AM   #5 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 10
Sound is on a distinguished road
Default

Data is not cleared immediately on free(), so if you have sensitive data, you will need to overwrite it yourself before you free the memory.
Sound is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 472
14 members and 458 guests
alexeir, David-T, Dj_kades, foslock, iAppDeveloper, jeroenkeij, Mijator, myach, pipposanta, QuantumDoja, robsmy, sacha1996, SLIC, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,129
Posts: 402,928
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:28 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0