PDA

View Full Version : Code Terminology...




larswik
Jun 20, 2011, 04:04 AM
Stumbling my way through Class's and Objects. The Lynda Objective C is nice to also learn from besides the book. I had a question about this code sample.
NSString *word = @"Laura";
NSLog(@"Hello, %@", word);

I did not define any char variables (I know you don't need a char array working with strings) for this code to hold this string. The video said NSString is the Class. *word is the Pointer to the Object holding the "Laura" String.

Like 'int num = 50;' num is the container (4 bytes of memory) storing the value 50. Is 'word' the pointer and the container holding this string value, or is the 'Laura' string just saved someplace in memory and the 'word' Pointer is used to set it and get it in that memory address?

I am just trying to see how the nuts and bolts are working here, a better understanding. In my Pascal class we did not deal with Turbo Pascal, just Pascal. Strings were in Turbo Pascal and the teacher did not like them. Hello char arrays and for loops.

Thanks!

-Lars



jiminaus
Jun 20, 2011, 05:42 AM
"abc" is fundamentally different from @"abc".

"abc" is C string, as you were used to dealing with in C. Its type is const char*. It's a pointer to an array the compiler will create containing the 3 characters 'a', 'b' and 'c' followed the null character '\0'.

@"abc" is an NSString object. It's type is NSString*. It's a pointer to a NSString object initialised by the compiler to contain the 3 characters 'a', 'b' and 'c'.

The variable word in your code snippet is a block memory big enough to hold a pointer to an NSString object. It will be 4 bytes when compiling 32-bit code and 8 bytes when compiling 64-bit code. The variable word can only hold an address. The address will be where the object actually exists (actually the address of it's first byte, the other bytes will follow in subsequent addresses). It cannot hold the object itself.

So in the code NSString* word = @"Laura", the NSString @"Laura" exists somewhere else, and word just has a pointer to it.

This is always the case in Objective-C. A variable can never hold an object directly. It will always hold a pointer to an object that exists somewhere else.

The following code is a compiler error. Notice the lack of an asterisk.

NSString str; // Compiler error


It might useful to actually try removing the asterisk in your code snippet and see what the compiler error is. You can be assured that you will make this mistake at some point. So it'll be good to learn what the compiler error means and how you fix it.

BTW Speaking of error message. Switch over to the LLVM compiler instead of GCC so you get more understandable error messages.

lee1210
Jun 20, 2011, 05:43 AM
Have you learned pointers from any source? Do you know what is stored in a pointer variable? Do you know "where" in the memory model Objective-C objects are created?

The @"" syntax confuses things slightly here, but as far as you're concerned this is just like any other NSString. Behind the scenes some Unicode characters (not chars) are stored. How is irrelevant to you. You interact with the NSString via messages, never worrying about the actual storage of the character data.

*word stores an address. The NSString object is being stored in the memory starting at that address. What is actually stored there is opaque. You don't know how the data is actually stored. Whatever it is is not stored in *word, only the address is stored there.

-Lee

larswik
Jun 20, 2011, 04:39 PM
Thanks Guys,

Jim - What you wrote explained a lot! So the 'word' is the variable that just stores the pointer that points the the memory location that my object is stored in. I don't care where it is stored, I just care about the pointer, 'word' in this case. Objects are always accessed via pointers stored in variables. But can I also say this, 'int number = 10;'. In this case, even in objective C the variable number stores the value of 10, and not a pointer to the number 10.

That I TOTALLY understand, thank you again for the very detailed.

Lee - Yes I understand the concept of Pointers and Addresses. The C book I read last year explained these very well. 'Scanf("%C", &myVar); The &myVar is the address location to where my char will be stored. Pointers allow me to access the contents of that memory address. If memory serves me right, in learning C, I used pointers from within the scope of functions to swap out values in variables. I would pass in the address as an argument which allowed me swap out the value store in that address for that Var.

I read about it, but have not put it into my practice much. Learning to program while have a have a full time job doing something else is tough. Sometimes it's weeks before I can start again and then trying to pick up again is hard. After I finished the C book it would have been nice to find another book that would be entitled "I just learned C, now how do I use it?" :)

Again I can not think you guys enough for all the support. If you ever get up to Santa Ynez California I would be very happy to buy you lunch to say thanks!

-Lars

PatrickCocoa
Jun 20, 2011, 04:58 PM
I must applaud jiminaus for his great answer.

One small clarification. He says "A variable can never hold an object directly". This is true. Since you understand what he's saying you can probably not read the rest of what I say.

But there are some non-object items that can be used in Objective-C. For example for integers you can define a primitive:
int joe = 3;

In this case the variable "joe" contains 3, not a pointer to someplace that has 3 stored in it.

larswik
Jun 20, 2011, 06:09 PM
Thanks for point that out. I did in my reply make mention of that with the example, 'int number = 10;'. This would be treated like normal old C code that I learned?

But to just dig a little further into this pile of knowledge. Why can't you store an object in a variable? I am taking a wild guess here but is it because a variable, once defined, is a fixed size and an object can be dynamic in size through out the life of it? That's a wild guess :)

A variable is stored in memory and so is an object, they are just treated differently.

Thanks,

-Lars

Cromulent
Jun 20, 2011, 06:30 PM
But to just dig a little further into this pile of knowledge. Why can't you store an object in a variable?

Because objects are dynamically allocated on the heap. Some languages (namely C++) allow you to create class objects on the stack or on the heap but Objective-C is not one of them.

When the object is created a pointer is returned so that you can access the area of memory that contains the object. This is exactly the same as the malloc() function in C that you should have learnt about when you were doing your C programming.