需要帮助查找 C 程序中的分段错误
Need Help Finding Segmentation fault in C Program
我正在为一个个人学习项目在 C 中实现数据加密标准,但在过去的 3 天里,我遇到了一个段错误,这个错误一直困扰着我。我知道这不是“为我修复我的代码”类型问题的地方,但我需要第二双眼睛来检查这个:
/*we must define our own modulo, as the C modulo returns unexpected results:*/
#define MOD(x, n) ((x % n + n) % n)
/*example: the 12th bit should be in the second byte so return 1 (the first byte being 0)*/
#define GET_BYTE_NUM(bit_index) (bit_index/8)
/*example: a bit index of 12 means this bit is the 4th bit of the second byte so return 4*/
#define GET_BIT_NUM(bit_index) MOD(bit_index, 8)
typedef unsigned char byte;
/*each row represents a byte, at the bit to be place in the position
* for example for the first row (first byte) we will place bits 31, 0, 1, 2, 3, 4 in
* in bit positions 0-6, respectively. The last two bits will be left blank. Since this is supposed to be a crypto implementation, static prevents this value from being accessed outside the file.*/
const static byte e_box[8][6] = { {31, 0, 1, 2, 3, 4}, {3, 4, 5, 6, 7, 8}, {7, 8, 9, 10, 11, 12}, {11, 12, 13, 14, 15, 16}, {12, 16, 17, 18, 19, 20}, {19, 20, 21, 22, 23, 24},
{23, 24, 25, 26, 27, 28}, {27, 28, 29, 30, 31, 0} }
void e(byte **four_byte_block)
{
int i, n, l = 0, four_bit_num, four_byte_num;
/*create the new byte_block and initialize all values to 0, we will have 4 spaces of bytes, so 32 bits in total*/
byte *new_byte_block = (byte*)calloc(4, sizeof(byte));
byte bit;
for(i = 0 i < 8; i++)
{
for(n = 0; n < 6; n++)
{
/*get the byte number of the bit at l*/
four_byte_num = GET_BYTE_NUM(e_box[i][n]);
/*find what index the bit at l is in its byte*/
half_bit_num = GET_BIT_NUM(e_box[i][n]);
bit = *four_byte_block[half_byte_num]; /*SEG FAULT!*/
}
}
/*finally, set four_byte_block equal to new_byte_block*/
/*four_byte_block = NULL;
* four_byte_block = new_byte_block;*/
}
我已将问题缩小到标记为 /SEG FAULT!/ 的那一行,但我看不出是什么问题。当我打印 half_byte_num 时,我得到一个在 half_block 范围内的数字,当我打印 half_block 的值时,我可以确认这些值存在。
我相信我可能对指针做错了什么,即通过传递 **four_byte_block,(指向指针的指针)并且它的操作可能导致段错误。
你试过这个吗:
bit = (*four_byte_block)[half_byte_num];
而不是这个:
bit = *four_byte_block[half_byte_num];
这些不一样,举个例子,拿下面的代码:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char **lines = malloc(sizeof(char *) * 8);
for (int i = 0; i < 8l; i++)
lines[i] = strdup("hey");
printf("%c\n", *lines[1]);
printf("%c\n",(*lines)[1]);
return 0;
}
前者会输出h
。
后者会输出 e
.
这是因为operators precedence,[]
会在*
之前被求值,所以如果你想去*foo
的第n个索引,你需要输入 (*foo)[n]
.
我正在为一个个人学习项目在 C 中实现数据加密标准,但在过去的 3 天里,我遇到了一个段错误,这个错误一直困扰着我。我知道这不是“为我修复我的代码”类型问题的地方,但我需要第二双眼睛来检查这个:
/*we must define our own modulo, as the C modulo returns unexpected results:*/
#define MOD(x, n) ((x % n + n) % n)
/*example: the 12th bit should be in the second byte so return 1 (the first byte being 0)*/
#define GET_BYTE_NUM(bit_index) (bit_index/8)
/*example: a bit index of 12 means this bit is the 4th bit of the second byte so return 4*/
#define GET_BIT_NUM(bit_index) MOD(bit_index, 8)
typedef unsigned char byte;
/*each row represents a byte, at the bit to be place in the position
* for example for the first row (first byte) we will place bits 31, 0, 1, 2, 3, 4 in
* in bit positions 0-6, respectively. The last two bits will be left blank. Since this is supposed to be a crypto implementation, static prevents this value from being accessed outside the file.*/
const static byte e_box[8][6] = { {31, 0, 1, 2, 3, 4}, {3, 4, 5, 6, 7, 8}, {7, 8, 9, 10, 11, 12}, {11, 12, 13, 14, 15, 16}, {12, 16, 17, 18, 19, 20}, {19, 20, 21, 22, 23, 24},
{23, 24, 25, 26, 27, 28}, {27, 28, 29, 30, 31, 0} }
void e(byte **four_byte_block)
{
int i, n, l = 0, four_bit_num, four_byte_num;
/*create the new byte_block and initialize all values to 0, we will have 4 spaces of bytes, so 32 bits in total*/
byte *new_byte_block = (byte*)calloc(4, sizeof(byte));
byte bit;
for(i = 0 i < 8; i++)
{
for(n = 0; n < 6; n++)
{
/*get the byte number of the bit at l*/
four_byte_num = GET_BYTE_NUM(e_box[i][n]);
/*find what index the bit at l is in its byte*/
half_bit_num = GET_BIT_NUM(e_box[i][n]);
bit = *four_byte_block[half_byte_num]; /*SEG FAULT!*/
}
}
/*finally, set four_byte_block equal to new_byte_block*/
/*four_byte_block = NULL;
* four_byte_block = new_byte_block;*/
}
我已将问题缩小到标记为 /SEG FAULT!/ 的那一行,但我看不出是什么问题。当我打印 half_byte_num 时,我得到一个在 half_block 范围内的数字,当我打印 half_block 的值时,我可以确认这些值存在。
我相信我可能对指针做错了什么,即通过传递 **four_byte_block,(指向指针的指针)并且它的操作可能导致段错误。
你试过这个吗:
bit = (*four_byte_block)[half_byte_num];
而不是这个:
bit = *four_byte_block[half_byte_num];
这些不一样,举个例子,拿下面的代码:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char **lines = malloc(sizeof(char *) * 8);
for (int i = 0; i < 8l; i++)
lines[i] = strdup("hey");
printf("%c\n", *lines[1]);
printf("%c\n",(*lines)[1]);
return 0;
}
前者会输出h
。
后者会输出 e
.
这是因为operators precedence,[]
会在*
之前被求值,所以如果你想去*foo
的第n个索引,你需要输入 (*foo)[n]
.