III The representation of structure in memory

Posted by PseudoEvolution on Thu, 03 Feb 2022 09:34:33 +0100

The representation of structure in memory

structural morphology

1. We talked about struct before. We all know that struct can store many different data types
2. This will inevitably lead to a problem
3. Our structure needs memory alignment
4. If it is not aligned, the memory arrangement will be very disordered (unless there are some special requirements, for example, it becomes difficult for us to find data in reverse, and we can't see what type of data it is at a glance)
5. OK, let's give you an example

give an example

1. We said before that the default is 8 bytes

#include <stdio.h>
#include "windows.h"
typedef struct  INFO
{
	char flag;
	int id;
	float fd;
}INFO;
INFO info = {'H',1001,666.66f};
int main()
{
	printf("%c-%d-%f\n", info.flag, info.id, info.fd);
	printf("Hello Heart");
	system("pause");
	return 0;
}

Let's compile it and use xdbg to see how the alignment in memory looks
2. We attach XDBG
3.ctrl+G input_ Main comes to the main function
4. Find the global variable address

5. Observe the structure memory. Note: in xdbg, the memory window address is increasing from right to left, not from left to right, so you will see 48 on the right

6. Here we can double-click the 0049A000 address to see the offset

7. This offset has been mentioned when we re align the memory
8. At this time, we find that the data is very neat, which makes it particularly simple for us to observe the data
9. Experienced students can also immediately see the types of these data, at least not much worse

Example 2

1. After the above, we found that memory alignment has advantages
2. But as mentioned above, we can change the memory alignment to make the memory storage irregular
3. OK, now let's change the memory alignment to see how the memory performs at this time
4. Just add #pragma pack(push,1) #pragma pack(pop)

#include <stdio.h>
#include "windows.h"
#pragma pack(push,1)
typedef struct  INFO
{
	char flag;
	int id;
	float fd;
}INFO;
#pragma pack(pop)
INFO info = {'H',1001,666.66f};
int main()
{
	printf("%c-%d-%f\n", info.flag, info.id, info.fd);
	printf("Hello Heart");
	system("pause");
	return 0;
}

5. What we modify here is 1-byte alignment
6. Repeat example 1 above and observe the memory
7. You will be surprised to find out what these values are! I can't understand it!

8. We look at each data one by one, and each data is close together, which makes it very difficult for us to observe the data

Get the value inside the structure through pointer + offset

1. Let's temporarily set the memory alignment as the default 8

#include <stdio.h>
#include "windows.h"
#pragma pack(push,1)
typedef struct  INFO
{
	char flag;
	int id;
	float fd;
}INFO;
#pragma pack(pop)
INFO info = {'H',1001,666.66f};
int main()
{
	char c = *((char*)(&info));
	printf("c=%c\n", c);
	int id = *((int*)((DWORD)(&info) + offsetof(INFO, id)));
	printf("id=%d\n", id);
	printf("%c-%d-%f\n", info.flag, info.id, info.fd);
	printf("Hello Heart");
	system("pause");
	return 0;
}

2. Even if we set the alignment number to 1, we can get it correctly, because we use the offsetof function

#include <stdio.h>
#include "windows.h"
typedef struct  INFO
{
	char flag;
	int id;
	float fd;
}INFO;
INFO info = {'H',1001,666.66f};
int main()
{
	char c = *((char*)(&info));
	printf("c=%c\n", c);
	int id = *((int*)((DWORD)(&info) + offsetof(INFO, id)));
	printf("id=%d\n", id);
	printf("%c-%d-%f\n", info.flag, info.id, info.fd);
	printf("Hello Heart");
	system("pause");
	return 0;
}

3. The effect is the same
4. But in fact, when we look for data, we don't know what the type of structure looks like, because our macro function offsetof needs to know the variable name and structure type
5. So in fact, the offset we find in the data is the offset calculated by this function

summary

1. The arrangement of the member variables of the structure in the structure depends on the alignment
2. The address of the structure variable is the first address of the structure and also the address of the first member variable of our structure
3. Our offset is calculated based on the first address of the structure
4. Setting memory alignment can make the arrangement of structures in memory difficult to find data

Dida communication group: 285530835

Topics: C C++ Data Analysis