当前位置:网站首页>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 .
边栏推荐
- Detailed explanation of the relationship between Zhongtai, wechat and DDD
- Speed up the energy Internet of things. What can low-power Internet of things technology represented by Zeta do?
- How will the complete NFT platform work in 2022? How about its core functions and online time?
- It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something
- No qualifying bean of type ‘com. netflix. discovery. AbstractDiscoveryClientOptionalArgs<?>‘ available
- Fudan 961 review
- Kubedl hostnetwork: accelerating the efficiency of distributed training communication
- Alibaba cloud container service differentiation SLO hybrid technology practice
- Global and Chinese market of melting furnaces 2022-2028: Research Report on technology, participants, trends, market size and share
- Analysis of refrigeration and air conditioning equipment operation in 2022 and examination question bank of refrigeration and air conditioning equipment operation
猜你喜欢

Axure resources and prototype tool Axure RP 9 download

BBS forum recommendation

Similarities and differences of text similarity between Jaccard and cosine

(Video + graphics and text) introduction to machine learning series - Chapter 4 naive Bayes

leetcode-43. String multiplication

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

Alibaba cloud container service differentiation SLO hybrid technology practice

MySQL is installed as a Windows Service

Regular expressions and text processors for shell programming

Bodong medical sprint Hong Kong stocks: a 9-month loss of 200million Hillhouse and Philips are shareholders
随机推荐
Global and Chinese markets of distributed control system (DCS) consumption 2022-2028: Research Report on technology, participants, trends, market size and share
Selenium library 4.5.0 keyword explanation (III)
What does redis do? Redis often practices grammar every day
STM32 GPIO CSDN creative punch in
Qtcharts notes (V) scatter diagram qscatterseries
leetcode-43. String multiplication
China standard gas market prospect investment and development feasibility study report 2022-2028
[BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
国元证券开户是真的安全可靠吗
Gossip about redis source code 76
(Introduction to database system | Wang Shan) Chapter V database integrity: Exercises
2020.2.14
Enter MySQL in docker container by command under Linux
Global and Chinese market of breast cancer imaging 2022-2028: Research Report on technology, participants, trends, market size and share
Private project practice sharing populate joint query in mongoose makes the template unable to render - solve the error message: syntaxerror: unexpected token r in JSON at
AI Challenger 2018 text mining competition related solutions and code summary
Double efficiency. Six easy-to-use pychar plug-ins are recommended
Gossip about redis source code 73
D23:multiple of 3 or 5 (multiple of 3 or 5, translation + solution)
Amway by head has this project management tool to improve productivity in a straight line