下面的函数fast_integer_output将input_integer从基数2转换为output_base。
my_str* fast_integer_output(bigint* input_integer, int output_base)
{
bigint** integer_to_binary_array = create_integer_to_binary_array();
bigint* base = integer_to_binary_array[output_base];
my_str* result = 0;
if(less_than(input_integer, base))
{
char* p_char = (char*) get_memory(sizeof(char));
p_char[0] = int_to_char(*(input_integer->number));
result = create_string(p_char, 1);
}
else
{
long k = find_k(input_integer, base);
bigint* base_to_k = power(base, k);
bigint* quotient;
bigint* remainder;
divide(input_integer, base_to_k, "ient, &remainder);
delete_bigint(&base_to_k);
my_str* low = fast_integer_output(remainder, output_base);
delete_bigint(&remainder);
my_str* high = fast_integer_output(quotient, output_base);
delete_bigint("ient);
result = concatenate(low, k - low->length, high);
delete_string(&low);
delete_string(&high);
}
release_integer_to_binary_array(integer_to_binary_array);
return result;
}
这里是 bigint 和 my_str 结构体以及 create_string 函数(bitarray
只是一个指向 的指针长
)
my_str* create_string(char* c_str, long length)
{
my_str* str = (my_str*) get_memory(sizeof(my_str));
str->c_str = c_str;
str->length = length;
}
typedef struct
{
char* c_str;
long length; // logical
} my_str;
typedef struct
{
bitarray* number;
long length; // logical
long size; // physical
} bigint;
这个函数负责内存管理。现在他们只是将函数包装到free和malloc,但我想实现某种内存池,如果它运行缓慢的话。
void init_memory(void* memory, size_t size)
{
unsigned char* tmp = (unsigned char*) memory;
unsigned char c = 0;
while (size > 0)
{
*tmp = c;
tmp++;
size--;
}
}
void* get_memory(size_t size)
{
void* memory = malloc(size);
init_memory(memory, size);
return memory;
}
void release_memory(void** memory)
{
free(*memory);
*memory = 0;
}
问题是,在调试配置上一切运行良好,但在发布配置上,函数 fast_integer_output 失败:
result = concatenate(low, k - low->length, high);
问题在于
my_str* low = fast_integer_output(remainder, output_base);
从这段代码返回
if(less_than(input_integer, base))
{
char* p_char = (char*) get_memory(sizeof(char));
p_char[0] = int_to_char(*(input_integer->number));
result = create_string(p_char, 1);
}
返回垃圾,因此它会因段错误而失败。
我对此代码遇到了同样的问题:
QString buffer; // function parameter in real code
instance_data_t data = {(unsigned char*) buffer.toStdString().c_str(), 0, 0, 0, 0, 0, 0, {0, 0}, {0, 0}, cur_state};
但我设法通过将其更改为以下内容来使其正常工作:
unsigned char c_buffer[1024];
memset(c_buffer, '\0', sizeof(c_buffer));
strcpy((char *) c_buffer, buffer.toStdString().c_str());
instance_data_t data = {c_buffer, 0, 0, 0, 0, 0, 0, {0, 0}, {0, 0}, cur_state};
重要说明是,除了 write、read、malloc 和 free 之外,我不能使用任何其他函数(所以没有 strcpy,上面的代码是来 self 不会提供的测试)
这不是作业,这是一份工作作业(我想得到)。
我已经搜索并阅读了大约 15-20 个问题,所以我不会全部列出,但问题是,大多数情况下,如果问题不是很具体,段错误不是因为字符串操作,如果它是,那么多半是因为索引越界了。
最佳答案
您分配的字符串不是以空('\0')结尾的。
我会这样做:
char* p_char = (char*) get_memory(sizeof(char) * 2);
p_char[0] = int_to_char(*(input_integer->number));
p_char[1] = 0;
result = create_string(p_char, 1);
正如 EOF 指出的那样(参见评论), create_string函数作为没有return语句。 因此,垃圾确实被返回了。
my_str* create_string(char* c_str, long length)
{
my_str* str = (my_str*) get_memory(sizeof(my_str));
str->c_str = c_str;
str->length = length;
return str;
}
关于c - 在 C 中操作字符串时,仅在发布配置中出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37724071/