• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: returning char* from an obj-c method
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: returning char* from an obj-c method


  • Subject: Re: returning char* from an obj-c method
  • From: Steve Bird <email@hidden>
  • Date: Sun, 23 Feb 2003 20:25:55 -0500

On Sunday, February 23, 2003, at 08:05 PM, crucial felix wrote:

On Sunday, February 23, 2003, at 07:56 PM, Scott Anguish wrote:

is it as simple as that you're not actually allocating the memory for the types[] char array?

forgive me, but my real working experience with c is pretty small...
--- OK.



On Sunday, February 23, 2003, at 07:39 PM, crucial felix wrote:

hi,

I am using a framework that wraps a c library with obj-c methods.

-(char*)fulltypes
{
int i=0;
int size = [args count] + 2; // , \0

stepping through in the debugger, the int size was correct.

char types[size];

and i thought that's all i have to do.
--- This will work IF YOU WANT A LOCAL VARIABLE. I suspect you don't. A Local variable exists ONLY until the routine exits.



types[0] = ',';
for(i = 0; i < [args count]; i++) {
types[i + 1] = [[args objectAtIndex: i] type]; // each arg::type returns a char
}
types[size - 1] = '\0';
NSLog(@"built types: %s",types);

it prints out correctly here.

--- YOu are still in the scope of the local variable.


return types;
}

but once returned, its garbage.

-- You betcha. By exiting, you have declared that you are done with the memory you used for all your local variables (including the "types" variable). But you are returning a pointer to that local variable anyway. As soon as you exit, that memory can be used by any other routine.

You need to:
1... Read up on local variables vs. static variables.

AND

2... rearrange the code so that the variable exists long enough to USE IT as well as set it.
That means allocating it in the caller's space and passing a pointer to it to the "fulltypes" routine, or declaring it static, or some other solution. You really need to understand WHY though.

This is a generic programming question, not specific to Obj-C.


----------------------------------------------------------------
Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
www.Culverson.com (toll free) 1-877-676-8175
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: returning char* from an obj-c method (From: crucial felix <email@hidden>)

  • Prev by Date: Re: Project Builder won't launch
  • Next by Date: Re: returning char* from an obj-c method
  • Previous by thread: Re: returning char* from an obj-c method
  • Next by thread: Subclassing NSLayoutManager
  • Index(es):
    • Date
    • Thread