Similar to nginx, a function linked list is generated when compiling

WBOY
Release: 2016-08-08 09:22:18
Original
1218 people have browsed it

The following code may require a certain c/c++ foundation.

You need to have some knowledge of function pointers

Click here for in-depth analysis of function pointers

common.h

#pragma once
typedef int (*pt)(void);
void init_2();
Copy after login

2.cpp
#include <iostream>
#include "common.h"
using namespace std;
static pt next_pt;
extern pt top_pt;
int filter_2()
{
    cout<<"filter_2"<<endl;
    if(next_pt)
        next_pt();
}
static void filter_2_init()
{
    next_pt = top_pt;
    top_pt = filter_2; 
}
void init_2()
{
    filter_2_init();
}
Copy after login
1.cpp
#include <iostream>
#include "common.h"
using namespace std;
static pt next_pt;
pt top_pt;

static int filter_1()
{
    cout<<"filter_1"<<endl;    
    if(next_pt)
        next_pt();
}
static void filter_init()
{
    next_pt = top_pt;
    top_pt = filter_1;
}
void init_1()
{
    filter_init();
}
int main()
{
    init_1();
    init_2();
    top_pt();
    return 0;
}
Copy after login
compilation command

g++ 1.cpp 2.cpp -g -O0

Execute

./a.out

filter_2
filter_1
If you have already programmed and executed successfully, please continue reading.

top_pt is a global variable

next_pt is a local global variable

If you want to know

top_pt will change every time the code is executed, constantly pointing to the head of the new linked list. Through the continuous execution of the init_* function, a linked list is generated. It looks like a single-linked list composed of global variables.

Okay, it’s another weird and obscene technique, that’s all!

The above has introduced the function linked list generated by nginx during compilation, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!