a

a bad topic!

In main mem_handle is never initialised.

mem_handle get value from pci_card_detect

No it does not.

Does or not, use your debugger - gdb to check!
One thing you should pay attention in pci_card_detect:

mem_handle = mmap_device_memory (NULL,
*(inf->BaseAddressSize),
(PROT_READ|PROT_WRITE),
0,
(uint64_t)addr_handle);
if (mem_handle == MAP_FAILED)
===> return;
Here you should return value!!!

No need for a debugger, pci_card_detect() passes mem_handle by value, hence the function cannot return a value into it.

Try:

int pci_card_detect(unsigned device,
unsigned vendor,
unsigned index,
unsigned *pci_handle,
struct pci_dev_info *inf,
uint32_t **dev_handle,
uint32_t **mem_handle)
{

*mem_handle = …

}

if (pci_card_detect (HOTICE_CARD_DEVICE_ID, ICE_CARD_VENDOR_ID, index, &pci_handle, &inf,

Koko is right about the return value. I’m surprise it compiled?
&dev_handle, &mem_handle ) == PCI_YES)

Let me repeat to make this clear.
The program has a programming error.
mem_handle is a pointer variable that never gets initialized.
In the call to pci_card_detect, the uninitialized value of mem_handle is passed.
If pci_card_detect trys to put a value where this pointer is pointing, it will probably cause a sigsegv.
Since pci_card_detect is not passed the address of the pointer variable, there is no
way for it to update it, and this is obviously not the way it works anyway.

If this is not clear, then the problem is likely in understanding how ‘C’ works.
I hope this is helpful.