Sign In/My Account | View Cart  

advertisement

AddThis Social Bookmark Button

Article:
  Memory Management in Objective-C
Subject:   (Q) interchangeable pointers and objects
Date:   2001-07-30 20:30:20
From:   psheldon
Toward the end of the last page, Mike Beam writes "Convenience constructors also make it less cumbersome to nest messages". I noted something puzzling here that I also vaguely remember from another object oriented language, Apple's object oriented pascal course I took long ago.
Note above this sentence on nesting that the variable string points to a NSString, the star prefix on string is saying the thing pointed to by the variable string is an NSString type. The method setStringValue accepts this pointer. But, when we collapse things into a nest, it accepts, not a pointer to, but rather the NSString type itself.
I might windily say that "the compiler is smart enough to forgive" or "the language is not context free in interpretation", but I think there is something deeper going on that makes all object oriented languages "forgive" this way, that this is "a feature and not a bug", a feature I might better exploit if I don't simply rationalize away with these windy words, but put into some sort of new rules for my writing code, rules to not merely rationalize what I have seen but also predict and help me write code I haven't seen.
Perhaps there is an introductory remark somewhere, something toward these rules, so I will more fearlessly let the compiler forgive me and not fuss with such things.
Full Threads Oldest First

Showing messages 1 through 10 of 10.

  • Object Semantics
    2001-07-31 07:29:27  canyonrat [View]

    I think it's dangerous to generalize here. You just have to accept that ObjC is an OO language that uses pointer semantics and move on. Object Pascal also uses pointer semantics but Java uses reference semantics and C++ uses copy semantics.

    All these languages are dealing with the same question: "How do I return an object from a method that will remain valid when the method goes out of scope?"

    C++ takes the most straightforward approach and copies the object. There is no behind the scenes conversion from myType to myType*. This puts the burden on the programmer of defining a copy constructor for all but the most trivial classes. It also can really bog a program down in copying.

    Java attempts to solve the problem with a universal garbage collector. At least in 1.2 it didn't really work and for a serious program the programmer had to manage memory "by hand." It may be fixed now.

    ObjC does return a myType* and asks the programmer to autorelease it or to make it with a convenience constructor that shifts memory management duties to the class object.
    • Object Semantics
      2001-07-31 10:42:35  psheldon [View]

      I am enjoying Cocoa introduced to me by Mike Beam. I thank you for your responses to my question.

      To show kindness and gratitude, I think I should stick my neck out actively responding to your responses and try to tell how they helped me focus.

      I shall give you three prefatory examples :
      Once I knew a computer science major who constantly asked the question is this a pass by reference or pass by value. It seemed to him a fundamental question that would clarify his understanding of code. What was the meaning or semantics of that old equal sign.
      In optical holography I saw two images called equal if they had transmission fields that were proportional. Once I knew there was a semantics to the symbol equal that was held in the "folklore of the subject", things became a whole lot easier to read and write.
      Maple allows assignment of expressions to variables and has complex rules of whether to take final evaluations or intermediate evaluations for procedures that I yet need to learn to use profitably. Not that I don't think this complexity useful, rather I don't yet know how to be useful with it.

      Some more introductory remarks to try to tell that I am coming from somewhere safe:
      When you have gotten to the point of thinking in a language, things aren't explicitly written down in any publication or textbook, but rather in this "folklore of the subject". The guys who write the insides of a language or compiler know stuff users don't and probably in a folklorish sort of way. They are not ready to share this, either because it will scare off users or just because it hasn't gotten "that perfect" yet.
      The semantic debates between language and compiler designers might, in fact, be blessedly hidden. Nonetheless, these debates happened. The big boys argued, each thought he won and the lesser of us might hide in one big boys house or the others. Whether the hiders or the big boys have ahold of the greater truth might be questioned. One can learn a lot playing a game for a little while rather than merely practicing running things into the ground with high level debate. There are also more choices of how to be than these two; I can choose to believe this.

      My search for threads to make things coherent :

      You (CanyonRat) mentioned "pointer semantics" which told me the words in that formal presentation that I wanted. That was very reassuring.

      So, I sherlock 2 searched those words and sped read a bunch of threads :
      http://www.rt.mipt.ru/docs/oop/chap12/study.htm
      and reassuringly found my question, "What does it mean to say a programming language uses pointer semantics for assignment? ", a study question for a text on general object oriented programming.

      Then, I sherlock 2'd again :
      http://vip.cs.utsa.edu/classes/cs4773s97/classes/cs4773.topic20.html
      "There are three possible interpretations of assignment
      ...
      Pointer semantics: Assignment changes the reference of the left side to be the right side.
      Also called pointer assignment
      The two variables not only have the same value but also refer to the same object. "

      All that reassures me from this statement is that the meaning (semantics) of the equal sign in an assignment varies in different languages.

      Now, from my thread experience, I attempt to stretch my imagination (do the homework, pay my dues) to try to see pointer semantics might mean equal sign semantics (meaning).

      Suppose there were a language that only passed or assigned by pointer semantics, then the compiler might look to see if a parameter were already a pointer or autoconvert to a pointer for assignments within a method (I believe what you call "behind the scenes conversion from myType to myType*."). I saw some debate in these threads about whether autoconversion was a good thing or could have you pointing to something that wasn't there and this was debate about language design involving tradeoffs of release pools vs other ways of freeing memory (beyond my caliber). That it was debated indicated that it was considered.
      Thence, I believe, you write "You just have to accept that ObjC is an OO language that uses pointer semantics and move on". Indeed, I don't wish to debate semantics or which language is best I am not of that caliber. I want to get just a "feel" of the axioms of Cocoa, but can't demand axiomatic definition or be pleased if someone gave it to me (I probably would get confused). Right now I can want to go on and code and dirty my hands. Indeed, as Justin Armstrong wrote, "...spend a while looking at it, ObjC takes some getting used to, but is in fact entirely sensible!" (well I'm not sensible, yet, and wish I could get more sensible as painlessly as possible).
      ;-)

      I am sorry that I don't have the axiomatic base or feel to write tersely as you do. For example, your "All these languages are dealing with the same question: "How do I return an object from a method that will remain valid when the method goes out of scope?"" Languages are candidate solutions to my question, oh boy. Have I asked a question whose answer is beyond my caliber to appreciate?
      I think somewhere in the book objective C is a caveat that there is no EBNF or grammar representation for objective C yet, it's in the "folklore" we are sharing now and you guys are sharing a more distilled version than I.

      Thank you, I feel a little wiser but am still hungry as I am not entirely sensible, yet.
      ;-)
      I have learned that when I do homework things always get more confused before they get simpler. I don't know whether I should have done homework in open forum because of this, yet I felt I should aknowledge response publically and gratefully.
      I hope you see I am human and don't bite.
      Maybe someone will be able to say the threads I found above in a more distilled fashion than I did and I shall be happier.

      • Where is the folklore
        2001-08-01 20:54:55  canyonrat [View]

        ObjC hasn't really been used by enough people for long enough to have its folklore published yet. That's OK. The books that capture the spirit and the conventions of any development system are distilled from news groups and mailing lists. You can always go to the source.

        Stroustrup suggests that if you are seriously using any language, you should subscribe to at least two news groups/mailing lists dedicated to it. I think that's good advice.

        For ObjC the best candidates seem to be:
        cocoa-dev@lists.apple.com
        and
        macosx-dev@omnigroup.com

        You can learn a lot just lurking. But do ask questions. The folks are friendly and helpful.
        • Where is the folklore
          2001-08-02 14:21:24  psheldon [View]

          Thank you. I am very curious about these candidates, especially omnigroup's. They were involved in os x translations of the game quake and have uncommented game frameworks.

          I really like the newsgroup thread structure so that I would not appear intrusive and also could choose what to read . Do these have thread structure ?
          From superficially looking at them being e mail addresses I write to, I am sorry that cocoa-dev@lists.apple.com and macosx-dev@omnigroup.com don't seem to have this thread structure.
          Perhaps, however, I write these e mail addresses to join the list ? Yet I might like to see that the lists supply a thread structure.

          Do e mails there work like yahoo e groups and appear on a web page like this forum with threads.

          Do they put my on some sort of local-computer group list for my news reader (newswatcher, incidentally).

          Looking at my isp's group list, I searched objective and found objective c.
          comp.lang.objective-c
          This was the only newswatcher group I found that seemed relevant.
          • cocoa-dev with browser-nomail option
            2001-08-02 21:24:14  psheldon [View]

            In the FAQ about the list that the e mail address, cocoa-dev@lists.apple.com, writes to, Apple indicates you can have a nomail option.
            http://www.lists.apple.com/cgi-bin/mwf/topic_show.pl?id=46
            Apple's list further offers other less intrusive mail options such as receive digest once a day rather than individual mails.
            FAQ also indicated people actually didn't want to use newsservers :
            http://www.lists.apple.com/cgi-bin/mwf/topic_show.pl?id=47

            Apple writes :
            http://www.lists.apple.com/mailman/listinfo/cocoa-dev
            "The archives are not currently searchable. We're working on adding a search engine to the archives, but don't yet have a date that it'll be functional. "
            so the list only has "human memory" now.

            I am ever curious about Apple's knowledge navigation improvements.
          • Where is the folklore
            2001-08-02 20:46:40  canyonrat [View]

            I should have given the web address of those lists. Cocoa-dev is at:
            http://lists.apple.com/mailman/listinfo/cocoa-dev
            macosx-dev is at:
            http://www.omnigroup.com/community/developer/mailinglists/macosx-dev/

            Threading is really done in the reader and it's easier to thread a news group than a mailing lit so many mailers don't thread. I tend to use Entourage because it threads both news groups and mailing lists.

            If you don't have a threading mail program, both of the above lists let you read threads on the web.

            comp.lang.objective-c has some good discussions but it's more oriented to people porting and extending the ObjC language and not just to Mac programmers.
            • thanks, checked omnigroup (nomail?)
              2001-08-02 21:40:48  psheldon [View]

              Interesting that you can have a threading mail program, but more interesting is that the list can observe threads in archives through a browser.
              Since I couldn't find the FAQ, I want to ask confirmation does omnigroup have a nomail option as well?
              • thanks, checked omnigroup (nomail?)
                2001-08-03 07:29:38  canyonrat [View]

                I think you can do this by subscribing and then turning on the "nomail" option. That may disable your ability to send to the list, though.
  • (Q) interchangeable pointers and objects
    2001-07-31 07:23:03  justinarmstrong [View]

    is this the line that is confusing you?

    [textField setStringValue:[NSString stringWithCString:"Hello"]];

    remember how the square brackets show
    the nesting of message sends.

    the first message sent is 'stringWithCString:'
    which goes to the NSString *class* object.
    A class object is a special kind of object
    that knows how to create other objects,
    in this case instances of NSString.
    So, as a result of this stringWithCString
    message is a new NSString instance that
    in this case contains the text "Hello".

    This new string is then passed as an
    argument to the setStringValue: message
    which is sent to a textField.

    there isn't really any voodoo compiler
    stuff going on behind your back.
    "NSString" is just the name of a global
    object that happens to be a class object.

    if you look at
    NSString* string = [NSString stringWithCString:"hello"];

    this creates a variable called 'string'
    which is a pointer to an instance of
    'NSString'. This pointer is then initialized
    to a new instance of the class NSString.

    spend a while looking at it, ObjC takes
    some getting used to, but is in fact entirely
    sensible!


    • (Q) interchangeable pointers and objects
      2003-12-09 17:35:27  anonymous2 [View]

      If you want to gain a better understanding of the relationship between a class and its instances, and find out what is happening "behind the scenes," I suggest you read the article below.

      http://www.macdevcenter.com/lpt/a/2432

      It takes careful reading, but is very valuable. Keep in mind that all objective-c objects are manipulated as pointers to an object that exists somewhere else in memory--the id type is implemented as a pointer to a class or instance. Classes are also objects. This is why a message can be sent to a class, as in the NSString initialization example. In [NSString stringWithCString:"hello"], NSString is actually a pointer to the object representing the NSString class, not to the NSString type. (I conjecture that the NSString type is actually not a type but a struct--structs behave somewhat like a typedef under certain circumstances). Maybe I've clarified things, or maybe I've just written an incomprehensible bunch of jargon. I can't tell. If this helps, good. If not, ignore it.