Thread: Why does sizeof only return 4???

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    69

    Why does sizeof only return 4???

    Well... I've just started programming in C again. I've been away from this beautiful language for far too long! Anyway, thought I'd brush up on a few very basic concepts, but it seems I've made a small error. Anyway, why doesn't this return the appropriate sizes for the given data types?

    Code:
    #include <stdio.h>
    
    
    int main(int argc, char *argv[])
    {
        int i;
        char *types[] = {"int", "char", "long", "double", "long long", "char*" };
    
    
        for(i=0;i<6;i++)
            printf("A %s is: %d bytes in size!\n", types[i], sizeof(types[i]));
    
    
        system("pause");
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Because you're just doing sizeof on a series of pointers.

    sizeof() cannot tell you how much memory a pointer points to.

    Use strlen() if you want to measure the length of each string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hmm... let's say we changed your program to this:
    Code:
    #include <stdio.h>
     
     
    int main(int argc, char *argv[])
    {
        int i;
        char *types[] = {"dog", "cat", "chicken", "ox", "whale", "dolphin" };
     
     
        for(i=0;i<6;i++)
            printf("A %s is: %d bytes in size!\n", types[i], sizeof(types[i]));
     
     
        system("pause");
        return 0;
    }
    Why doesn't this return the appropriate sizes for the given animals?

    Basically, types is an array of pointers to char. Therefore, types[i] is a pointer to char. Hence, sizeof(types[i]) results in the size of a pointer to char. It does not matter that these pointers happen to be pointing to the first characters of strings that happen to be C type names or animal names.
    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

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Your clever idea didn't work because the sizeof operator only cares about the type of its argument. Every cell of types is a char*, and I think you'll find that a char* is 4 bytes. Hard code what you want to know.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    69
    Damn, I can't believe I didn't realize that! Rookie mistake. Hahaha. So there's no way to circumvent this?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suppose that if you wanted to, you could do something like:
    Code:
    #include <stdio.h>
    
    #define PRINT_SIZE(x) printf("A " #x " is: %u bytes in size!\n", sizeof(x))
    
    int main(void)
    {
        PRINT_SIZE(int);
        PRINT_SIZE(char);
        PRINT_SIZE(long);
        PRINT_SIZE(double);
        PRINT_SIZE(long long);
        PRINT_SIZE(char*);
        return 0;
    }
    But of course it isn't a loop.
    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

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    In the question it seems like you are mixing up compiled languages with dynamic languages. Once a C program is compiled, the resulting program has no built-in knowledge about any C keywords. In other words, things like sizeof are all figured out at compile-time. Rather than using sizeof(), try making your own function to accomplish the task it seems you are trying to do:

    Code:
    // csize(keyword): return size of the C keyword or -1 if unknown
    size_t csize(const char *keyword)
    {
        if (strcmp(keyword, "int") == 0)
            return sizeof(int);
        else if (strcmp(keyword, "char") == 0)
            return sizeof(char);
        // else if ...
        else
            return -1;
    }
    You can extend it for all types which are keywords in C. Of course, this approach will always be incomplete because you can make custom defined types in C, which would require basically re-implementing part of the compiler in order to figure out their sizes at runtime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-09-2010, 02:33 PM
  2. how sizeof() return 2 for multicharacter constant
    By nkrao123@gmail. in forum C Programming
    Replies: 5
    Last Post: 11-26-2009, 11:39 PM
  3. Replies: 6
    Last Post: 10-15-2007, 08:05 AM
  4. sizeof(cin) and sizeof(cout)
    By noobcpp in forum C++ Programming
    Replies: 11
    Last Post: 06-30-2007, 11:00 AM
  5. Replies: 6
    Last Post: 04-09-2006, 04:32 PM