sizeof operator (in C)

Sharky Forums


Results 1 to 9 of 9

Thread: sizeof operator (in C)

  1. #1
    Hammerhead Shark
    Join Date
    Feb 2001
    Posts
    1,612

    Post sizeof operator (in C)

    Is it legal to use the sizeof operator anywhere in a C program, except in #if statements? This book says that sizeof is a compile-time operator, so would that mean I couldn't use it if the compiler can't know the size of that object? For example:
    Code:
    #include <stdio.h>
    
    void dosomething(int);
    
    int main()
    {
    	srand();
    	dosomething(rand());
    	return 0;
    }
    
    void dosomething(int x)
    {
    	char s[x];
    	printf("%d\n", sizeof s);
    }
    I tried doing something like this, but ended up with a Segmentation fault. I wasn't sure why I got one, so I just decided to ask the question here. Thanks in advance.

  2. #2
    Tiger Shark
    Join Date
    Mar 2001
    Posts
    615

    Post

    Yeah, sizeof can be thought of of working like a #define statement. That is, all of the calls to "sizeof" are evaluated when you compile, and are, more-or-less, replaced with the actual numbers. The reason people use sizeof is to make sure that they are using the right size for a type, and this especially applies when dealing with how portable code will be.

    The code you posted is bad because of:

    "char s[x];"

    I wouldn't think that would even compile, since an array must be defined with a number that is known at compile-time, and in your program, x is most definitely not.

    Also, even if you had something like: "s[80]", sizeof(s) would still be 4 on most systems, since s is really a pointer to a character, which is probably a 32-bit pointer on your system.
    System specs:


    | Core i5 750 | GA-P55A-UD3 | 4.0 GB G.skill DDR3 1600 | eVGA 470 GTX |
    | Intel X25-M 80 GB SSD | WD 5000AAKS | Lian Li PC-7FN | Corsair TX750W |
    | Windows 7 Home 64-bit |

  3. #3
    Hammerhead Shark
    Join Date
    Feb 2001
    Posts
    1,612

    Post

    I wouldn't think that would even compile, since an array must be defined with a number that is known at compile-time...
    Wow, really? I never realized that!

    Also, even if you had something like: "s[80]", sizeof(s) would still be 4 on most systems, since s is really a pointer to a character, which is probably a 32-bit pointer on your system.
    Are you sure about this? This book I'm using (The C programming language, 2nd ed.) says that you can use sizeof to find the amount of elements in this array keytab[]:
    Code:
    struct key {
    	char *word;
    	int count;
    } keytab[] = {
    	"auto", 0,
    	"break", 0
    };
    
    #define NKEYS (sizeof keytab / sizeof(struct key))

  4. #4
    Hammerhead Shark
    Join Date
    Feb 2001
    Posts
    1,612

    Question

    Oh boy...

    Code:
    #include <stdio.h>
    
    void dosomething(int x);
    
    int main()
    {
    	int x;
    	srand((unsigned int) time());
    	do
    		x = rand();
    	while(x > 500);
    	dosomething(x);
    	printf("%d\n", x);
    	return 0;
    }
    
    void dosomething(int x)
    {
    	char s[x];
    	printf("%d\n", sizeof s);
    }
    That program there works perfectly. It looks like the segmentation faults were simply caused by trying to declare an array with too many elements. Anyway, I'm definitely getting confused now.

  5. #5
    Hammerhead Shark
    Join Date
    Feb 2001
    Posts
    1,612

    Post

    Well, reading over some other message boards, it looks like there is a new standard for C called C99, that will allow variable-length arrays. (which is why char s[x] worked, on my compiler) Maybe the compiler just knew that the size of s would always be x, so it just replaced "sizeof s" with "x"?

  6. #6
    Reef Shark
    Join Date
    Apr 2001
    Location
    Austin, TX
    Posts
    371

    Post

    I think you're confusing what sizeof s with the number of elements in the array. s is a pointer, and so sizeof is going to return the size of that pointer. Allow me to explain:

    Code:
    int w;
    int *x;
    int y[5];
    struct z { int a,b,c; };
    struct z zs;
    struct z *zp;
    
    sizeof w;  // the size of an int
    sizeof(int); // the size of an int
    sizeof x; // the size of a pointer
    sizeof *x; // the size of an int
    sizeof y; // the size of the array
    sizeof (struct z); // the size of the structure
    sizeof zs; // the size of the structure
    sizeof zp; // the size of a pointer
    Here's an example of using sizeof to compute array elements:

    Code:
    sel = 0;
    while (sel != (sizeof menu / sizeof(Menu))) {
      for (i = 0; i < Selections; ++i) {
        // something with menu[i]
      }
    }
    Does that make it a bit clearer?

    ------------------
    Advocate of the Sharky (Ultra) High-Resolution Club [SHRC]
    main(i){putchar(341513875>>(i-1)*5&31|!!(i<6)<<6)&&main(++i);}
    Advocate of the Sharky (Ultra) High-Resolution Club [SHRC]
    main(i){putchar(341513875>>(i-1)*5&31|!!(i<6)<<6)&&main(++i);}

  7. #7
    Hammerhead Shark
    Join Date
    Feb 2001
    Posts
    1,612

    Post

    sizeof y; // the size of the array
    That is the same as my sizeof s;
    Why does everyone want to tell me that I'm getting the size of a pointer? =) (another msg board I posted this to got a response telling me that sizeof s would be the size of a pointer) The output looks like this, btw (from running it several times):
    421
    421

    312
    312

    316
    316

  8. #8
    Mako Shark dighn's Avatar
    Join Date
    Nov 2000
    Location
    Vancouver, BC Canada
    Posts
    3,171

    Post

    Originally posted by Strogian:
    sizeof y; // the size of the array
    That is the same as my sizeof s;
    Why does everyone want to tell me that I'm getting the size of a pointer? =) (another msg board I posted this to got a response telling me that sizeof s would be the size of a pointer) The output looks like this, btw (from running it several times):
    421
    421

    312
    312

    316
    316
    It really depends on how much the compiler knows
    for example

    char blah[50];
    sizeof blah would give you 50
    BUT
    if you do something like blah* = malloc(50);
    sizeof blah would give you the size of the pointer which is usually 4 bytes

    the first blah and the second blah are both of type char*, but they give u different results because the compilers knows more in the first case...

    ------------------
    So easy to use, no wonder it's number one!!!
    Where do you want to go today?
    Member of NT Jocks
    Tesla coils are not weapons

    [This message has been edited by dighn (edited July 18, 2001).]
    .

  9. #9
    Tiger Shark
    Join Date
    Mar 2001
    Posts
    615

    Post

    Yeah, I was wrong, sizeof(someArray) DOES return the size of the array, and not of a pointer. I must have done something weird on my compiler
    System specs:


    | Core i5 750 | GA-P55A-UD3 | 4.0 GB G.skill DDR3 1600 | eVGA 470 GTX |
    | Intel X25-M 80 GB SSD | WD 5000AAKS | Lian Li PC-7FN | Corsair TX750W |
    | Windows 7 Home 64-bit |

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •