我遇到了一个奇怪的bug,我无论如何也找不出来。我有一个函数可以根据另一个编码函数将一个字节数组解码成一个字符串。解码的函数大致如下所示:
char *decode_string( uint8_t *encoded_string, uint32_t length,
uint8_t encoding_bits ) {
char *sequence_string;
uint32_t idx = 0;
uint32_t posn_in_buffer;
uint32_t posn_in_cell;
uint32_t encoded_nucleotide;
uint32_t bit_mask;
// Useful Constants
const uint8_t CELL_SIZE = 8;
const uint8_t NUCL_PER_CELL = CELL_SIZE / encoding_bits;
sequence_string = malloc( sizeof(char) * (length + 1) );
if ( !sequence_string ) {
ERR_PRINT("could not allocate enough space to decode the string\n");
return NULL;
}
// Iterate over the buffer, converting one nucleotide at a time.
while ( idx < length ) {
posn_in_buffer = idx / NUCL_PER_CELL;
posn_in_cell = idx % NUCL_PER_CELL;
encoded_nucleotide = encoded_string[posn_in_buffer];
encoded_nucleotide >>= (CELL_SIZE - encoding_bits*(posn_in_cell+1));
bit_mask = (1 << encoding_bits) - 1;
encoded_nucleotide &= bit_mask;
sequence_string[idx] = decode_nucleotide( encoded_nucleotide );
// decode_nucleotide returns a char on integer input.
idx++;
}
sequence_string[idx] = '\0';
printf("%s", sequence_string); // prints the correct string
return sequence_string;
}
错误是,如果我尝试打印返回指针,它会导致分段错误。但是在函数内部调用printf("%s\n", sequence_string)
将会很好地打印所有内容。如果我像这样调用函数:
const char *seq = "AA";
uint8_t *encoded_seq;
encode_string( &encoded_seq, seq, 2, 2);
char *decoded_seq = decode_string( encoded_seq, 2, 2);
if ( decoded_seq ) {
printf("%s\n",decoded_seq); // this crashes
if ( !strcmp(decoded_seq, seq) ) {
printf("Success!");
}
然后它就会在打印时崩溃。注意,其他函数似乎都可以工作,我已经对它们进行了相当彻底的测试(即decode_nucleotide
、encode_string
)。字符串也可以在函数内部正确打印。只有在函数返回后,它才会停止工作。我的问题是,通过从函数返回指针,什么可能会导致这个内存失效?提前感谢!
发布于 2018-05-04 02:24:28
语句中的第一个(并不是那么重要,但是):
sequence_string = malloc( sizeof(char) * (length + 1) );
根据定义,sizeof(char)
始终为== 1
。因此,该语句变为:
sequence_string = malloc(length + 1);
在你帖子的这一部分:
char *decoded_seq = decode_string( encoded_seq, 2, 2);
...since我看不到你的decode_string
实现,在返回之前我只能假设你是如何验证它的输出的。但是,我理解您希望返回值包含的值对于C字符串来说是合法的内容。我还可以假设,因为您正在处理编码和解码,所以输出类型很可能是unsigned char
。如果我没记错,那么输出类型的合法字符范围。
在将值发送到printf
语句之前,您没有检查输出。如果decoded_seq
的内存地址处的值恰好是0
(在unsigned char
的范围内),您的程序将崩溃。字符串函数不能很好地处理空指针。
您应该验证将其发送到printf
的_decode_string_返回
char *decoded_seq = decode_string( encoded_seq, 2, 2);
if(decoded_seq != NULL)
{
...
https://stackoverflow.com/questions/50160733
复制相似问题