Thread: Sizeof

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    102

    Sizeof

    Code:
    #include <stdio.h>
    
    size_t getSize( float *ptr ); /* prototype */
    
    int main( void )
    {
       int array[ 20 ]; /* create array */
    
       printf( "The number of bytes in the array is %d"
               "\nThe number of bytes returned by getSize is %d\n",
               sizeof( array ), getSize( array );
    
       return 0; /* indicates successful termination */
    
    } /* end main */g
    
    /* return size of ptr */
    size_t getSize( float *ptr )
    {
       return sizeof( ptr );
    
    } /* end function getSize */
    The sizeof(array) will get 80 while getSize(array) will get 4. Why it's 4. Does this means the pointer point to the first array. Example: array[0].

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    An int (on most architectures) takes up four bytes of memory. You're allocating 20 ints, hence, 80 bytes.

    The getSize function returns the size of the pointer, which is usually 4 or 8 bytes, and used to reference the location in memory that the first element is stored.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    ok thank you. Understand.

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Is pointer 4 bytes on 32 bit machine and 8 on 64? or is that irrelevant

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by camel-man View Post
    Is pointer 4 bytes on 32 bit machine and 8 on 64? or is that irrelevant
    That's generally the case, though the "bits" of a machine typically refer to the word (data) size, not the "address size".

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    it also (or mostly) depends on the compilation mode. if you compile as Win32 you still see the pointer having 4 bytes size even on the 64-bit computer
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-09-2010, 02:33 PM
  2. Replies: 6
    Last Post: 10-15-2007, 08:05 AM
  3. sizeof(cin) and sizeof(cout)
    By noobcpp in forum C++ Programming
    Replies: 11
    Last Post: 06-30-2007, 11:00 AM
  4. sizeof()
    By vb.bajpai in forum C Programming
    Replies: 6
    Last Post: 06-17-2007, 12:07 PM
  5. wat is the use of sizeof(int)?
    By zell in forum C Programming
    Replies: 3
    Last Post: 01-24-2005, 05:27 AM