• 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: UInt8 > NSString
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: UInt8 > NSString


  • Subject: Re: UInt8 > NSString
  • From: Bob Smith <email@hidden>
  • Date: Mon, 5 Mar 2007 12:22:31 -0800


The suggestion from others to return an NSString * instead is the better solution. But just so you know, the problem here is a basic C memory management error. Your function is returning an automatic variable, which becomes undefined out of scope of the function. What you need to do is change:


char addr[12]

to

static char addr[12]

and it would work. However this is not thread-safe since the same static space would be used in any thread calling the function.

Hope this helps!

Bob S.

On Mar 5, 2007, at 5:19 AM, arri wrote:

hoi list,

i'm aware my question comes from the lack of knowledge about datatypes and pointers (C, carbon etc..)
but for now i'd like to get this working quickly.


i'm using the 'GetPrimaryMACAddress' example from apple's site in order to retrieve the main MAC-address of the local machine;
http://developer.apple.com/samplecode/GetPrimaryMACAddress/index.html


the example outputs to stdout, now i modified it so that the main() function is a function i can call from cocoa like this:

static char * mainMacAddress()
{
kern_return_t kernResult = KERN_SUCCESS; // on PowerPC this is an int (4 bytes)
io_iterator_t intfIterator;
UInt8 MACAddress[kIOEthernetAddressSize];
char addr[12];
kernResult = FindEthernetInterfaces(&intfIterator);
if (KERN_SUCCESS != kernResult) {
//printf("FindEthernetInterfaces returned 0xx\n", kernResult);
sprintf(addr,"000000000000");
} else {
kernResult = GetMACAddress(intfIterator, MACAddress, sizeof (MACAddress));


if (KERN_SUCCESS != kernResult) {
sprintf(addr,"000000000000");
} else {
sprintf(addr,"xxxxxx",
MACAddress[0], MACAddress[1], MACAddress[2], MACAddress[3], MACAddress[4], MACAddress[5]);
}
}
(void) IOObjectRelease(intfIterator); // Release the iterator.
return (char*) addr;
}


and then elsewhere i call that function and create an NSString with the result like this:

char *adr  = mainMacAddress();
macAddress = [NSString stringWithCString:adr];

the compiler gives me a warning (function returns address of local variable), but it works on intel-mac. on ppc it just returns rubbish.

what would be the correct way of doing what i want?

thanks,
arri








_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: UInt8 > NSString
      • From: arri <email@hidden>
References: 
 >shortcut for selecting in NSPopUpButton (From: email@hidden)
 >UInt8 > NSString (From: arri <email@hidden>)

  • Prev by Date: Re: Multiple QTMovies and Images under one controller
  • Next by Date: Re: Interesting private API
  • Previous by thread: Re: UInt8 > NSString
  • Next by thread: Re: UInt8 > NSString
  • Index(es):
    • Date
    • Thread