views:

80

answers:

3

Hi, in Objective-C and simply speaking - am I correct when assuming

  1. that all pointer variables must be released when they're not used any more?
  2. Every pointer variable (*) is a class of some kind, or not?
  3. Every pointer variable therefore needs to be allocated and initialised using "alloc" and "init" (or similar)?
  4. When declaring variales using Object methods I may not need "alloc" or "init"?
  5. Number declarations (BOOL, int, float, etc) do not require memory management as long as they're not declared as a pointer?

Thanks for any advice helping to sort out my confusion iFloh

A: 

I believe you are correct:

  1. Yes.

  2. I guess. I'm not sure I understand the question.

  3. Kind of: as you said, it's not necessarily alloc+init, if you use certain Class methods. So, the allocation and initialization are done implicitly.

  4. Correct. (see above)

  5. I believe that it's more than numbers and that C primitives do not require memory management.

I'd recommend that you read the memory management rules from Apple as well as the Stanford class on iPhone Programming.

Timothée Boucher
1 - incorrect. 2 - It is a c-ism. 3 - correct. 4 - correct. 5 - Sort of.
bbum
Thanks for the comment! Good point on #1. It's not that I don't know that you don't release explicitly every time, it's that I took the question as "do the objects need to be released (implicitly by me or somewhere else)?" (as in with autoreleased objects). But that was a dumb way of reading the question :)
Timothée Boucher
+2  A: 

that all pointer variables must be released when they're not used any more?

Only if the "pointer variable" points to an instance of an Objective-C class and only if you caused that instance to be retained previously. Read this: Memory Management Guide.

Every pointer variable ( *) is a class of some kind, or not?

If the pointer variable is declared as being a pointer to an instance of a class, then it will point to an instance of a class. Otherwise, it won't. Pointers are exactly that; a pointer -- a reference -- to a chunk of memory. That chunk of memory might be a class, an instance of a class, a C structure, or a raw buffer (or something else).

Every pointer variable therefore needs to be allocated and initialised using "alloc" and "init" (or similar)?

Only if the pointer points to an instance of an Objective-C class. If it is a C structure, you might use malloc(). Even in the case of an Objective-C class, you might not alloc anything:

NSString *foo = [NSString stringWithFormat: @"Hello, %@", @"World!"]; NSString *bar = @"bar"; NSBundle *main = [NSBundle mainBundle];

(BTW: None of the above need a -release.)

When declaring variales using Object methods I may not need "alloc" or "init"?

This question indicates that you should go read -- and re-read (I read it about once every 18 months for the first decade I wrote Objective-C code, learning something each time) -- the Objective-C language guide.

You should probably also read something about the C language itself. That would likely help you to understand pointers vs. other types.

Number declarations (BOOL, int, float, etc) do not require memory management as long as they're not declared as a pointer?

Yes, but it is much simpler than being type specific. When you say int foo; you are telling the compiler to carve out a bit of space in the local scope -- on the stack, generally -- to store an int's worth of data. When you say int *foo;, you are telling the compiler to carve out a bit of space in the local scope to store a pointer to -- to store the address of -- a location in memory that contains an int.

Thus, NSArray *bar is just a pointer to an instance and, yes, NSArray bar;, if it were not explicitly disallowed, would carve out a chunk of space on the stack to hold an NSArray instance.

When you have a pointer to something that something must be initialized somehow and that is often done through allocation (but not always).

bbum
+2  A: 

1) that all pointer variables must be released when they're not used any more?

It depends if you "own" the pointed thing. I recommend you to carefully read these Memory Management rules to learn how to release a pointer.

2) Every pointer variable (*) is a class of some kind, or not?

Not every pointers. A pointer variable may hold a pointer to an object or to a block of memory. It is just a pointer to something.

3) Every pointer variable therefore needs to be allocated and initialised using "alloc" and "init" (or similar)?

Not every pointers. A pointer variable may hold a pointer to an object or to a block of memory. The pointed thing can be an already existing object or an allocated chunck of memory.

4) When declaring variales using Object methods I may not need "alloc" or "init"?

Not everytime. You may get a pointer to an existent object without knowing who allocated and initialized it. Again, there are some ownership rules to follow. I recommend you to carefully read these Memory Management rules.

5) Number declarations (BOOL, int, float, etc) do not require memory management as long as they're not declared as a pointer?

Yes. There are called primitive types, and as long as you manipulate them as value, you don't have to deal with their memory management.

Laurent Etiemble