Nima uchun struktura eng katta ma'lumot turi bo'yicha hajmni belgilaydi?

Bu mening tuzilishim va men o'zimning ob'ektimning ajratilgan hajmini bilishni istayman.

struct toCheckSize
{
    int a;
    char b;
};

struct toCheckSize v,w,z;

Xo'sh, ajratilgan xotira hajmi v, w, z ga teng? Derivatorga ko'ra, v, w, z hajmi 8 ga teng.

1
Tasavvur qiling, agar u besh bayt bo'lsa va ikkitadan qatorni ajratgan bo'lsangiz. Ham tamsayılar qanday qilib hizalanishi mumkin?
qo'shib qo'ydi muallif David Schwartz, manba
Tasavvur qiling, agar u besh bayt bo'lsa va ikkitadan qatorni ajratgan bo'lsangiz. Ham tamsayılar qanday qilib hizalanishi mumkin?
qo'shib qo'ydi muallif David Schwartz, manba
Tasavvur qiling, agar u besh bayt bo'lsa va ikkitadan qatorni ajratgan bo'lsangiz. Ham tamsayılar qanday qilib hizalanishi mumkin?
qo'shib qo'ydi muallif David Schwartz, manba
qo'shib qo'ydi muallif jxh, manba
qo'shib qo'ydi muallif jxh, manba

9 javoblar

The padding is used for memory alignment. Modern processors perform better by accessing aligned memory using default packing. But you can use struct __attribute__((__packed__)) toCheckSize to make the structure packed with the exact size. See also Structure padding and packing

2
qo'shib qo'ydi
1) Bu KSK bo'yicha savol emas edi. 2) Uning platformasidan norozi kirishlar uchun hech qanday yordam yo'qligini qaerdan bilasiz?
qo'shib qo'ydi muallif David Schwartz, manba

The padding is used for memory alignment. Modern processors perform better by accessing aligned memory using default packing. But you can use struct __attribute__((__packed__)) toCheckSize to make the structure packed with the exact size. See also Structure padding and packing

2
qo'shib qo'ydi
1) Bu KSK bo'yicha savol emas edi. 2) Uning platformasidan norozi kirishlar uchun hech qanday yordam yo'qligini qaerdan bilasiz?
qo'shib qo'ydi muallif David Schwartz, manba

The padding is used for memory alignment. Modern processors perform better by accessing aligned memory using default packing. But you can use struct __attribute__((__packed__)) toCheckSize to make the structure packed with the exact size. See also Structure padding and packing

2
qo'shib qo'ydi
1) Bu KSK bo'yicha savol emas edi. 2) Uning platformasidan norozi kirishlar uchun hech qanday yordam yo'qligini qaerdan bilasiz?
qo'shib qo'ydi muallif David Schwartz, manba

Tizimning kattaligi sistemangizdagi tamsaytning o'lchamiga bog'liq. Bo'shliqning o'lchami amalga oshirilgan dasturdir . Ammo, agar tuzilish uchun ko'rayotgan o'lchami 8 bo'lsa, men buni sizeof (int) == 4 deb taxmin qilaman. Keyin o'lchami quyidagicha

{
  int a,  --> 4 bytes
  char b, --> 1 byte
          --> padding 3 bytes
} 
1
qo'shib qo'ydi
u ham butun sonlarning moslashtirish talablariga bog'liq.
qo'shib qo'ydi muallif Antti Haapala, manba

Tizimning kattaligi sistemangizdagi tamsaytning o'lchamiga bog'liq. Bo'shliqning o'lchami amalga oshirilgan dasturdir . Ammo, agar tuzilish uchun ko'rayotgan o'lchami 8 bo'lsa, men buni sizeof (int) == 4 deb taxmin qilaman. Keyin o'lchami quyidagicha

{
  int a,  --> 4 bytes
  char b, --> 1 byte
          --> padding 3 bytes
} 
1
qo'shib qo'ydi
u ham butun sonlarning moslashtirish talablariga bog'liq.
qo'shib qo'ydi muallif Antti Haapala, manba

