假設我通過Emscripten _malloc
(Javascript)在Javascript中分配一些內存M.我是否允許將M的所有權轉換爲一個封送的C++函數,並在其上調用free
(C++)?Emscripten malloc和免費跨JS和C++
2
A
回答
3
是的。在Emscripten中,malloc的C++版本在JavaScript中轉換爲Module._malloc();同樣,Module._free()與C++的free()相同。
-1
看看這段代碼, 這是library.js一段源代碼 約emscripten
free: function() {
#if ASSERTIONS == 2
Runtime.warnOnce('using stub free (reference it from C to have the real one included)');
#endif
},
爲您免費看到沒有實現 ,但你可以通過下面
exampe釋放char *s1 = (char*) malloc (256);
EM_ASM_INT ({
return _free ($0 );
}, s1) ;
目前工作在這樣 這是一個完整的例子
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <emscripten.h>
int main (void)
{
// ************************************** free do not free
char *s1 = (char*) malloc (256);
strcpy (s1,"Hello\0") ;
puts (s1);
free(s1);
puts(s1);
// ************************************** free do not free
char *s2 = (char*)EM_ASM_INT ({
var p = Module._malloc(256);
setValue (p + 0 , 65 , 'i8') ; // A
setValue (p + 1 , 66 , 'i8') ; // B
setValue (p + 2 , 67 , 'i8') ; // C
setValue (p + 3 , 0 , 'i8') ;
return p ;
} , NULL);
puts(s2);
free(s2); // do not free
puts(s2);
// ************************************** _free do free
/*
EM_ASM_INT ({
return _free ($0 );
}, s1) ;
EM_ASM_INT ({
return _free ($0 );
}, s1) ;
*/
puts(s1);
puts(s2);
char * s3 = (char*) EM_ASM_INT ({
var str = 'ciao' ;
var ret = allocate(intArrayFromString(str), 'i8', ALLOC_NORMAL);
return ret ;
}, NULL ) ;
puts(s3) ;
free(s3); // do not free
puts(s3) ;
// ************************************** _free do free
/*
EM_ASM_INT ({
return _free ($0 );
}, s3) ;
*/
puts(s3) ;
return 0 ;
}
+0
這是不正確的。正如在警告消息中指出的那樣,如果從C.Emscripten調用,如果不使用它,那麼'free'的實際實現將可用。但是,這並不能真正回答提問者的問題。 – camerondm9
相關問題
- 1. C malloc和免費
- 2. C,malloc,免費和自動檢查
- 3. C malloc和免費不工作
- 4. malloc和免費使用
- 5. 結構和malloc()/免費()
- 6. 使用malloc()和免費()
- 7. malloc和免費構造函數和destrtructor
- 8. C編程 - Malloc /免費
- 9. Malloc Realloc免費
- 10. 嵌入式RTOS和使用malloc /免費
- 11. Malloc /免費誤區
- 12. C編程:malloc和循環內部免費
- 13. C malloc和免費函數的奇怪行爲
- 14. 正確使用malloc和免費使用C++指針
- 15. Linux堆結構和與malloc()和免費()行爲
- 16. 免費()和結構用C
- 17. 堆頭和免費()在C
- 18. 基本的malloc /免費
- 19. 新,刪除,malloc,免費
- 20. 免費的Java HTML和JS解析器
- 21. realloc和免費
- 22. 包裝malloc和免費,給分配統計?
- 23. 瞭解malloc和指針增量與免費
- 24. 細化我做malloc和免費的方式
- 25. 崩潰與malloc和Android本機代碼免費
- 26. Malloc和免費的多個數組在組裝
- 27. Malloc和免費的功能(優化你的記憶)
- 28. malloc和免費的D /探戈不釋放內存?
- 29. 版稅免費和免費音效
- 30. 試圖學習正確的內存處理C - malloc,realloc和免費
我對Emscripten沒有任何經驗,但我非常肯定答案是** No **,大寫N.你甚至不應該在獨立編譯的C代碼之間傳遞指針的所有權 - 你永遠不知道哪個使用了運行時版本,並且如果free()和malloc()會使用相同的語言。 – SergeyA