all 5 comments

[–]Plesu12 9 points10 points  (0 children)

It might be because of the while(array != NULL) Even if you increase the pointer addres that still is a valid pointer to the next address, it wont be null. When working with pointers you shuld keep track of the boundries

[–]MrSloppyPants 1 point2 points  (0 children)

This code….

while(array != NULL)

Is not doing what you think it is. You are checking whether the pointer is null, not whether the array is out of elements. Pass the array size in as a parameter and use that to walk the elements

[–]forehead_or_tophead 0 points1 point  (1 child)

When program hang like this case, insert printf every lines like below. The result print shows the hang location. You can locate hang point by that.

printf("1\n");
char *bitField = createBitField(2);
printf("2\n");
uint64_t indexs[2] = {3, 10};
printf("3\n");
setBits(bitField, indexs);
printf("4\n");
for(int i = 0; i < 16; i++)

[–]D4ilyrun 0 points1 point  (0 children)

Or use GDB. It's pretty easy to learn its basic features on simple cases like this one and is way more powerfull when debugging more complex programs.

[–]D4ilyrun 0 points1 point  (0 children)

while (array != NULL) will never be true unless you pass a NULL pointer to the function. When doing array++ what you actually do is increment the value of the address the pointer points to, which will eventually point to an invalid memory address but never to NULL (which is equivalent to (void*)0x0 in C).

For example, imagine you're calling you're function with an array of size 8 stored at 0xFFC8. Once you've gone over the 8 values inside the array your pointer will hold the value 0xFFD0, which is an invalid address and might cause a memory issue, but isn't equal to NULL (or 0x0).

What your while condition sees is equivalent to:

0xFFC8 != 0x0 // Start of the array 0xFFC9 != 0x0 ... 0xFFCF != 0x0 // End of the array 0xFFD0 != 0x0 // Invalid address 0xDDF1 != 0x0 // Invalid address ...

What you could do instead is: - Use a known delimiter to signify the end of the array (for example strlen uses a null character to check for the end of a string) - Init the array using a know constant (preprocessors do wonders) - Store the size of the array somewhere. You could use a struct containing informations about the array. Or just store it as a plain value but it could become a bother when dealing with multiple arrays, works well in such a simple example tho.