C++ 多位Trie在C语言中的实现
当我试图使用gcc编译我的C代码时,我得到了错误分段错误:11。代码正在实现ip查找的多位trie算法,如下所示: 有时代码确实会运行,但大多数情况下它最终会出现分段错误问题C++ 多位Trie在C语言中的实现,c++,c,ip,trie,C++,C,Ip,Trie,当我试图使用gcc编译我的C代码时,我得到了错误分段错误:11。代码正在实现ip查找的多位trie算法,如下所示: 有时代码确实会运行,但大多数情况下它最终会出现分段错误问题 #include <stdio.h> #include <stdlib.h> #include <arpa/inet.h> #include <math.h> /* nodes is an array 8 elements. Each element is a pointe
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <math.h>
/* nodes is an array 8 elements. Each element is a pointer to its child node.*/
/* stride has been taken as 3. therefore 2^3 = 8, the elements in a node */
struct Mt_Node{
struct Mt_Node* nodes[8];
int verdict;
};
typedef struct Mt_Node node;
/* Initialize the multibit trie node */
Mt_Node* init_mtnode(){
node *ret;
int size;
ret = static_cast<node *>(malloc(sizeof(node)));
if (ret == NULL) /* check for NULL */
return NULL;
size = 2 << 3;
if (size >= 8)
size = 7; /* maximum possible value */
for (int i = 0 ; i < size ; ++i)
ret->nodes[i] = NULL;
ret->verdict = -1;
return ret;
}
/* Clean up Multibit Trie */
void free_mt(struct Mt_Node *root){
for( int i=0; i<8; i++){
if(root->nodes[i]){
free_mt(root->nodes[i]);
}
}
free(root);
}
/* Insert a Rule */
void insert_rule(struct Mt_Node *root, uint32_t prefix, int prelen, int portnum){
static int n_rules = 0;
n_rules ++;
/* default rule: if packet matches none of the rules,
* it will match this default rule, i.e. 0.0.0.0/0 */
if( prelen == 0 ){
root->verdict = portnum;
return;
}
uint32_t temp_prefix = prefix;
int curr_bits, curr_bits1;
Mt_Node *curr_node = root;
if(prelen % 3 == 0){
/* if the condition is 0, then this node will be used,
otherwise we will have to move to next node */
for(int j=0; j<(prelen/3); j++){
curr_bits = temp_prefix & 0xE0000000;
temp_prefix = curr_bits >> 29;
int index = (int) temp_prefix;
if(curr_node->nodes[index] == NULL){
curr_node->nodes[index] = init_mtnode();
}
curr_node = curr_node->nodes[index];
temp_prefix=temp_prefix<<3;
}
curr_node->verdict = portnum;
}
else if(prelen % 3 == 1){
int b = prelen/3;
int c = 1;
for (int i=0; i<b; i++){
curr_bits = temp_prefix & 0xE0000000;
temp_prefix = curr_bits >> 29;
int index = (int) temp_prefix;
if(curr_node->nodes[index] == NULL){
curr_node->nodes[index] = init_mtnode();
}
curr_node = curr_node->nodes[index];
temp_prefix=temp_prefix<<3;
}
curr_bits = (temp_prefix & 0x80000000)? 1 : 0;
if(curr_bits == 0){
for(int z=0; z<4;z++){
if(curr_node->nodes[z] == NULL){
curr_node->nodes[z] = init_mtnode();
(curr_node->nodes[z])->verdict = portnum;
}
}
}
else {
for(int z=4; z<8;z++){
if(curr_node->nodes[z] == NULL){
curr_node->nodes[z] = init_mtnode();
(curr_node->nodes[z])->verdict = portnum;
}
}
}
}
else if(prelen % 3 == 2){
int b = prelen/3;
int c = 0;
for (int i=0; i<b; i++){
curr_bits = temp_prefix & 0xE0000000;
temp_prefix = curr_bits >> 29;
int index = (int) temp_prefix;
if(curr_node->nodes[index] == NULL){
curr_node->nodes[index] = init_mtnode();
}
curr_node = curr_node->nodes[index];
temp_prefix=temp_prefix<<3;
}
curr_bits = temp_prefix & 0xc0000000;
curr_bits = curr_bits>>30;
int curr_bits1 = (int) curr_bits;
if(curr_bits1 == 0){
for(int z=0; z<2;z++){
if(curr_node->nodes[z] == NULL){
curr_node->nodes[z] = init_mtnode();
(curr_node->nodes[z])->verdict = portnum;
}
}
}
else if(curr_bits1 == 1){
for(int z=2; z<4;z++){
if(curr_node->nodes[z] == NULL){
curr_node->nodes[z] = init_mtnode();
(curr_node->nodes[z])->verdict = portnum;
}
}
}
else if(curr_bits1 == 2){
for(int z=4; z<6;z++){
if(curr_node->nodes[z] == NULL){
curr_node->nodes[z] = init_mtnode();
(curr_node->nodes[z])->verdict = portnum;
}
}
}
else {
for(int z=6; z<8;z++){
if(curr_node->nodes[z] == NULL){
curr_node->nodes[z] = init_mtnode();
(curr_node->nodes[z])->verdict = portnum;
}
}
}
}
}
int lookup_ip(Mt_Node *root, uint32_t ip)
{
uint32_t temp_prefix = ip;
Mt_Node *curr_node = root;
int curr_verdict = root->verdict;
int curr_bit = 0;
int curr_3bits = 0;
int curr_bits1 = 0;
int b =0;
temp_prefix = temp_prefix & 0xE0000000;
temp_prefix = temp_prefix >> 29;
int index = (int) temp_prefix;
if(curr_node->nodes[index] == NULL){
return curr_verdict;
}
while(curr_node->nodes[index] != NULL){
curr_verdict = (curr_node->verdict == -1) ? curr_verdict : curr_node->verdict;
curr_node = curr_node->nodes[index];
b += 3;
temp_prefix = (ip << b);
temp_prefix = temp_prefix & 0xE0000000;
temp_prefix = temp_prefix >> 29;
index = (int) temp_prefix;
}
curr_verdict = (curr_node->verdict == -1) ? curr_verdict : curr_node->verdict;
return curr_verdict ;
}
节点中的节点数组有8个元素的空间,但在代码中最大可能值为7。因此,节点[7]没有给定值,当您使用free_mt和引用该元素的其他位置释放节点时,节点[7]中将包含随机垃圾。解决此类问题的正确工具是调试器。在询问堆栈溢出之前,应该逐行检查代码。如需更多帮助,请阅读。至少,你应该编辑你的问题,包括一个例子,它可以复制你的问题,同时也可以在调试器中做一些观察。这是C还是C++问题?选择一个或另一个标签。你应该仔细查看。或者学习如何。