Finding C array size in objective-C

  • Ok, so I have a question. I've never officially programmed in C before but I
    do have some experience in C++. Right now, I'm trying to figure out the
    command for finding the size of a C array (in my case, a one dimensional
    array). Keep in mind, I'm programming in Objective-C and I am passing a C
    array into a method/function. I realize the "count" command is for NSArray,
    so will it work for a C array also? I have one more question...if I'm using
    a NSMutableArray, can I find the size of it by calling the "count" command
    which is a NSArray command? I'm new to objective-C, so don't be to harsh
    please. Here's my example code below.

    Code:

    -(int)Majority: (int[]) posClasses

    {

    [posClasses count];

    }
  • On Jun 17, 2009, at 14:48, Joshua Bradley wrote:

    > Ok, so I have a question. I've never officially programmed in C
    > before but I
    > do have some experience in C++. Right now, I'm trying to figure out
    > the
    > command for finding the size of a C array (in my case, a one
    > dimensional
    > array).

    What you have is not "really" an array (since it's of unspecified
    size) but rather a pointer. Pointers have no array-size information at
    runtime. You must have some way to determine the size yourself, such
    as by an additional parameter or knowing a rule like "the last element
    is always 0" (e.g. C strings (char *) work this way).

    --
    Kevin Reid                                  <http://switchb.org/kpreid/>
  • On Jun 17, 2009, at 1:48 PM, Joshua Bradley wrote:

    > Ok, so I have a question. I've never officially programmed in C
    > before but I
    > do have some experience in C++. Right now, I'm trying to figure out
    > the
    > command for finding the size of a C array (in my case, a one
    > dimensional
    > array).

    There isn't one.
    A C array say an 'int a[16];' - is just the 16 ints C doesn't add any
    overhead -
    no size field, no pointer to a method dispatch table.

    > Keep in mind, I'm programming in Objective-C and I am passing a C
    > array into a method/function. I realize the "count" command is for
    > NSArray,
    > so will it work for a C array also?

    NO
    the [count ...] message requires an ObjC object, something with a
    pointer into it's ObjC class's dispatch table.

    > I have one more question...if I'm using
    > a NSMutableArray, can I find the size of it by calling the "count"
    > command
    > which is a NSArray command? I'm new to objective-C, so don't be to
    > harsh
    > please. Here's my example code below.
    >

    Yes - NSMutableArray is a subclass of NSArray,
    it takes all of it's parent's messages.

    If you're working with C arrays, you need to write down the size
    somewhere - they don't
  • google sizeof()

    http://publications.gbdirect.co.uk/c_book/chapter5/sizeof_and_malloc.html

    count works with the mutable array which is an artifact of OO, not the
    language.  Obj-C is OO, like C++, ....., think inheritance.

    -b

    On Jun 17, 2009, at 2:48 PM, Joshua Bradley wrote:

    > Ok, so I have a question. I've never officially programmed in C
    > before but I
    > do have some experience in C++. Right now, I'm trying to figure out
    > the
    > command for finding the size of a C array (in my case, a one
    > dimensional
    > array). Keep in mind, I'm programming in Objective-C and I am
    > passing a C
    > array into a method/function. I realize the "count" command is for
    > NSArray,
    > so will it work for a C array also? I have one more question...if
    > I'm using
    > a NSMutableArray, can I find the size of it by calling the "count"
    > command
    > which is a NSArray command? I'm new to objective-C, so don't be to
    > harsh
    > please. Here's my example code below.
    >
    > Code:
    >
    > -(int)Majority: (int[]) posClasses
    >
    > {
    >
    > [posClasses count];
    >
    > }
    > _______________________________________________
    > MacOSX-dev mailing list
    > <MacOSX-dev...>
    > http://www.omnigroup.com/mailman/listinfo/macosx-dev
  • On Jun 17, 2009, at 8:54 PM, Kevin Reid wrote:

    > On Jun 17, 2009, at 14:48, Joshua Bradley wrote:
    >
    >> Ok, so I have a question. I've never officially programmed in C
    >> before but I
    >> do have some experience in C++. Right now, I'm trying to figure out
    >> the
    >> command for finding the size of a C array (in my case, a one
    >> dimensional
    >> array).
    >
    > What you have is not "really" an array (since it's of unspecified
    > size) but rather a pointer. Pointers have no array-size information
    > at runtime. You must have some way to determine the size yourself,
    > such as by an additional parameter or knowing a rule like "the last
    > element is always 0" (e.g. C strings (char *) work this way).
    >
    > --
    > Kevin Reid                                  <http://switchb.org/kpreid/
    > >

    In most cases the way to pass a C-array (of any type) as an argument
    of an obj-C method is to pass both a pointer and the count. Example
    are the class factory method +[NSArray arrayWithObjects:count:] and +
    [NSColor colorWithColorSpace:components:count:].

    The idea is that the caller of the method generally knows the count,
    because it either created the C-array or it got it passed together
    with the count. I couldn't think of an exception to that.

    Apart from this, using C-arrays in obj-C is very rare. Generally you
    shouldn't use it, except perhaps as a local variable. And in that case
    you just know the count. And if you think you need to pass a C-array,
    I advice you to rethink. So you're probably just asking an irrelevant
    question.

    Last but not least, when you begin with obj-C you should read an
    introduction, there are some really good books (e.g. Hillegass), and
    also Apple's documentation and sample code can help you getting
    started if you don't want to spend money (though it's always worth
    using a book). I'd certainly advice you to read about the basics of
    obj-C and Cocoa, as well as programming style in obj-C. For example if
    you don't understand what's wrong with your method name -Majority: you
    need to do some reading.

    Christiaan
  • Am 17.06.2009 um 22:56 schrieb Christiaan Hofman:

    > Last but not least, when you begin with obj-C you should read an
    > introduction,

    Absolutely. Doing a toy app is almost a must to get behind how
    convenient Cocoa works. The day or two you need for this is well
    invested.

    Cocoa Dev Central has quite a bunch of tutorials, for beginners as
    well as on key technologies.

    <http://www.cocoadevcentral.com/>

    Markus

    - - - - - - - - - - - - - - - - - - -
    Dipl. Ing. Markus Hitter
    http://www.jump-ing.de/
previous month june 2009 next month
MTWTFSS
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          
Go to today