I added another calculator and studied the representation and placement of data in memory.

main
KleptsovKD 3 месяцев назад
Родитель 5b398b88a4
Сommit 2593caee8d

@ -2,6 +2,7 @@
#include <cassert>
#include <iomanip>
#include <bitset>
#include <cstddef>
using namespace std;
@ -94,6 +95,15 @@ void print_in_binary(uint16_t value) {
cout << bits;
}
struct Student {
char name[17]; // 17 áàéò
uint16_t year; // 2 áàéòà
float gpa; // 4 áàéòà
unsigned gender : 1; // 1 áèò (áèòîâîå ïîëå)
uint8_t courses; // 1 áàéò
Student* monitor; // 8 áàéò (äëÿ 64-áèòíîé ñèñòåìû)
};
int main() {
// Òåñòèðîâàíèå ÷èñåë èç ëåêöèîííîãî ñëàéäà
uint16_t a = 0x4d2; // 1234 â äåñÿòè÷íîé
@ -134,6 +144,45 @@ int main() {
cout << "\n\nThe location of the structure in memory (binary):\n";
print_in_binary(arr, sizeof(arr));
Student students[3] = {
{"Ivanov Ivan", 2023, 4.5f, 1, 3, &students[1]}, // 'f' suffix for float
{"Petrova Anna", 2022, 4.8f, 0, 4, nullptr},
{"Sidorov Alex", 2023, 4.2f, 1, 2, &students[1]}
};
// Structure analysis
std::cout << "Array address: " << &students << std::endl;
std::cout << "Array size: " << sizeof(students) << " bytes\n\n";
for (size_t i = 0; i < 3; i++) {
std::cout << "Element " << i << ":\n";
std::cout << "Address: " << &students[i] << std::endl;
std::cout << "Size: " << sizeof(students[i]) << " bytes\n";
}
Student& s = students[0];
std::cout << "\nFields of the first student:\n";
std::cout << "name: Address=" << static_cast<void*>(s.name)
<< " Offset=" << offsetof(Student, name)
<< " Size=17 bytes\n";
std::cout << "year: Address=" << &s.year
<< " Offset=" << offsetof(Student, year)
<< " Size=2 bytes\n";
std::cout << "gpa: Address=" << &s.gpa
<< " Offset=" << offsetof(Student, gpa)
<< " Size=4 bytes\n";
std::cout << "courses:Address=" << static_cast<void*>(&s.courses)
<< " Offset=" << offsetof(Student, courses)
<< " Size=1 byte\n";
std::cout << "monitor:Address=" << &s.monitor
<< " Offset=" << offsetof(Student, monitor)
<< " Size=8 bytes\n";
cout << endl;
char op;

Загрузка…
Отмена
Сохранить