簡體   English   中英

不使用sizeof的數據類型的大小

[英]size of a datatype without using sizeof

我有一個數據類型,比如X ,我想知道它的大小而不聲明該類型的變量或指針,當然不使用sizeof運算符。

這可能嗎? 我想過使用標准頭文件,其中包含數據類型的大小和范圍,但不適用於用戶定義的數據類型。

在我看來,這符合“如何在不使用++,+ =或+?”的情況下添加兩個整數的類別。 這是浪費時間。 你可以嘗試通過做這樣的事情來避免未定義行為的怪物。

size_t size = (size_t)(1 + ((X*)0));

請注意,我沒有聲明類型的變量或指向X指針。

看, sizeof 這方面的語言設施。 唯一的,所以它是實現這一目標的唯一便攜方式。

對於某些特殊情況,您可以生成不可移植的代碼,使用其他啟發式來了解特定對象的大小[*](可能通過讓它們跟蹤自己的大小),但您必須自己完成所有的簿記。

[*]對象在一般意義上而不是OOP意義上。

好吧,我是一個業余愛好者..但我嘗試了這個問題,我得到了正確的答案,沒有使用sizeof。 希望這會有所幫助..我試圖找到一個整數的大小。

int *a,*s, v=10;

a=&v;

s=a;

a++;

int intsize=(int)a-(int)s;

printf("%d",intsize);

這個采訪問題的正確答案是“為什么我會這樣做,當sizeof()為我這樣做,並且這是唯一可行的方法嗎?”

填充的可能性在不知道用於引入它的規則的情況下阻止了所有希望。 這些都是依賴於實現的。

您可以通過閱讀特定處理器的ABI來解決它,這解釋了結構在內存中的布局。 每個處理器可能有所不同。 但除非您正在編寫編譯器,否則您不希望僅使用sizeof ,這是解決此問題的一種正確方法。

試試這個:

int a;
printf("%u\n", (int)(&a+1)-(int)(&a));

查看編譯器源代碼。 你會得到 :

  • 標准數據類型的大小。
  • 填充結構的規則

從這個,任何東西的預期大小。

如果你至少可以為變量分配空間,並在其中填充一些sentinel值,你可以一點一點地改變它,看看值是否改變,但是這仍然不會告訴你任何關於填充的信息。

如果X是數據類型:

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

如果X是變量:

#define SIZEOF(X) (unsigned int)( (char *)(&X+1)-(char *)(&X) )

試試這個:

 #include<stdio.h>

int main(){

  int *ptr = 0;

  ptr++;
  printf("Size of int:  %d",ptr);

  return 0;

自用戶代碼中的C89解決方案可用:

  1. 不聲明X類型的變量。
  2. 不聲明指向X的類型的指針。
  3. 不使用sizeof運算符。

使用@steve jessop暗示的標准代碼很容易

offsetof(type, member-designator)

它擴展為一個整數常量表達式,其類型為size_t ,其值是以字節為單位的偏移量,從結構的開頭...到結構成員......C11§7.193

#include <stddef.h>
#include <stdio.h>

typedef struct {
  X member;
  unsigned char uc;
} sud03r_type;

int main() {
  printf("Size X: %zu\n", offsetof(sud03r_type, uc));
  return 0;
}

注意:此代碼使用"%zu" ,這需要C99向前。

這是代碼:訣竅是創建一個指針對象,保存其地址,遞增指針,然后從前一個地址中減去新地址。 關鍵點是當指針遞增時,它實際上移動的大小等於它指向的對象,所以這里是類的大小(它指向的對象)。

#include<iostream>
using namespace std;
 class abc
    {
           int a[5];
           float c;           
    };
main()
{
    abc* obj1;
    long int s1;
    s1=(int)obj1; 
    obj1++;
    long int s2=(int)obj1;
    printf("%d",s2-s1);
}

問候

很多這些答案都假設你知道你的結構會是什么樣子。 我相信這個面試問題的目的是要求你跳出框框思考。 我正在尋找答案,但沒有找到我喜歡的任何解決方案。 我會做出更好的假設

struct foo {
  int a;
  banana b;
  char c;
  ...
};

通過創建foo [2],我現在將在內存中有2個連續的foo對象。 所以...

foo[2] buffer = new foo[2];
foo a = buffer[0];
foo b = buffer[1];

return (&b-&a);

假設我的指針算法正確,這應該是票 - 它的便攜性! 不幸的是,諸如填充,編譯器設置等等也都會起作用

思考?

把它放到你的代碼中

然后檢查鏈接器輸出(映射文件)

unsigned int  uint_nabil;
unsigned long  ulong_nabil;

你會得到這樣的東西;

uint_nabil 700089a8 00000004
ulong_nabil 700089ac    00000004

4是大小!!

一種簡單的方法是使用數組。 現在,我們知道在數組中,相同數據類型的元素存儲在連續的內存塊中。 所以,通過利用這個事實我想出了以下內容:

#include <iostream>
using namespace std;

int main()
{
    int arr[2];
    int* ptr = &arr[0];
    int* ptr1 = &arr[1];
    cout <<(size_t)ptr1-(size_t)ptr;
}

希望這可以幫助。

# include<stdio.h>

struct node
{
  int a;
  char c;
};

void main()
{
   struct node*ptr;
   ptr=(struct node*)0;
   printf("%d",++ptr);
}
#include <stdio.h>

struct {
  int a;
  char c;
};

void main() {
  struct node*temp;
  printf("%d",(char*)(temp+1)-(char*)temp);
}
    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);
    }

通過此代碼腳本,可以在不使用sizeof運算符的情況下計算任何數據的大小。只需要更改第1行中的float,其類型為要計算的大小

試試這個,

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

對於以下用戶定義的數據類型,

struct x
{
    char c;
    int i;
};

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

這考慮到C ++字節並不總是8個二進制位,並且只有無符號類型具有明確定義的溢出行為。

#include <iostream>
int main () {
    unsigned int i = 1;
    unsigned int int_bits = 0;
    while (i!=0) {
        i <<= 1;
        ++int_bits;
    }

    unsigned char uc = 1;
    unsigned int char_bits = 0;
    while (uc!=0) {
        uc <<= 1;
        ++char_bits;
    }

    std::cout << "Type int has " << int_bits << "bits.\n";
    std::cout << "This would be  " << int_bits/8 << " IT bytes and "
              << int_bits/char_bits << " C++ bytes on your platform.\n";
    std::cout << "Anyways, not all bits might be usable by you. Hah.\n";
}

當然,你也可以#include <limit><climits>

#include <bits/stdc++.h> 

using namespace std; 

int main() 
{ 

    // take any datatype hear 
    char *a = 0; // output: 1

    int  *b = 0;  // output: 4

    long *c = 0; // output: 8

    a++;

    b++;

    c++;

    printf("%d",a);

    printf("%d",b);

    printf("%d",c);

    return 0; 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM