Before starting I know that there has been quite a lot of questions about this and I hope that my question is not redundant. I have read quite a lot of things on the internet, and I have a specific question. When allocating memory in C language, what is the best way to allocate memory.
So imagine I want to allocate a int* nb
, so is there a better way to allocate the memory?
First solution I have read:
nb=malloc(sizeof *nb);
Second solution I have read:
nb=malloc(sizeof(nb));
Third solution I have read:
nb=malloc(sizeof(int*));
Th reason I am asking this is because I have read on the internet, all three solutions, and if I understood well, that the allocation size may differ depending the system you are on, so the reason for using sizeof(nb)
, which may allocate more memory than sizeof(int)
. So am I wrong ?
[EDIT]
The aim here is mostly to allocate an array of arbitrary size
[/EDIT]
Thanks for any help and again, hoping my question is not redundant