Memory management of C arrays in objects

  • If I have a class that contains several large C arrays, do I have to
    free them in a dealloc method in the class, or do they get freed
    automatically when the object is freed?  If not, what's the right
    syntax for freeing a C array?

    Thanks!

    - Jason
  • On Dec 16, 2007, at 22:05, Jason Horn wrote:

    > If I have a class that contains several large C arrays, do I have
    > to free them in a dealloc method in the class, or do they get freed
    > automatically when the object is freed?  If not, what's the right
    > syntax for freeing a C array?

    You have to free the memory you allocated. If you used malloc() to
    get the memory, use free().

    Best Regards,

    Nir Soffer
  • Il giorno 16/dic/07, alle ore 21:05, Jason Horn ha scritto:

    > If I have a class that contains several large C arrays, do I have to
    > free them in a dealloc method in the class, or do they get freed
    > automatically when the object is freed?  If not, what's the right
    > syntax for freeing a C array?

    If you allocate memory on the heap with malloc(), thus declaring your
    variables as pointers (type* var), the space will not be recycled by
    the system on object deallocation, and usually you will free() during
    your dealloc or risk leaking memory.
    If you allocate memory in the object itself by declaring a fixed-size
    array (type var[5]) as an instance variable of the object, then the
    array will take up space in the memory structure of the object itself
    and will be recycled as part of the object being freed (just as it
    happens for structs containing fixed-size arrays in vanilla C).

      - ∞
  • Interesting,  so the following would be feed automatically?

    @interface MyObject : NSObject {
    uint16_t pixels[307200];
    }

    On Dec 16, 2007, at 3:29 PM, ∞ wrote:

    >
    > Il giorno 16/dic/07, alle ore 21:05, Jason Horn ha scritto:
    >
    >> If I have a class that contains several large C arrays, do I have
    >> to free them in a dealloc method in the class, or do they get freed
    >> automatically when the object is freed?  If not, what's the right
    >> syntax for freeing a C array?
    >
    > If you allocate memory on the heap with malloc(), thus declaring
    > your variables as pointers (type* var), the space will not be
    > recycled by the system on object deallocation, and usually you will
    > free() during your dealloc or risk leaking memory.
    > If you allocate memory in the object itself by declaring a fixed-
    > size array (type var[5]) as an instance variable of the object, then
    > the array will take up space in the memory structure of the object
    > itself and will be recycled as part of the object being freed (just
    > as it happens for structs containing fixed-size arrays in vanilla C).
    >
    > - ∞
    >
  • yes...

    Am Dec 16, 2007 um 9:50 PM schrieb Jason Horn:

    > Interesting,  so the following would be feed automatically?
    >
    >
    > @interface MyObject : NSObject {
    > uint16_t pixels[307200];
    > }
    >
    > On Dec 16, 2007, at 3:29 PM, ∞ wrote:
    >
    >>
    >> Il giorno 16/dic/07, alle ore 21:05, Jason Horn ha scritto:
    >>
    >>> If I have a class that contains several large C arrays, do I have
    >>> to free them in a dealloc method in the class, or do they get
    >>> freed automatically when the object is freed?  If not, what's the
    >>> right syntax for freeing a C array?
    >>
    >> If you allocate memory on the heap with malloc(), thus declaring
    >> your variables as pointers (type* var), the space will not be
    >> recycled by the system on object deallocation, and usually you will
    >> free() during your dealloc or risk leaking memory.
    >> If you allocate memory in the object itself by declaring a fixed-
    >> size array (type var[5]) as an instance variable of the object,
    >> then the array will take up space in the memory structure of the
    >> object itself and will be recycled as part of the object being
    >> freed (just as it happens for structs containing fixed-size arrays
    >> in vanilla C).
    >>
    >> - ∞
    >>

previous month december 2007 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
31            
Go to today