• 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: Pete Yandell <email@hidden>
  • Date: Mon, 24 Feb 2003 12:26:41 +1100

Felix,

You can't do this in C!

The reason is that the variable 'types' is allocated within the stack frame for your 'fulltypes' method. When you return from that function, the local variables all go away and the char pointer you've returned is left pointing to garbage.

You've got a couple of options:

1) Allocate the memory for 'types' using malloc and return that. Then, of course, the calling method will be responsible for later freeing that memory.

2) A better idea would be to return an NSString, something like:
return [NSString stringWithCString:types];


On Monday, February 24, 2003, at 11:39 AM, 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
char types[size];
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);
return types;
}

you can see the result logged here:
2003-02-23 19:20:06.646 Untitled[24544] built types: ,siiisfssiiisf

but here is the method being called from my unit test:

NSLog(@"fulltypes: %s",[msg fulltypes]);

2003-02-23 19:20:06.646 Untitled[24544] built types: ,siiisfssiiisf
2003-02-23 19:20:06.646 Untitled[24544] fulltypes : \\370\\u02c7\\317\\uf8ffisfs\\352\\307\\u02db\\331sf

the char* didn't survive being returned ?
the header for -(char*)fulltypes is correct. ( a common problem )

does anybody have any ideas why my string turned to gibberish ?
some kind of encoding mismatch, or total garbage ?

thank you, oh wise ones.

-felix
_______________________________________________
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.


Pete Yandell
http://pete.yandell.com/
_______________________________________________
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: 
 >returning char* from an obj-c method (From: crucial felix <email@hidden>)

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