当前位置:网站首页>Struct in linked list
Struct in linked list
2022-07-04 00:20:00 【Original ten】
Recently, when looking at the code , It's a little strange to find a linked list , I didn't see a reason at all , So I looked at it curiously and seriously , So I recorded this blog post .
First look at the usage :
void threadProc()
{
ListNode *node = sFdList.head;
while(node)
{
ReactorSocket *reactor = (ReactorSocket*)node;
//to do
printf("socket fd = %d, reactor addr = %p\n", reactor->socket->sockFd, reactor);
node = node->next;
}
}
node Points to the head node of the linked list , And then in while Take out from circulation , And then force it to ReactorSocket* type . here node The type of is pointing to ListNode Pointer to type ,ListNode For the definition of :
typedef struct node
{
struct node *prev;
struct node *next;
}ListNode;ReactorSocket For the definition of :
typedef struct reactorSock
{
ListNode node;
CSocket *socket;
}ReactorSocket;At first glance, see how to node The conversion of type to ReactorSocket? Actually, adding linked list nodes is , It's added like this :
void addReactor(List *list, ReactorSocket *reactor)
{
printf("add node addr: %p\n", &reactor->node);
addListNode(list, &reactor->node);
}
void addListNode(List *list, ListNode *node)
{
list->nodeTotal++;
node->next = NULL;
node->prev = NULL;
if(list->tail) // Add node to tail
{
list->tail->next = node; // The tail node next Point to a new node
node->prev = list->tail; // New nodes prev Point to the tail node
list->tail = node; // Update tail node , Now? node It's the tail node
}
else // First node
{
list->head = node;
list->tail = node;
}
}One of them is :addListNode(list, &reactor->node); It's a ReactorSocket The variable of node The address is added to the linked list , Actually node My address is ReactorSocket The address of , So it can be converted in this way :ReactorSocket *reactor = (ReactorSocket*)node;
Complete code :
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/socket.h>
// Node definition
typedef struct node
{
struct node *prev;
struct node *next;
}ListNode;
typedef struct list
{
int nodeTotal;
ListNode *head; // Chain head
ListNode *tail; // Chain tail
}List;
// In the function pointer defined below , The parameters of its function have void*, Its function is to bring objects in
typedef struct
{
uint32_t sockFd;
int32_t (*sockSend)(void *, const char *buf, uint32_t len);
int32_t (*sockRecv)(void *, char *buf, uint32_t len);
int32_t (*sockClose)(void *);
void (*sockDestory)(void *);
}CSocket;
typedef struct reactorSock
{
ListNode node;
CSocket *socket;
}ReactorSocket;
void addReactor(List *list, ReactorSocket *reactor);
void addListNode(List *list, ListNode *node);
void threadProc();
int32_t createSock(ReactorSocket *reactor);
static List sFdList;
struct Test
{
char a;
int b;
long c;
};
int main()
{
ReactorSocket *reactor = (ReactorSocket*)malloc(sizeof(ReactorSocket));
int32_t fd = createSock(reactor);
addReactor(&sFdList, reactor);
threadProc();
if(fd > 0)
{
close(fd);
}
free(reactor->socket);
free(reactor);
printf("sizeof(Test) = %lu\n", sizeof(long));
return 0;
}
void addReactor(List *list, ReactorSocket *reactor)
{
printf("add node addr: %p\n", &reactor->node);
addListNode(list, &reactor->node);
}
void addListNode(List *list, ListNode *node)
{
list->nodeTotal++;
node->next = NULL;
node->prev = NULL;
if(list->tail) // Add node to tail
{
list->tail->next = node; // The tail node next Point to a new node
node->prev = list->tail; // New nodes prev Point to the tail node
list->tail = node; // Update tail node , Now? node It's the tail node
}
else // First node
{
list->head = node;
list->tail = node;
}
}
int32_t createSock(ReactorSocket *reactor)
{
int32_t fd = socket(AF_INET, SOCK_STREAM, 0);
printf("create socket fd = %d\n", fd);
reactor->socket = (CSocket*)malloc(sizeof(CSocket));
reactor->socket->sockFd = fd;
return fd;
}
void threadProc()
{
ListNode *node = sFdList.head;
while(node)
{
ReactorSocket *reactor = (ReactorSocket*)node;
//to do
printf("socket fd = %d, reactor addr = %p\n", reactor->socket->sockFd, reactor);
node = node->next;
}
}

It can be seen from the printing that the address when adding the linked list is the same as the address taken out , So it can be converted successfully .
边栏推荐
- How will the complete NFT platform work in 2022? How about its core functions and online time?
- Subgraph isomorphism -subgraph isomorphism
- Advanced C language - pointer 2 - knowledge points sorting
- P1339 [USACO09OCT]Heat Wave G
- 2022 system integration project management engineer examination knowledge points: software development model
- [2021]NeRF in the Wild: Neural Radiance Fields for Unconstrained Photo Collections
- Analysis: misunderstanding of choosing WMS warehouse management system
- Joint examination of six provinces 2017
- [Mongodb] 2. Use mongodb --------- use compass
- Gossip about redis source code 73
猜你喜欢

STM32 GPIO CSDN creative punch in

A method to solve Bert long text matching
![[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!](/img/28/fc05f2e8d53cf81fd061c799090022.jpg)
[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!

Zipper table in data warehouse (compressed storage)

I wrote a chat software with timeout connect function

Research Report on the scale prediction of China's municipal engineering industry and the prospect of the 14th five year plan 2022-2028

Correlation analysis summary

Tencent interview: can you pour water?

Investment demand and income forecast report of China's building ceramics industry, 2022-2028

Detailed explanation of the relationship between Zhongtai, wechat and DDD
随机推荐
Double efficiency. Six easy-to-use pychar plug-ins are recommended
2022 examination of safety production management personnel of hazardous chemical production units and examination skills of safety production management personnel of hazardous chemical production unit
Reading notes on how programs run
Global and Chinese markets for blood and liquid heating devices 2022-2028: Research Report on technology, participants, trends, market size and share
Similarities and differences of text similarity between Jaccard and cosine
【leetcode】374. Guess the size of the number
Several ways to set up a blog locally [attach relevant software download links]
2022 t elevator repair registration examination and the latest analysis of T elevator repair
JDBC Technology
[leetcode] interview question 17.08 Circus tower
P1656 bombing Railway
D30:color tunnels (color tunnels, translation)
Selenium library 4.5.0 keyword explanation (III)
Actual combat | use composite material 3 in application
Global and Chinese market of underwater bags 2022-2028: Research Report on technology, participants, trends, market size and share
1214 print diamond
[NLP] text classification still stays at Bert? Duality is too strong than learning framework
D25:sequence search (sequence search, translation + problem solving)
Pytorch learning notes 5: model creation
Idea a method for starting multiple instances of a service