<分区>
我有以下功能:
void print_out_str(FILE* outstream, UINT x, int n) {
assert(n >= 1);
int n1, n2, j;
n1 = (n - 1)/64; n2 = n % 64;
for(j = 0; j <= n1 - 1; j++)
fprintf(outstream,"%016llx ",x[j]); //padding with 0
fprintf(outstream,"%016llx ",x[n1] & ((1ULL<<n2) - 1ULL));
“outstream”是我想打印的地方,UINT
是 uint64_t*
的类型定义,n
是一些我要打印的位。
我不明白为什么,但每次我尝试调用此类函数时,程序都会因段错误而崩溃。我已经尝试使用 GDB 来了解变量内容 (outstream, x, n
) 是否符合我的预期并且没问题。具体来说,作为一个简单的例子,我尝试了一个包含两个元素的数组,n = 87 和 x[0] = x[1] = 0,outstream = stdout。
我错过了什么吗?
更新:
更多信息...
这是我在提到的函数之前调用的代码:
void test_set_bit() {
int nbits_to_allocate, i;
UINT x;
printf("Set the size of your x var: "); scanf("%d",&nbits_to_allocate);
init_ui(x,nbits_to_allocate);
printf("Set the specific bit would you like to set: "); scanf("%d",&i);
set_ui_zero(x,nbits_to_allocate);
printf("Content before the bit set:\n");
print_out_str(stderr,x,nbits_to_allocate);
//Neglect the following three lines...
//set_ui_bit(x,nbits_to_allocate,i);
//printf("Content after the bit set:\n");
//print_out_str(stderr,x,nbits_to_allocate);
}
init_ui
在哪里
void init_ui(UINT x, int n) {
assert(n >= 1);
int N;
N = (n + 63)/64;
x = (UINT)malloc(N*sizeof(uint64_t));
}