size of a datatype without using sizeof

后端 未结 21 1781
慢半拍i
慢半拍i 2020-12-01 02:34

I have a data type, say X, and I want to know its size without declaring a variable or pointer of that type and of course without using sizeof oper

相关标签:
21条回答
  • 2020-12-01 02:58

    if X is datatype:

    #define SIZEOF(X) (unsigned int)( (X *)0+1 )
    

    if X is a variable:

    #define SIZEOF(X) (unsigned int)( (char *)(&X+1)-(char *)(&X) )
    
    0 讨论(0)
  • 2020-12-01 03:01

    Try this:

    int a;
    printf("%u\n", (int)(&a+1)-(int)(&a));
    
    0 讨论(0)
  • 2020-12-01 03:01

    put this to your code

    then check the linker output ( map file)

    unsigned int  uint_nabil;
    unsigned long  ulong_nabil;
    

    you will get something like this ;

    uint_nabil 700089a8 00000004
    ulong_nabil 700089ac    00000004
    

    4 is the size !!

    0 讨论(0)
  • 2020-12-01 03:01

    Try this,

    #define sizeof_type( type )  ((size_t)((type*)1000 + 1 )-(size_t)((type*)1000))
    

    For the following user-defined datatype,

    struct x
    {
        char c;
        int i;
    };
    
    sizeof_type(x)          = 8
    (size_t)((x*)1000 + 1 ) = 1008
    (size_t)((x*)1000)      = 1000
    
    0 讨论(0)
  • 2020-12-01 03:01
        main()    
        {
        clrscr();
        int n;
        float x,*a,*b;//line 1
        a=&x;
        b=(a+1);
        printf("size of x is %d",
        n=(char*)(b)-(char*)a);
        }
    

    By this code script the size of any data can be calculated without sizeof operator.Just change the float in line 1 with the type whose size you want to calculate

    0 讨论(0)
  • 2020-12-01 03:02

    Try This:

     #include<stdio.h>
    
    int main(){
    
      int *ptr = 0;
    
      ptr++;
      printf("Size of int:  %d",ptr);
    
      return 0;
    
    0 讨论(0)
提交回复
热议问题