当前位置:网站首页>Hello C (VII) - structure
Hello C (VII) - structure
2022-06-24 23:42:00 【Tianshan old demon】
One 、 Introduction to structure
1、 Structure definition
There are two common methods for defining a structure :
The first method :
struct person{
char *name;
unisgned int age;
};
The second method :
typedef struct person{
char *name;
unsigned int age;
}Person;
person The instance declaration is as follows :
Person person;// Make a statement person object
Person *ptrPerson = (Person*)malloc(sizeof(Person));// Make a statement person object , And allocate memory
2、 Initialization of structure
Initializing a structure object using a structure declaration :
Person person;
person.name = (char *)malloc(strlen(“socprio”) + 1);
strcpy(person.name, “scorpio”);
person.age = 30;
Initialization of a structure declared using a structure pointer :
Person *ptrPerson;
prtPerson = (Person *)malloc(sizeof(Person));
ptrPerson->name = (char *)malloc(strlen(“scorpio”) + 1);
strcpy(ptrPerson->name, “scorpio”);
prtPerson->age = 30;
3、 The problem of structure release
When a structure is defined, memory is allocated for the structure , However, the runtime system will not automatically allocate memory for pointers inside the structure , When the structure is destroyed , The runtime system will not automatically release the memory pointed to by the pointer inside the structure . The pointer inside the structure points to the memory, which is generally dynamically allocated , Release after use , Otherwise, it will cause memory leakage .
4、 Dynamic allocation of memory overhead avoidance
Repeatedly allocating and then releasing structures incurs overhead , This can lead to huge performance bottlenecks . The migration method to solve this problem is to maintain a separate table for the allocated structure . When the user does not need a structure instance , Return it to the structure pool . When a structure instance is required , Get an object from the structure pool . If there are no available structures in the structure pool , A structure instance will be automatically and dynamically allocated .
Two 、 Structure macro
1、offsetof macro
offsetof Calculate the offset of the structure member from its first address according to its type and name . The macro is defined as follows :
#define offsetof(type, member) ((size_t)(&((type *)0)->member))
( (TYPE *)0 ): 0 Address enforcement " transformation " by TYPE Pointer to structure type
((TYPE *)0)->MEMBER : visit TYPE The structure of the MEMBER Data member
&( ( (TYPE *)0 )->MEMBER): Take out TYPE Data members in the structure MEMBER The address of
(size_t)(&(((TYPE*)0)->MEMBER)): The result is converted to size_t type
offsetof The macro will first 0 Convert to structure pointer type , Then reference the member variable and take its address . Because the first address of the structure is 0, So the address of the member variable is the offset of the member from the first address of the structure .
Commonly used in modern compilers
#define offsetof(type, member) __builtin_offsetof(type, member)
2、container_of macro
container_of According to the Member Address 、 Structure type and member name to calculate the first address of the structure , Its definition is as follows :
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#include <stdio.h>
#define offsetof(type, member) ((size_t)&((type *)0)->member)
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
struct test{
char a;
int b;
short c;
char *p;
double d;
}__attribute__((aligned(4)));
int main(int argc, char **argv)
{
struct test s;
printf("offset a=%lu\n",offsetof(struct test,a));
printf("offset b=%lu\n",offsetof(struct test,b));
printf("offset c=%lu\n",offsetof(struct test,c));
printf("offset p=%lu\n",offsetof(struct test,p));
printf("offset d=%lu\n",offsetof(struct test,d));
printf("s=%p\n",container_of(&s.a,struct test,a));
printf("s=%p\n",container_of(&s.p,struct test,p));
return 0;
}
Running results :
offset a=0
offset b=4
offset c=8
offset p=16
offset d=24
s=0x7fffc8590cf0
s=0x7fffc8590cf0
边栏推荐
- MySQL 表的增删查改
- R language uses the aggregate function of epidisplay package to split numerical variables into different subsets based on factor variables, calculate the summary statistics of each subset, and customi
- Printf redirection of serial port under sw4stm32 (SW4)
- Harmonyos accessing database instances (3) -- use ORM bee to test how good harmonyos is
- R语言使用MatchIt包进行倾向性匹配分析、使用match.data函数构建匹配后的样本集合、对匹配后的样本的不同分组对应的目标变量的均值进行Welch双样本t检验分析、双独立样本t检验
- [JS] - [string - application] - learning notes
- The R language uses the matchit package for propensity matching analysis and match The data function constructs the matched sample set, and performs Welch double sample t-test analysis and double inde
- 一文理解OpenStack网络
- go 语言指针,值引用和指针引用
- Window系统安装Nacos
猜你喜欢
Volcano成Spark默认batch调度器
Hydropower project construction scheme based on 3D GIS Development
JS listens for page or element scroll events, scrolling to the bottom or top
Mirror image of sword finger offer binary tree
安装IBM CPLEX学术版 academic edition | conda 安装 CPLEX
[JS] - [array application] - learning notes
单调栈以及单调栈的应用
Still using simpledateformat for time formatting? Be careful of project collapse
[JS] - [stack, team - application] - learning notes
[JS] - [array, stack, queue, linked list basics] - Notes
随机推荐
js监听页面或元素scroll事件,滚动到底部或顶部
376. 机器任务
378. Knight placement
Yyds dry goods counting uses xshell to implement agent function
Actipro WPF Controls 2022.1.2
Leetcode topic [array] -39- combined sum
普通人的生活准则
MySQL 表的增删查改
都2022年了,你还不了解什么是性能测试?
7-8 循环日程安排问题
安装IBM CPLEX学术版 academic edition | conda 安装 CPLEX
Sword finger offer merges two sorted lists
7-9 寻宝路线
Quickly build KVM virtual machine on # yyds dry goods inventory # physical machine
HMS core discovery Episode 13 live broadcast Preview - building the real world in mobile games
websocket长链接压测
选择类排序法
如何化解35岁危机?华为云数据库首席架构师20年技术经验分享
QT display RGB data
Still using simpledateformat for time formatting? Be careful of project collapse