Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8886027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:27:28+00:00 2026-06-14T21:27:28+00:00

i have two Objective-C classes, say ParentLayer and ChildLayer. in my child instance, i

  • 0

i have two Objective-C classes, say ParentLayer and ChildLayer. in my child instance, i want to access a C-Array in my parent instance. so i have something like this in my cocos2d code:

#define kNumOfElements 10
@implementation ParentLayer{
    int array[kNumOfElements];
}
-(id)init{
    //...
    for(int i=0;i<kNumOfElements;i++){
        array[i] = i;
    }
    [self addChild:childLayer];
    [childLayer initializeValues];
    //...
}
-(int *)getArray{
    return array;
}
@end

//meanwhile in my child layer...
//...
-(void)initializeValues{
    int *arr = [(ParentLayer *)[self parent] getArray];
    //NSLog(@"%d",arr[0]);   <------- this gives you bad exec access point, and looks like it's 0x00 for memory address
}
//...
  • what’s the proper way to do this?
  • maybe i dont understand the right memory management behind C Arrays.
    i was under the impression that C Arrays didn’t need to be allocated,
    and that they could be passed by value, on the stack?
  • also, shouldn’t my parent instance still be around? i thought if i
    put a C Array as an ivar of my parent, it shouldn’t get destroyed

any help is appreciated. thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:27:29+00:00Added an answer on June 14, 2026 at 9:27 pm

    what’s the proper way to do this?

    Ideally, you should never pass a C-style array pointer outside of the object that owns it. You open yourself up to all sorts of problems if a piece of code tries to use the array after the object is deallocated, or writes past the end, or something else. It is easier to guarantee that none of this happens if you can make sure the reference never leaves the object’s source file.

    maybe i dont understand the right memory management behind C Arrays. i was under the impression that C Arrays didn’t need to be allocated, and that they could be passed by value, on the stack?

    It is not that simple.

    A C-style array is just a memory address. That’s it. It doesn’t carry around the other useful information that an object might, such as number of elements, retain count.

    If you declare an array like this:

    int array[100];
    

    Then the memory is allocated in either the stack or the heap, depending on where you put the declaration. If it’s a local variable inside a function or method, it’s on the stack. If it’s in global scope or a member variable of an object, it’s on the heap.

    Furthermore, if it’s an instance variable, you’re actually setting aside 100 ints worth of memory inside the block of memory allocated to hold the object. It isn’t a separate thing.

    Since array is just a memory address, you are basically passing it around by reference. Technically, you are passing the address by value, but any changes you make to the memory will be seen by anyone looking at the same address, so it acts like pass by reference.

    also, shouldn’t my parent instance still be around? i thought if i put a C Array as an ivar of my parent, it shouldn’t get destroyed

    The way you have coded it, that array will be valid as long as the parent object is around. Once the parent gets deallocated, that memory could be reclaimed. Since the array variable is just a memory address, however, you have no way of knowing whether the data it points to is valid or not. This is the danger of using C-style arrays rather than objects.

    Since the last line is giving you NULL (0) address, my guess is that [self parent] is nil. That would put a 0 in arr; when you try to dereference NULL, you will get an exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my game in objective c, I have two classes, The main ArcherClass and
I created two classes in objective c and I would like to use one
Supposed I have two objective-c classes, LBFoo and LBBar . In LBFoo I have
I have two major classes in objective C using cocos2D, DebugZoneLayer and HeroClass. Using
I have two model classes: Goal and Objective. A Goal has many Objectives, and
Possible Duplicate: How can I have references between two classes in Objective-C? Objective-C #import
I have an Objective-C++ file, and I have two classes: one Objective-C, one C++:
I have two Objective-C classes and one is derived from the other as @interface
I'm trying to override a getter in Objective-C. I have two classes, First and
I have an objective-c application for OS X that compares two sqlite DB's and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.