I have turned of Automatic Reference Counting (ARC) in my Xcode 4.3.2, and thought I was in good hands. Then I wrote this little program:
int main()
{
int *a = new int(3);
delete a;
printf("%i",*a);
return 0;
}
Which is printing 3, but it should print garbage. It looks like the compiler takes care of all my memory allocations, which I don’t want it to do. What do I have to do to remove that?
You give your program an address on the heap with new. It happens to be an integer with the value 3 at that address.
When you free that heap area with delete, it is not immediately set to some garbage value, it still contains 3.
Your print statement goes to the address at a and tries to print the integer that is there, it finds 3 and prints it.
You could try adding compiler warnings if this is possible in xcode, I know gcc and other command line compilers offer this feature, to avoid these confusing undefined behaviors from occurring with out the program yelling at you.