strlen sizeof puzzle

Discussion in 'Mac Programming' started by mdeh, Apr 11, 2010.

  1. mdeh macrumors 6502

    Jan 3, 2009
    Hi all,
    May I ask what I thought would have been a simple issue. ( other questions relating to the same, ref a small difference, but usually sizeof is larger than strlen, ....accounted for by NULL)

    Given this code:

    NSString *testString = @"Some data value.";
    const char  *cStringForHashValueClass = [testString UTF8String];
    NSLog(@"\"sizeof\" \"%s\" = %i", cStringForHashValueClass, sizeof(cStringForHashValueClass) );
    NSLog(@"\"strlen\" \"%s|' = %i", cStringForHashValueClass, strlen(cStringForHashValueClass));

    "sizeof" "Some data value." = 8
    "strlen" "Some data value." = 16

    strlen of 16 I understand. But why is storage of the same variable only 8 bytes?

    Sorry if this is really obvious. :D
    thank you.
  2. Mac Player macrumors regular

    Jan 19, 2006
  3. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    And the pointer is 8 bytes because it's compiled and run for 64-bit.

    If compiled and run for 32-bits, then sizeof the pointer would be 4, while strlen would still be 16.
  4. mdeh thread starter macrumors 6502

    Jan 3, 2009
    thanks chown33 and Mac Player. I thought it might be that, but was not sure.

Share This Page