可能重複:
determine size of dynamically allocated memory in c
newbie questions about malloc and sizeof
How can I get the size of an array from a pointer in C?如何獲得動態分配內存的大小(使用malloc或calloc)?
int **arrofptr;
arrofptr = (int **)malloc(sizeof(int *) * 2);
arrofptr[0] = (int *)malloc(sizeof(int)*6144);
arrofptr[1] = (int *)malloc(sizeof(int)*4800);
現在我已經知道了多少字節arrofptr分配,arrofptr [0] ,arrofptr [1]?有什麼方法可以知道尺寸嗎?
如果我們將打印
sizeof(arrofptr);
sizeof(arrofptr[0]);
sizeof(arrofptr[1]);
然後將打印4
這是特定的實現。例如,當你在Linux內核中時,你可以通過kmalloc獲得這些信息:'stuff = kmalloc(1,GFP_KERNEL); printk(「我得到了:%zu字節的內存\ n」,ksize(stuff));'顯然只能在Linux內核中工作,您需要獲得一個與您正在使用的malloc實現綁定的答案。 – Mike