Thread: sizeof operator

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    1

    sizeof operator

    hello!
    Am a beginner on programming in c, i would like to know the uses of sizeof operator

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by nzagajunior View Post
    hello!
    Am a beginner on programming in c, i would like to know the uses of sizeof operator
    Well, it tells you how many bytes an object contains (with no assumptions of the number of bit in a byte, mind you), so for instance making a "shallow" copy of an object (ie: bitwise; no accounting for the values referred to by pointers of a data structure), or generic bit-twiddling algorithms for integers of arbitrary size, etc.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    The most common use is as an argument to malloc

    Code:
    int main(void)
    {
        int *x = malloc(sizeof *x);
        *x = 1234;
        printf("x is %d\n", *x);
        return 0;
    }

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by c99tutorial View Post
    The most common use is as an argument to malloc

    Code:
    int main(void)
    {
        int *x = malloc(sizeof *x);
        *x = 1234;
        printf("x is %d\n", *x);
        return 0;
    }
    I know that's just a trivial demonstrative snippet, but for the sake of good programming habits examples of malloc should always be followed up with an appropriate number of free calls.

    I'll go ahead and amend that for the OP:

    Code:
    int main(void)
    
    {
    
        int *x = malloc(sizeof *x);
        *x = 1234;
        printf("x is %d\n", *x);
        free(x);
        return 0;
    
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    True. Actually to be correct you must also handle failed memory allocations as well

    Code:
    int main(void)
    {
        // allocate x
        int *x;
        if ((x = malloc(sizeof *x)) == NULL) {
            fprintf(stderr, "Failed to allocate memory");
            return 1;
        }
    
        // do something with x
        *x = 1234;
        printf("x is %d\n", *x);
    
        // clean up
        free(x);
        return 0;
    }

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by c99tutorial View Post
    True. Actually to be correct you must also handle failed memory allocations as well

    Code:
    int main(void)
    {
        // allocate x
        int *x;
        if ((x = malloc(sizeof *x)) == NULL) {
            fprintf(stderr, "Failed to allocate memory");
            return 1;
        }
    
        // do something with x
        *x = 1234;
        printf("x is %d\n", *x);
    
        // clean up
        free(x);
        return 0;
    }
    Good point.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    One other common use is to get the number of elements in an array. This can be used like so:
    Code:
    int x[10];
    size_t i;
    
    for (i = 0; i < sizeof x / sizeof x[0]; i++)
              x[i] = 1;
    i dont believe in competition in da field of cboard posts, u see, i believe in a collection of posts each 2 be admired for der own personal statement. when in doubt, ask Willy!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-15-2007, 08:05 AM
  2. sizeof operator
    By Dave Jay in forum C Programming
    Replies: 9
    Last Post: 04-05-2005, 10:14 PM
  3. sizeof operator
    By Roaring_Tiger in forum C Programming
    Replies: 9
    Last Post: 09-05-2004, 07:31 AM
  4. sizeof operator
    By studentc in forum C Programming
    Replies: 5
    Last Post: 05-20-2004, 02:42 PM
  5. sizeof operator
    By rjeff1804 in forum C Programming
    Replies: 0
    Last Post: 01-13-2003, 01:52 PM