On Apr 4, 2009, at 2:35 PM, Daniel Luis dos Santos wrote:
Hello,From what I know so far, memory allocated using the malloc() family of functions is freed using the free() function. Literal values such as :char *aString = "some text";are automatic values and are deallocated by the compiler automatically.
In actual fact, they are neither allocated nor deallocated. String literals are stored in a section executable itself, and the compiler just initializes the aString pointer have the address of that literal.
When I free some pointer that was allocated as in the example declaration above I get a warning that a non page-aligned, non allocated pointer is being freed.
Yup, don't do that. You're attempting to free something that was never malloc'ed.
Then in practical terms, what does a literal value such as a #define that is used to initialize pointers such as the one above serves for ?
#define is a different thing altogether. The C compiler never sees #defines; by the time the C compiler is processing that code, all macros have been evaluated and that is what the C compiler sees.
Thus, the following two things are identical as far as the C compiler is concerned.
#define STR "string" char* str = STR; --- char* str = "string";
If for example I have a group of string #defines that are used in log messages, that means that I will have to malloc space for them the sprintf them to it, so I can be sure that I don't get that warning when deallocating the log messages.
You don't have to dealloc them, ever.
when you pass as pointer to bytes (like a void*) to cocoa (for example NSData), what does it do ? It copies the bytes or just copies the pointer ?
It just copies the pointer.This is basic C (no cocoa or anything involved). I strongly recommend that you find a good C book that talks about pointers and how they work. You will save yourself years of grief and unexplained bugs if you understand pointers now; they are the most critical concept in C and if you don't understand them, you will never be a good C (C+ +,Objective-C) programmer.
-- Dave Carrigan d...@rudedog.org Seattle, WA, USA
PGP.sig
Description: This is a digitally signed message part
_______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com