Thread: sizeof operator

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    175

    sizeof operator

    Hello All,

    Looking at the following code sizeof int is hardcoded.

    Code:
     size = sizeof(i);
    0040B840   mov         byte ptr [ebp-8],4

    I had thought that sizeof operator work similar to the following code.( 5th and 6th printf)
    Code:
    # include<stdio.h>
    
    main(void)
    {
    	int i;
    	char character;
    	int *p = &i;
    	char *c = &character;
    
    	printf("Size of %d\n",p);
    	printf("Size of %d\n",c);
    
    	printf("Size of %d\n",p+1);
    	printf("Size of %d\n",c+1);
    
    	printf("Size of %d\n",p+1-p);
    	printf("size of %d\n",c+1-c);
    
    	printf("Size of %p\n",p+1-p);
    	printf("size of %p\n",c+1-c);
    
    	character = sizeof(i);
    	printf("Sizeof int = %d\n",character);
    }
    But looking the above two snaps I am confused.

    Please let me know, How sizeof is implemented.

    And in above C program, 5th and 6th printf statement prints

    1
    1

    and is still confusing.

    I wonder, when - and + operator have same associtivity and at same level, why doesn't it increment first to address four bytes ahead( in case of integer pointer) and substracting same pointer give size of datatype.

    Please let me know..

    RT

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    But you see you only moved the pointer 1 * sizeof (int) units away from p.

    Lets use simple math here:
    x + 1 - x =
    x - x + 1 =
    0 + 1 =
    1

    Same rule applies when using pointer arithmatic. It doesn't tell you how many bytes are between the two pointers, it tells you how many ( bytes / sizeof (object) ) are between the two

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Im not sure what all those printfs are supposed to do, but that is not how sizeof() is implemented.

    sizeof() is implemented by the compiler. It is neither a operator or a function, it is a language feature (like for, if, while). sizeof() is executed by the compiler, and not during runtime. That is why if you look at the ASM code for sizeof(), it appears to be hard-coded in.

  4. #4
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by bithub
    Im not sure what all those printfs are supposed to do, but that is not how sizeof() is implemented.

    sizeof() is implemented by the compiler. It is neither a operator or a function, it is a language feature (like for, if, while). sizeof() is executed by the compiler, and not during runtime. That is why if you look at the ASM code for sizeof(), it appears to be hard-coded in.
    actually sizeof is a unary operator.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    I mean, How compiler knows that size of integer is 4 byte?

    If you look at assembly code it directly copies value 4.

    Let me know..
    RT

  6. #6
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Someone will probably answer this better. But If i'm correct An Ansi compiller should conform to the rules of ANSI C. As far as i know C gurantes that an integer should be 4 bytes of memory minum. I believe it can vary based on your system (ie 16bit, 32 or 64 bit). Same way it guarantees the minimum size of a char and "other types"
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    sizeof is calculated at compile time, not at run time. The same compiler on your system will always generate 4 byte integers (for instance) so the compiler can just directly swap sizeof(int) for 4 during the compilation. That's why you're seeing 4 in the assembly output. The compiler doesn't create assembly code to figure out the size of an int, it already knows and just plugs the value in. It's the same reason that if you had a = 4+3; you'd likely see the assembly set a directly to 7 instead of calculating 4+3. The compiler is smart and tries to make the program only do as much as it has to at run time

    Size of an int doesn't have to be 4 bytes minimum. It just has to be at least as large as a short int.
    Last edited by itsme86; 09-04-2004 at 07:36 PM.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by caroundw5h
    As far as i know C gurantes that an integer should be 4 bytes of memory minum. I believe it can vary based on your system (ie 16bit, 32 or 64 bit). Same way it guarantees the minimum size of a char and "other types"
    In C, it is best to remember that values are key. An integer is guaranteed to be within some minimum value range. From this some things may be inferred, but try not to assume too much. A single 32-bit byte will easily contain the minimum requirements without requiring any multi-"byte" storage.
    Last edited by Dave_Sinkula; 09-04-2004 at 08:16 PM.
    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.*

  9. #9
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Thanks for clarifying Dave. As I said someone else would be more knowledgable in that.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I mean, How compiler knows that size of integer is 4 byte?
    Knowing that the size if an int is 4 isn't half as interesting as how the compiler knows that the following is an int and correctly prints its size.
    Code:
    int i;
    
    printf ( "%lu\n", (unsigned long)sizeof i );
    Lookup tables are much more interesting than unchanging values prechosen by the compiler writer.

    >As far as i know C gurantes that an integer should be 4 bytes of memory minum.
    Actually, 2 bytes is the minimum (assuming 8 bit bytes). int must be at least 16 bits wide, but can and often is more. Most systems these days use 4 byte integers.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Replies: 6
    Last Post: 10-15-2007, 08:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM