I need to open multiple shared libraries at run time. I don't know their number(count), so I am using dynamic memory allocation:
void* handle= new void* [n]; // n refers to number of handles
handle[0] = dlopen("./fileN.so", RTLD_LAZY); // doesn't work : Error: ‘void*’ is not a pointer-to-object type
However if I do static allocation, it works-
void* handle[10];
handle[0] = dlopen("./file0.so", RTLD_LAZY); // works
Why is that when I am dynamically accessing the handle, I am getting error? and how do I fix it?
You need an extra level of indirection. A pointer to a pointer:
void** handle= new void* [n];
^
Your code would be invalid on another type:
int* handle= new int* [n]; // error assigning int** to int*
But it works with void*
as void*
can point to void**
.
您需要将handle
声明为void**
,而不是void*
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.