Thread: A question about sizeof()

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    26

    Question A question about sizeof()

    Hi all:

    I need to measure the size of an alphanumeric string. But I found out when I used sizeof() on the following strings:

    1: VIEW1
    2: VIEW10

    The result I got was 4 for both of them. Can anyone point out why and which function I should use to measure the size of an alphanumeric string.

    Thank you

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You want strlen(). Sizeof gives you the size of the variable. I'm guessing you call it on a char*, and on a 32bit system, pointers are 4 bytes. Strlen() runs through your char* until it encounters a \0 character and then returns the number of characters read

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    26

    Thumbs up

    You got it exactly right.
    I was so confused.
    I am very glad to have helpful people like you around.

    Cheers

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    sizeof measures the size of a data type, not a specific instance. It exists because the size required to store, eg. a long int, may differ from place to place (altho I think it rarely does) and, more importantly, for checking the size of unusual data types that may be part of a non-standard library (such as a widget). Since a "char" is four bytes, you will always get the same answer.

    However, testing a char array with sizeof does return the number of characters:

    Code:
    char this[]="blah blah blah";
    int x = sizeof(this);
    x should be 15, because it includes the null terminator (\0).

    But I think the normative way to measure a string is with strlen:
    Code:
    char this[]="blah blah blah";
    int x = strlen(this);
    x should be 14, because it doen't include that extra character.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > Since a "char" is four bytes, you will always get the same answer.
    You mean 1 byte ? The standard defines, sizeof char will always result in 1.

    > a long int, may differ from place to place (altho I think it rarely does)
    It does on 64-bit Linux vs 64-bit Windows (speaking generically, of the 'typical' compilers).

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by MK27 View Post
    However, testing a char array with sizeof does return the number of characters:
    Not true. It only works for the example you've given where you initialize a variable size array with a string literal. If I declare a 1000 character array to hold user input, calling sizeof on that will always return 1000 no matter how long/short the user input.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by zacs7 View Post
    > Since a "char" is four bytes, you will always get the same answer.
    You mean 1 byte ? The standard defines, sizeof char will always result in 1.
    Oh yeah. I suppose I meant sizeof(char*)

    QuantumPete makes an key point too:
    If I declare a 1000 character array to hold user input, calling sizeof on that will always return 1000 no matter how long/short the user input.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by QuantumPete
    Not true. It only works for the example you've given where you initialize a variable size array with a string literal. If I declare a 1000 character array to hold user input, calling sizeof on that will always return 1000 no matter how long/short the user input.
    I'd say MK27 is right: "testing a char array with sizeof does return the number of characters". Testing a null terminated string (whether given a char array or a pointer) with sizeof, on the other hand, does not return the number of characters in the string (except for the special case where it is equal to the sizeof the pointer).

    Incidentally, the array is not a variable size (length) array, but one that is fixed with a size of 15 at compile time.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by laserlight View Post
    I'd say MK27 is right: "testing a char array with sizeof does return the number of characters".
    It returns the number of possible characters, but not the number of characters in your string (if we consider the string ending with its NULL terminator).

    QuantuMPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It returns the number of possible characters, but not the number of characters in your string (if we consider the string ending with its NULL terminator).
    A char array is not necessarily a null terminated string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by laserlight View Post
    A char array is not necessarily a null terminated string.
    My point exactly; since the OP wanted to find the size of his string, he needs to call strlen and not sizeof.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    My point exactly; since the OP wanted to find the size of his string, he needs to call strlen and not sizeof.
    Yes, but your other point was that MK27 was wrong to say that "testing a char array with sizeof does return the number of characters", which is incorrect. Rather, MK27 was wrong to regard the set of char arrays and the set of null terminated strings as equivalent.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read from 15-pin port
    By C_ntua in forum C Programming
    Replies: 23
    Last Post: 07-11-2008, 09:09 AM
  2. sizeof union question
    By noops in forum C Programming
    Replies: 13
    Last Post: 06-06-2008, 11:56 AM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  4. Malloc,calloc..Sscanf.
    By ozumsafa in forum C Programming
    Replies: 22
    Last Post: 07-26-2007, 01:09 AM
  5. sizeof() EZ question
    By V1P3R V3N0M in forum Windows Programming
    Replies: 15
    Last Post: 01-11-2003, 11:25 PM