Thread: question about sizeof?

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    63

    question about sizeof?

    hi
    i just needed to know about the sizeof...?what does it do?and what is it for? i have this question that i hope some1 can explain to me about its output?the output is coming out to be: 10 2 2 ,i
    wasn't being able to understand it...how did it come?
    Code:
    main()
    {
    int arr[]={12,13,14,15,16};
    printf("\n%d %d %d",sizeof(arr),sizeof(*arr),sizeof(arr[0]));
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Sizeof is an operator that returns the size of the arguement in bytes.

    Your results of 10, 2, and 2 are so because 10 is 5 the 2 bytes an int holds on your system (16 bit, though 32 bit is more common these days) The 5 being because your array has 5 members. The second is a pointer, which is also the size of an int (16 bit) and the last is the size of a single integer element in the array, which as I said is 2 bytes.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    does that mean that sizeof(arr[0]) and sizeof(*arr) are the same thing?
    i thought that *arr means that it would indicate the value at adress or the value at base address which is 12...and now it becomes sizeof(12) which is and integer so it means it is of 2 bytes....is this the right way to think...am i thinking correctly here?

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Everything in C is an object. sizeof() is a operator that returns the size of that object in bytes that is the amount of memory that object takes.
    in your code:
    sizeof(arr) is size of whole of the array. You have 5 integers in that array each take four bytes. So it will be 20.

    sizeof(*arr) and sizeof(arr[0]) have the same meaning in C: The first element of the array. So size of will return the size of first element of the array that here is an integer that takes four bytes.
    One use of sizeof() is finding the number of elements of an array, by deviding the size of it to the size of one element of it. In your code:
    Code:
     int numberOfElements = sizeof(arr) / sizeof(arr[0]); /* 20/4=5 */
    It is good to take a look at this if you know some little about pointers:
    http://www.cplusplus.com/doc/general/siavoshkc1.html
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Though, the reason you're getting results of 10, 2, 2 rather than the 20, 4, 4 that siavoshkc is saying is because, as I said, you're on a 16 bit platform, not 32.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    i believe that int takes 2 bytes and not 4 bytes as u said.....so sizeof(arr) should not be 20 but should be 10 as there r 5 elements and each integer takes 2 bytes of memory in c.i hope i am right here?

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    The amount of memory each type takes, is very dependent on platform. In mine it takes 4 bytes. This is exactly why we should use sizeof() instead of guessing the size. It keeps code portable.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by galmca
    each integer takes 2 bytes of memory in c.i hope i am right here?
    No, as I said, an integer takes 2 bytes of memory on a 16 bit system. As does a pointer. On a 32 bit system it takes 4 bytes. You under stand the correlation between 16 bits and 2 bytes as well as 32 bits and 4 bytes, correct?
    Sent from my iPadŽ

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    yeah i get it now..thanks alot for ur help!

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by SlyMaelstrom
    No, as I said, an integer takes 2 bytes of memory on a 16 bit system. As does a pointer.
    Or not. Things such as this existed on "a 16-bit system".
    sizeof(int) = 2
    sizeof(void*) = 4
    ILP32 can teach incorrect assumptions.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

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