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

Re: hash problem with NSString


  • Subject: Re: hash problem with NSString
  • From: Steve Checkoway <email@hidden>
  • Date: Mon, 27 Sep 2004 22:14:39 -0700


On Sep 27, 2004, at 2:49 PM, Nat! wrote:
int main (int argc, const char * argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

const char char_array[5] = { 'R', 'o', 'o', 't', '\0' };
NSString *a = [NSString stringWithCString:&char_array length:5];
NSString *b = [NSString stringWithCString:&char_array length:strlen( char_array)];
NSString *c = @"Root";


   NSLog( @"Hash: a=%X b=%X c=%X", [a hash], [b hash], [c hash]);

   [pool release];
   return 0;
}

returns

2004-09-27 23:48:22.489 hash_really_broken_question[2466] Hash: a=889E36C9 b=9B749F28 c=9B749F28

I fail to see the problem here. Unless I'm totally wrong, a has an internal length of 5 characters with the string "Root\0". strlen() is going to return 4 as the length of char_array so b has a length of 4. b and c are identical so they have the same hash value while a has an extra \0 so it has a different hash value. This is exactly what I would expect.


Try changing strlen to sizeof. I would guess that a and b would then have the same hash value of 0x889E36C9.

- Steve

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >hash problem with NSString (From: Eric Ocean <email@hidden>)
 >Re: hash problem with NSString (From: Eric Ocean <email@hidden>)
 >Re: hash problem with NSString (From: Nat! <email@hidden>)

  • Prev by Date: NSSearchField sub menu weirdness
  • Next by Date: Window zooming and NSTableView column widths
  • Previous by thread: Re: hash problem with NSString [RADAR #]
  • Next by thread: sort descriptors [Java]
  • Index(es):
    • Date
    • Thread