2012-01-22 12 views
2

我需要在運行時打開多個共享庫。我不知道它們的數量(計數),所以我使用的動態內存分配:錯誤:'void *'在動態打開多個共享庫時不是指向對象類型的錯誤

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 

但是如果我做靜態分配,其工程─

void* handle[10]; 
handle[0] = dlopen("./file0.so", RTLD_LAZY); // works 

這是爲什麼,當我動態訪問句柄,我收到錯誤?我該如何解決它?

回答

4

您需要額外的間接級別。一個指向指針:

void** handle= new void* [n]; 
    ^

您的代碼將是無效的另一種類型:

int* handle= new int* [n]; // error assigning int** to int* 

但它與void*作爲void*可以指向void**工作。

1

您需要聲明handlevoid**而不是void*

相關問題