Reputation: 4595
Let us assume I have declared the variable 'i' of certain datatype (might be int, char, float or double) ...
NOTE: Simply consider that 'i' is declared and dont bother if it is an int or char or float or double datatype. Since I want a generic solution I am simply mentioning that variable 'i' can be of any one of the datatypes namely int, char, float or double.
Now can I find the size of the variable 'i' without sizeof operator?
Upvotes: 3
Views: 46064
Reputation: 11
#include<stdio.h>
struct abc
{
int a;
char b;
int c;
};
void main()
{
struct abc A;//FINDING THE SIZE FOR A STRUCTURE VARIABLE
printf("SIZE:%ld\n",(char*)(&A+1)-(char*)(&A));
int i=10;//FINDING THE SIZE FOR AN INT VARIABLE
printf("SIZE:%ld\n",(void*)(&i+1)-(void*)(&i));
}
Upvotes: 1
Reputation: 1
#define GET_SIZE(myvar) ((char)( ((char*)(&myvar+1) )- ((char*)&myvar) ))
Upvotes: -1
Reputation: 1325
This should work.
#define xsizeof(x) (char *)(&x+1) - (char *)&x
Upvotes: 0
Reputation: 364
I hope that below code would solve your problem in c++ without using sizeof() operator
for any variables like (int, char, float, double, char, short and many more...)
here I take integer,
int a;
then show it as byte addressable output,
cout<<(char *)(&a + 1) - (char *)(&a);
Upvotes: 1
Reputation: 1
Below statement will give generic solution:
printf("%li\n", (void *)(&i + 1) - (void *)(&i));
i
is a variable name, which can be any data type (char, short, int, float, double, struct).
Upvotes: -2
Reputation: 5962
Program to find Size of the variable without using sizeof
operator
#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;
}
Upvotes: -1
Reputation: 29
This 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);
}
Upvotes: 2
Reputation: 1
This should give you the size of your variable
#define mySizeof(type) ((uint)((type *)0+1))
Upvotes: -1
Reputation: 35
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);
Upvotes: 1
Reputation: 53
#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();
}
Upvotes: -2
Reputation: 14443
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
Upvotes: -1
Reputation: 308239
It's been ages since I wrote any C code and I was never good at it, but this looks about right:
int i = 1;
size_t size = (char*)(&i+1)-(char*)(&i);
printf("%zi\n", size);
I'm sure someone can tell me plenty of reasons why this is wrong, but it prints a reasonable value for me.
Upvotes: 8
Reputation: 91349
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 the variable, and then subtract the original address of the variable, yielding its size. For example, if you have an int16_t i
variable located at 0x0002
, you would be subtracting 0x0002
from 0x0006
, thereby obtaining 0x4
or 4 bytes.
However, I don't really see a valid reason not to use sizeof
, but I'm sure you must have one.
Upvotes: 24