[C language 13] The declaration of the structure, the definition and the memory alignment of the structure

Please add a picture description

1. Structure

1.1 What is a structure

In layman's terms, a structure is a collection of classes, just as an integer array is a collection of integer numbers, a structure is also a collection of values, which are called member variables. Each member of the structure can be a variable of a different type.
If we want to express a book, then what are the elements of this book, the name of the book, the author of the book, the date of creation of the book~, and the built-in collection in the c language cannot be expressed well. At this time, just The book can be defined using a struct.

1.2 Structure declaration

We can create a variable name for him when creating the structure
as follows:

struct Book
{
    
    
	char name[10];//自行定义的书的名字
	char author[10];//作者
	int year;//出版日期
}Mybook;//定义的这个结构体的名字

It is also possible to create this variable name in the main function:

int main()
{
    
    
	struct Book Mybook2;
	return 0;
}

The variables of the structure also have two different ways of passing parameters: passing by value and passing by reference.
Passing by value:

#include<stdio.h>
int main()
{
    
    
	struct Book Mybook2 = {
    
     "book","author","0708" };
	printf("%s %s %d", Mybook.name, Mybook2.author, Mybook2.year);
	return 0;
}

When using pass-by-value operations, accessing this structure should use the . operator to
pass by:

void print(struct Book* book)
{
    
    
	printf("%s %s %d", book->name,book->author,book->year);
}
int main()
{
    
    
	struct Book Mybook2 = {
    
     "book","author","0708" };
	print(&Mybook2);
	return 0;
}

1.3 Structure memory

First, you must master the alignment rules of the structure:

  1. The first member is at offset 0 from the structure variable.
  2. Other member variables should be aligned to an address that is an integer multiple of a certain number (alignment number).
    Alignment = Compiler's default alignment and the smaller value of the member size.
    The default value in VS is 8.
    There is no default alignment number in Linux, and the alignment number is the size of the member itself
  3. The total size of the structure is an integer multiple of the maximum alignment (each member variable has an alignment).
  4. If a structure is nested, the nested structure is aligned to an integer multiple of its own maximum alignment, and the
    overall is an integer multiple of all maximum alignments (including the alignment of the nested structure) .
    Take the graphic book as an example,
    insert image description here
    insert image description here
    the calculation results
    insert image description here
    are exactly the same as our calculations.
    So why does memory alignment exist?

Platform reasons (reasons for porting):
Not all hardware platforms can access any data at any address; some hardware platforms can only fetch certain types
of data at certain addresses, otherwise a hardware exception will be thrown.

Performance reasons:
data structures (especially stacks) should be aligned on natural boundaries as much as possible.
The reason is that to access unaligned memory, the processor needs to make two memory accesses; while aligned memory accesses require only one access
.

In general:
the memory alignment of structures is a practice of exchanging space for time.

The above is the introduction of the structure, if there are any deficiencies, please correct me!

Guess you like

Origin blog.csdn.net/nanmiao666/article/details/131615001