当前位置:网站首页>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 .
边栏推荐
- What is the difference between NFT, SFT and dnft? How to build NFT platform applications?
- No qualifying bean of type ‘com. netflix. discovery. AbstractDiscoveryClientOptionalArgs<?>‘ available
- A method to solve Bert long text matching
- 【leetcode】300. Longest increasing subsequence (dynamic programming, dichotomy)
- Global and Chinese market of melting furnaces 2022-2028: Research Report on technology, participants, trends, market size and share
- Recommendation of knowledge base management system
- [MySQL] sql99 syntax to realize multi table query
- JDBC Technology
- NLP Chinese corpus project: large scale Chinese natural language processing corpus
- 网上的低佣金链接安全吗?招商证券怎么开户?
猜你喜欢
How to make recv have a little temper?
Joint examination of six provinces 2017
[C language] break and continue in switch statement
2022 t elevator repair registration examination and the latest analysis of T elevator repair
Qtcharts notes (V) scatter diagram qscatterseries
Detailed explanation of the relationship between Zhongtai, wechat and DDD
Analysis of refrigeration and air conditioning equipment operation in 2022 and examination question bank of refrigeration and air conditioning equipment operation
A method to solve Bert long text matching
Report on the construction and development mode and investment mode of sponge cities in China 2022-2028
China standard gas market prospect investment and development feasibility study report 2022-2028
随机推荐
Gossip about redis source code 81
Yyds dry goods inventory three JS source code interpretation - getobjectbyproperty method
Ramble 72 of redis source code
Global and Chinese market of underwater bags 2022-2028: Research Report on technology, participants, trends, market size and share
Idea a method for starting multiple instances of a service
Celebrate the new year | Suihua fire rescue detachment has wonderful cultural activities during the Spring Festival
Shell script three swordsman sed
2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
MySQL winter vacation self-study 2022 12 (1)
What is the Valentine's Day gift given by the operator to the product?
【leetcode】374. Guess the size of the number
Idea set class header comments
Report on the construction and development mode and investment mode of sponge cities in China 2022-2028
[PHP basics] cookie basics, application case code and attack and defense
How to make recv have a little temper?
Double efficiency. Six easy-to-use pychar plug-ins are recommended
Enter MySQL in docker container by command under Linux
NLP pre training technology development
Is the securities account opened by Caicai for individuals safe? Is there a routine
SPI based on firmware library