I have the following function:
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));
where "outstream" is where I'd like to print, UINT
is a typedef for uint64_t*
and n
is a number of bits I want to print.
I don't understand why but every time i try to call such function the program crash with a segmentation fault. I've tried with GDB to understand if the variables content (outstream, x, n
) is what i expect and it is fine. Specifically, as simple case i tried with an array of two elements, n = 87 and x[0] = x[1] = 0, outstream = stdout.
Am I missing something?
Update:
Some more info...
Here it is the code i call before the function i mentioned:
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);
}
where init_ui
is
void init_ui(UINT x, int n) {
assert(n >= 1);
int N;
N = (n + 63)/64;
x = (UINT)malloc(N*sizeof(uint64_t));
}
x[j]
– user8469759 Mar 15 '16 at 10:42x
.x
is a pointer,x[j]
is the value pointed at address x + j, since it is allocated there's no problem with that... and i want to print such value, I can't see the problem with that. – user8469759 Mar 15 '16 at 10:46typedef uint64_t* UINT
, and i use it as an array, after dinamic allocation, I still can't see the problem. Please elaborate more. – user8469759 Mar 15 '16 at 10:53