Tizimning kattaligi sistemangizdagi tamsaytning o'lchamiga bog'liq. Bo'shliqning o'lchami amalga oshirilgan dasturdir . Ammo, agar tuzilish uchun ko'rayotgan o'lchami 8 bo'lsa, men buni sizeof (int) == 4 deb taxmin qilaman. Keyin o'lchami quyidagicha

{
  int a,  --> 4 bytes
  char b, --> 1 byte
          --> padding 3 bytes
} 
1
qo'shib qo'ydi
u ham butun sonlarning moslashtirish talablariga bog'liq.
qo'shib qo'ydi muallif Antti Haapala, manba

biron bir ma'lumot turining o'lchami sotuvchilarga bog'liq Sizeof (int) = 4 bayt va sizeof (char) = 1 bayt

    int main()
   {
          struct ex//ex is tag for this structure
         {
            char c;
            int i;

        };//this is the just declaration that ex tag is of the form
         struct ex x,y,z;//memory will be allocated here

       printf("%u  %u  %u\n",x,y,z);
       printf("%u  %u  %u\n",&x,&y,&z);
       printf("%u  %u  %u\n",sizeof(x),sizeof(y),sizeof(z));
       printf("%u  %u  %u\n",sizeof(x.i),sizeof(y.i),sizeof(z.i));
       printf("%u  %u  %u\n",sizeof(x.c),sizeof(y.c),sizeof(z.c));
       return 1;
 }

Chiqish

See the Chiqish of this program

Bu yerda Shuningdek, sotuvchiga qaram bo'lgan allignment constarintni ta'minlash uchun belgi to'ldirish uchun qo'shiladi

Ammo quyidagi kodni ko'ring:

struct record
{
    int id;
    char name[20];
    float salary;
 };
 struct record student;

sizeof(student);====>28bytes. because there is no need to add padding for character array

0
qo'shib qo'ydi

biron bir ma'lumot turining o'lchami sotuvchilarga bog'liq Sizeof (int) = 4 bayt va sizeof (char) = 1 bayt

    int main()
   {
          struct ex//ex is tag for this structure
         {
            char c;
            int i;

        };//this is the just declaration that ex tag is of the form
         struct ex x,y,z;//memory will be allocated here

       printf("%u  %u  %u\n",x,y,z);
       printf("%u  %u  %u\n",&x,&y,&z);
       printf("%u  %u  %u\n",sizeof(x),sizeof(y),sizeof(z));
       printf("%u  %u  %u\n",sizeof(x.i),sizeof(y.i),sizeof(z.i));
       printf("%u  %u  %u\n",sizeof(x.c),sizeof(y.c),sizeof(z.c));
       return 1;
 }

Chiqish

See the Chiqish of this program

Bu yerda Shuningdek, sotuvchiga qaram bo'lgan allignment constarintni ta'minlash uchun belgi to'ldirish uchun qo'shiladi

Ammo quyidagi kodni ko'ring:

struct record
{
    int id;
    char name[20];
    float salary;
 };
 struct record student;

sizeof(student);====>28bytes. because there is no need to add padding for character array

0
qo'shib qo'ydi

biron bir ma'lumot turining o'lchami sotuvchilarga bog'liq Sizeof (int) = 4 bayt va sizeof (char) = 1 bayt

    int main()
   {
          struct ex//ex is tag for this structure
         {
            char c;
            int i;

        };//this is the just declaration that ex tag is of the form
         struct ex x,y,z;//memory will be allocated here

       printf("%u  %u  %u\n",x,y,z);
       printf("%u  %u  %u\n",&x,&y,&z);
       printf("%u  %u  %u\n",sizeof(x),sizeof(y),sizeof(z));
       printf("%u  %u  %u\n",sizeof(x.i),sizeof(y.i),sizeof(z.i));
       printf("%u  %u  %u\n",sizeof(x.c),sizeof(y.c),sizeof(z.c));
       return 1;
 }

Chiqish

See the Chiqish of this program

Bu yerda Shuningdek, sotuvchiga qaram bo'lgan allignment constarintni ta'minlash uchun belgi to'ldirish uchun qo'shiladi

Ammo quyidagi kodni ko'ring:

struct record
{
    int id;
    char name[20];
    float salary;
 };
 struct record student;

sizeof(student);====>28bytes. because there is no need to add padding for character array

0
qo'shib qo'ydi