strlen sizeof puzzle

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

  1. mdeh macrumors 6502

    Joined:
    Jan 3, 2009
    #1
    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:


    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));
    output:

    "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

    Joined:
    Jan 19, 2006
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Brobdingnag
    #3
    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

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

Share This Page