The problems in your add_device function stem from incorrect usage of pointers and passing variables by value instead of by reference. Here's a detailed explanation of the issues and how to fix them:
Issues in Your Code:
-
Incorrect Assignment to udevs After realloc:
udevs = temp;
-
Problem: udevs is a triple pointer libusb_device ***udevs. Assigning temp (which is a libusb_device **) to udevs modifies the local copy of the pointer in the function, not the original pointer in the caller. This means the caller's udevs won't reflect the new allocation.
-
Fix: You need to dereference udevs once to assign the new array back to the caller's udevs.
*udevs = temp;
-
Incorrect Access When Adding the New Device:
udevs[num_udevs] = new_device;
-
Problem: udevs is a triple pointer, so udevs[num_udevs] doesn't point to the correct location where you can store new_device.
-
Fix: Dereference udevs once to access the correct array level.
(*udevs)[num_udevs] = new_device;
-
Incrementing num_udevs Passed by Value:
(num_udevs)++;
-
Problem: num_udevs is passed by value, so incrementing it inside the function doesn't affect the variable in the caller.
-
Fix: Pass num_udevs as a pointer to modify its value in the caller.
-
Change the function parameter to accept a pointer:
static int add_device(libusb_device ***udevs, int *num_udevs, libusb_device *new_device)
-
Increment the value pointed to by num_udevs:
(*num_udevs)++;
-
No Return Statement Despite Non-void Return Type:
- Problem: Your function is declared to return
int, but it doesn't return any value, leading to undefined behavior.
- Fix: Return an appropriate value, such as
0 on success or -1 on failure.
Corrected Code:
static int add_device(libusb_device ***udevs, int *num_udevs, libusb_device *new_device)
{
printf("Count of devices before adding: %d\n", *num_udevs);
// Reallocate the array to hold one more device
libusb_device **temp = realloc(*udevs, (*num_udevs + 1) * sizeof(libusb_device *));
if (temp == NULL) {
printf("Out of memory\n");
return -1; // Indicate failure
}
// Assign the reallocated array back to udevs
*udevs = temp;
// Add the new device to the array
(*udevs)[*num_udevs] = new_device;
// Increment the device count
(*num_udevs)++;
printf("Count of devices after adding: %d\n", *num_udevs);
return 0; // Indicate success
}
Explanation:
-
Passing num_udevs by Reference:
- By changing the parameter to
int *num_udevs, you allow the function to modify the caller's variable directly.
- When you increment
(*num_udevs)++, it updates the variable in the caller's scope.
-
Proper Usage of Pointers:
*udevs dereferences the triple pointer to access the double pointer (the array of device pointers).
(*udevs)[*num_udevs] accesses the array at the position *num_udevs.
-
Memory Allocation and Error Handling:
- Always check the result of
realloc to ensure memory allocation was successful.
- Return an error code if
realloc fails.
Usage Example:
Here's how you might call the add_device function:
int main() {
libusb_device **udevs = NULL;
int num_udevs = 0;
libusb_device *new_device = /* Assume this is initialized elsewhere */;
if (add_device(&udevs, &num_udevs, new_device) != 0) {
// Handle error
}
// Use udevs and num_udevs...
// When done, free the allocated memory
free(udevs);
return 0;
}
Additional Tips:
-
Initialize Pointers to NULL:
- When you start with
udevs = NULL, realloc behaves like malloc.
-
Free Memory Appropriately:
- Ensure you
free any allocated memory when it's no longer needed to avoid memory leaks.
-
Consistent Error Handling:
- Decide on a consistent way to handle errors (e.g., returning
-1 or using errno) and document it.
Summary:
- Dereference Pointers Correctly: Always make sure you're accessing the correct level of indirection when dealing with multiple levels of pointers.
- Pass Variables by Reference if They Need to Be Modified: If a function needs to modify a variable from the caller, pass it as a pointer.
- Check Return Values for Functions That Can Fail: Functions like
realloc can fail; always handle such cases.
- Return Values Appropriately: Ensure your function returns a value if it's declared to do so.
By making these corrections, your function should work as intended, adding a new device to the array and updating the device count in the caller.