How to find the size of a variable without using sizeof

Suppose I have declared a variable "i" of a specific data type (can be int, char, float or double) ...

NOTE. Just think that "i" is declared and doesn't care if it's int or char or float or double datatype. Since I want to use a generic solution, I just mention that the variable "i" can be any of the data types, namely int, char, float or double.

Now I can find the size of the variable "i" without the sizeof operator?

+2


source to share


9 replies


You can use the following macro, taken from here :

#define sizeof_var( var ) ((size_t)(&(var)+1)-(size_t)(&(var))) 

      



The idea is to use pointer arithmetic ( (&(var)+1)

) to determine the offset of a variable, and then subtract the original address of the variable, giving its size. For example, if you have a variable int16_t i

located in 0x0002

, you will subtract 0x0002

from 0x0006

, thus getting 0x4

or 4 bytes.

However, I really don't see a compelling reason not to use sizeof

, but I'm sure you should have one.

+21


source


It's been so long since I wrote the C code, and I've never been good at it, but it looks right:

int i = 1;
size_t size = (char*)(&i+1)-(char*)(&i);
printf("%zi\n", size);

      



I'm sure someone can tell me a lot of reasons why this is wrong, but it prints out a reasonable value for me.

+6


source


It works.

int main() {
    int a; //try changing this to char/double/float etc each time//
    char *p1, *p2;
    p1 = &a;
    p2 = (&a) + 1;
    printf("size of variable is:%d\n", p2 - p1);
}

      

+3


source


int *a, *b,c,d;/* one must remove the declaration of c from here*/
int c=10;
a=&c;
b=a;
a++;
d=(int)a-(int)b;
printf("size is %d",d);

      

+2


source


Try it,

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

      

For the next custom data type

struct x
{
    char c;
    int i;
};

sizeof_type(x)          = 8
(size_t)((x*)1000 + 1 ) = 1008
(size_t)((x*)1000)      = 1000

      

-1


source


This should give you the size of your variable

#define mySizeof(type) ((uint)((type *)0+1))

      

-1


source


Program for determining the size of a variable without using an operator sizeof

#include<stdio.h>
int main()
  {
  int *p,*q;
  int no;
  p=&no;
   printf("Address at p=%u\n",p);
  q=((&no)+1);
  printf("Address at q=%u\n",q);
  printf("Size of int 'no': %d Bytes\n",(int)q-(int)p);

  char *cp,*cq;
    char ch;
  cp=&ch;
  printf("\nAddress at cp=%u\n",cp);
  cq=cp+1;
  printf("Address at cq=%u\n",cq);
  printf("Size of Char=%u Byte\n",(int)cq-(int)cp);

  float *fp,*fq;
  float f;
  fp=&f;
 printf("\nAddress at fp=%u\n",fp);
  fq=fp+1;
  printf("Address at fq=%u\n",fq);
  printf("Size of Float=%u Bytes\n",(int)fq-(int)fp);

  return 0;
}

      

-1


source


#include<stdio.h>

#include<conio.h>

struct size1


  {
int a;
char b;
float c;
};

void main()
{
struct size1 *sptr=0;  //declared one pointer to struct and initialise it to zero//
sptr++;                 
printf("size:%d\n",*sptr);
getch();
}

      

-2


source


A general solution will be given below the operator:

printf("%li\n", (void *)(&i + 1) - (void *)(&i));

      

i

is a variable name that can be any data type (char, short, int, float, double, struct).

-2


source







All Articles