Professional Web Applications Themes

why *foo and not just foo? - Mac Programming

This is probably the easiest question someone here can answer - but it's something I'm very confused about. I'm working my way through "Cocoa Programming with Mac OS X" and along with my little knowledge, I don't understand what exactly the * does when declaring objects. For example, when declaring an Integer I do: int foo; However, with NSMutableArray: NSMutableArray *array; Can someone please explain what the * does and when it is, and isn't needed and why. Really appreciate any answer :) Thanks....

  1. #1

    Default why *foo and not just foo?

    This is probably the easiest question someone here can answer - but it's
    something I'm very confused about.

    I'm working my way through "Cocoa Programming with Mac OS X" and along with
    my little knowledge, I don't understand what exactly the * does when
    declaring objects.

    For example, when declaring an Integer I do:

    int foo;

    However, with NSMutableArray:

    NSMutableArray *array;

    Can someone please explain what the * does and when it is, and isn't needed
    and why.

    Really appreciate any answer :)

    Thanks.
    Dave Guest

  2. #2

    Default Re: why *foo and not just foo?

    In article <2004020422432916807%davewintercommanderbondnet >,
    Dave Winter <net> wrote:
     

    It means 'pointer to'.

    So

    char *foo;

    Declares a pointer to a block of memory for an char, identified by foo.

    Pointers are used in C to deal with things that are allocated at runtime
    or that are passed between functions where you do not want to have more
    than one copy.

    Let's say for example that you will need a character buffer sometimes in
    the program and do not know for sure how large it needs to be until you
    execute.

    One way is to declare it larger than you figure will ever be needed for
    your application:

    #define MYBUFSIZE 2000
    ....
    char myBuf[MYBUFSIZE];

    The problem with that is that eventually it is almost guaranteed that
    someone will need that extra byte for their 2001 byte long bit of text
    and you are dead.

    Alternatively you could:

    char *myBuf = NULL;
    ....
    /* Now I have the size I need in needBufSize */
    if (myBuf != NULL) {
    myBuf = (char *)realloc(myBuf, needBufSize);
    } else {
    myBuf = (char *)malloc(needBufSize);
    }
    if (myBuf == NULL) {
    /* Deal with this as an error, you have run out of memory,
    * start looking for memory leaks */
    ...
    }

    A lot more code, but it prevents a lot of possible problems.
    Shawn Guest

  3. #3

    Default Re: why *foo and not just foo?

    On Wed, 4 Feb 2004 22:43:29 +0000, Dave Winter wrote: 
     

    This is only a little bit about Cocoa, and mostly about the C language.
    A good textbook on C programming will help. The Cocoa manuals assume
    that you already know C.
     
     

    You are also allowed to say "int *foo;", but it has a different meaning.
    The "*" means you are declaring a pointer variable. Literally, it says
    that "*foo", the object pointed to by foo, is of type int. Before you
    can do anything with *foo, though, you have to allocate memory for it.
    You also have to deallocate memory when you are done with it. This is
    called dynamic allocation. The C language uses malloc/free, but Cocoa
    uses something quite different, which I won't try to describe here.
     
     

    Right. That's a Cocoa object type, a descendant of NSObject.
     

    With ordinary built-in types you are allowed to use either static
    allocation (int foo;) or dynamic allocation (int *foo;).

    With Cocoa objects such as NSMutableArray, you don't have a choice;
    dynamic allocation is the only kind that's allowed.


    --
    Dave Seaman
    Judge Yohn's mistakes revealed in Mumia Abu-Jamal ruling.
    <http://www.commoncouragepress.com/index.cfm?action=book&bookid=228>
    Dave Guest

  4. #4

    Default Re: why *foo and not just foo?

    In article <2004020422432916807%davewintercommanderbondnet >, Dave
    Winter <net> wrote:
     

    Others have answered the question directly but the fact that you ask
    the question suggests you need to get a good book on C and learn the
    basic language as this is not anything to do with Cocoa or Objective C
    but rather is standard C. Remember that Objective C is a superset of C
    and you really should be reasonably proficient in C.

    Spence

    --
    James P. Spencer
    Rochester, MN

    "Badges?? We don't need no stinkin badges!"
    James Guest

  5. #5

    Default Re: why *foo and not just foo?

    In article <040220042007057264%net>,
    James Spencer <net> wrote:
     [/ref]

    Apart from what the others have said (that you'll want to read a good
    book on C), let me mention that memory management is a topic many people
    have problems with. After teaching a couple of people, I decided to
    write a little article on memory management. Basically, it attempts to
    show you how it basically works underneath all the pretty C APIs, which
    helps a lot in comprehending why pointers work that way. You can find it
    at:

    http://www.zathras.de/articles/How_Memory_Management_Works.php

    Any feedback on ways to improve this, or making this more comprehensible
    would be appreciated.

    Cheers,
    -- M. Uli Kusterer
    http://www.zathras.de
    Uli Guest

  6. #6

    Default Re: why *foo and not just foo?

    In article <mdli.com>,
    Shawn Lavin <com> wrote:
     

    By the way, many people prefer to write this as:

    char* foo;

    to make it clear that the type is "char*" and the name is "foo".

    meeroh

    --
    If this message helped you, consider buying an item
    from my wish list: <http://web.meeroh.org/wishlist>

    Miro Guest

  7. #7

    Default Re: why *foo and not just foo?

    Hi Miro,

     [/ref]

    Miro Jurisic <org> writes: 

    It should be noted though, that C always reads it in the first way
    anyway, as in

    char *foo, *baz, *boom;

    which declares three pointers all with the same type "char *". Which
    is of course why some people prefer the first version.

    For this reason the more important recommendation is probably not to
    declare more than one pointer variable per declaration statement.


    benny
    Benjamin Guest

  8. #8

    Default Re: why *foo and not just foo?

    In article <benny.turtle-trading.net>,
    Benjamin Riefenstahl <de> wrote:
     [/ref]
    >
    > Miro Jurisic <org> writes: 
    >
    > It should be noted though, that C always reads it in the first way
    > anyway, as in
    >
    > char *foo, *baz, *boom;[/ref]

    Yeah, the same people who prefer char* foo prefer one declaration per statement
    :-)
     

    I actually think it's best not to declare more than one of any kind of variable
    per statement. YMMV.

    meeroh

    --
    If this message helped you, consider buying an item
    from my wish list: <http://web.meeroh.org/wishlist>

    Miro Guest

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139