当前位置:网站首页>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 .
边栏推荐
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- P3371 [template] single source shortest path (weakened version)
- system. Exit (0) and system exit(1)
- Gossip about redis source code 81
- What is the potential of pocket network, which is favored by well-known investors?
- Global and Chinese markets of distributed control system (DCS) consumption 2022-2028: Research Report on technology, participants, trends, market size and share
- JDBC Technology
- (Video + graphics and text) introduction to machine learning series - Chapter 4 naive Bayes
- 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
- AI Challenger 2018 text mining competition related solutions and code summary
猜你喜欢
How to solve the "safe startup function prevents the operating system from starting" prompt when installing windows10 on parallel desktop?
Regular expressions and text processors for shell programming
Entropy and full connection layer
[CSDN Q & A] experience and suggestions
ESP Arduino playing with peripherals (V) basic concept of interrupt and timer interrupt
Yyds dry goods inventory three JS source code interpretation - getobjectbyproperty method
Detailed explanation of the relationship between Zhongtai, wechat and DDD
[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!
[MySQL] classification of multi table queries
[PHP basics] cookie basics, application case code and attack and defense
随机推荐
MySQL is installed as a Windows Service
Joint examination of six provinces 2017
SPI based on firmware library
[source code] VB6 chat robot
Solve the problem that the kaggle account registration does not display the verification code
Version rollback revert don't reset better reset must be forced
Global and Chinese markets of distributed control system (DCS) consumption 2022-2028: Research Report on technology, participants, trends, market size and share
Idea set class header comments
Alibaba cloud container service differentiation SLO hybrid technology practice
Stock price forecast
Global and Chinese market of underwater bags 2022-2028: Research Report on technology, participants, trends, market size and share
leetcode-43. String multiplication
Data storage - interview questions
STM32 key light
Global and Chinese market of breast cancer imaging 2022-2028: Research Report on technology, participants, trends, market size and share
Gossip about redis source code 73
ESP Arduino playing with peripherals (V) basic concept of interrupt and timer interrupt
[complimentary ppt] kubemeet Chengdu review: make the delivery and management of cloud native applications easier!
URL (data:image/png; Base64, ivborw0k... Use case
Unity elementary case notes of angry birds Siki college 1-6