简体   繁体   English

如何在不使用 sizeof 的情况下找到变量的大小

[英]How to find the size of a variable without using sizeof

Let us assume I have declared the variable 'i' of certain datatype (might be int, char, float or double)...让我们假设我已经声明了某个数据类型的变量“i”(可能是 int、char、float 或 double)......

NOTE: Simply consider that 'i' is declared and dont bother if it is an int or char or float or double datatype.注意:只需考虑声明了“i”,如果它是 int 或 char 或 float 或 double 数据类型,请不要打扰。 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.因为我想要一个通用的解决方案,所以我只是提到变量“i”可以是任何一种数据类型,即 int、char、float 或 double。

Now can I find the size of the variable 'i' without sizeof operator?现在我可以在没有 sizeof 运算符的情况下找到变量“i”的大小吗?

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.这个想法是使用指针算法( (&(var)+1) )来确定变量的偏移量,然后减去变量的原始地址,得到它的大小。 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.例如,如果您有一个int16_t i变量位于0x0002 ,您将从0x0006减去0x0002 ,从而获得0x4或 4 个字节。

However, I don't really see a valid reason not to use sizeof , but I'm sure you must have one.但是,我真的没有看到不使用sizeof的正当理由,但我确定您必须拥有一个。

It's been ages since I wrote any C code and I was never good at it, but this looks about right:自从我写任何 C 代码以来已经有很长时间了,我从来不擅长它,但这看起来是对的:

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.我相信有人可以告诉我很多错误的原因,但它为我打印了一个合理的值。

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);
}
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);

I hope that below code would solve your problem in c++ without using sizeof() operator我希望下面的代码可以在不使用 sizeof() 运算符的情况下解决您在 C++ 中的问题

for any variables like (int, char, float, double, char, short and many more...)对于任何变量,如(int、char、float、double、char、short 等等...)

here I take integer,这里我取整数,

int a;

then show it as byte addressable output,然后将其显示为字节可寻址输出,

cout<<(char *)(&a + 1) - (char *)(&a);

这应该有效。

#define xsizeof(x) (char *)(&x+1) - (char *)&x

#define GET_SIZE(myvar) ((char)( ((char*)(&myvar+1) )- ((char*)&myvar) )) #define GET_SIZE(myvar) ((char)( ((char*)(&myvar+1) )- ((char*)&myvar) ))

这应该给你变量的大小

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

Program to find Size of the variable without using sizeof 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;
}

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

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). i是一个变量名,可以是任何数据类型(char、short、int、float、double、struct)。

#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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM