分段错误(核心转储) [英] segmentation fault (core dump)

查看:137
本文介绍了分段错误(核心转储)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个错误的分割(核心转储),当我尝试运行此。它编译完美,但我得到的错误,我不知道为什么。我试图编辑我的code在所有可能的方式,但我仍然收到此错误。我的想法了。任何帮助将是巨大的。谢谢!

 无符号短*章= NULL;    INT字节;
    INT I;
    对于(字节= 0;字节< NUM_BYTES;字节++){
        unsigned int类型next_byte =(unsigned int类型)消息(字节);
        crc_byte(REG,钥匙,next_byte);
    }    对于(i = 0; I< 16;我++){
        crc_bit(章,键,0);
    }    返回*章;
}


解决方案

对于我来说,你的段错误的问题来自于章指针是NULL。
这意味着你将修改位于地址零unisgned hsort值。在大多数操作系统中,这是不允许的。

你为什么不这样做以下的事情吗?

 无符号短crc_message(无符号整数键,字符*消息,诠释NUM_BYTES){无符号短章;INT字节;
INT I;
对于(字节= 0;字节< NUM_BYTES;字节++){
    unsigned int类型next_byte =(unsigned int类型)消息(字节);
    crc_byte(安培;章,钥匙,next_byte);
}对于(i = 0; I< 16;我++){
    crc_bit(安培;章,键,0);
}返回章;

}

I'm getting a segmentation error (core dump) when I try to run this. It compiles perfectly but I get the error, and I don't know why. I've tried to edit my code in all possible ways, but am still getting this error. I'm out of ideas already. Any help would be great. Thanks!

    unsigned short *reg = NULL;

    int byte;
    int i;
    for (byte = 0; byte < num_bytes; byte++){
        unsigned int next_byte = (unsigned int) message[byte];
        crc_byte(reg, key, next_byte);
    }

    for (i = 0; i < 16; i++){
        crc_bit(reg, key, 0);
    }

    return *reg;
}

解决方案

For me, your segmentation fault problem comes from the reg pointer which is NULL. This means that you will modify an unisgned hsort value located at address zero. On most operating systems, this is not allowed.

Why don't you do the following thing ?

unsigned short crc_message(unsigned int key, char *message, int num_bytes) {

unsigned short reg;

int byte;
int i;
for (byte = 0; byte < num_bytes; byte++){
    unsigned int next_byte = (unsigned int) message[byte];
    crc_byte(&reg, key, next_byte);
}

for (i = 0; i < 16; i++){
    crc_bit(&reg, key, 0);
}

return reg;

}

这篇关于分段错误(核心转储)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