1

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

  • I always go for third one... as it is more reliable and error free up to my understanding. – someone May 27 '14 at 07:11
  • @z̫͋.. I think third one is `nb=malloc(sizeof(int));` so third one is not incorrect. – someone May 27 '14 at 07:13
  • Your second allocation will allocate memory based on the size of a pointer, not an integer. It is not equivalent to your first and third allocations. – M Oehm May 27 '14 at 07:23
  • It isn't clear whether you want to allocate space for a pointer (must of the time this doesn't make any sense) or for an int. – Lundin May 27 '14 at 07:47

3 Answers3

3
int *IntPtr=NULL;
IntPtr=malloc(100*sizeof(int));

is the same as

int *IntPtr=NULL;
IntPtr=malloc(100*sizeof(*IntPtr));

because IntPtr is of type int* so dereferencing it (i.e.*IntPtr ) leads to type int

On the other hand:

nb=malloc(sizeof(nb)); 

is a bad choice, since the sizeof(nb) is the size of the pointer itself, i.e. the size of the address in memory. On 32 bit systems this is always 4 no matter what the type of nb is.

For dynamic memory allocation, you should check realloc

H_squared
  • 1,251
  • 2
  • 15
  • 32
1

The advantage of the first solution is that you can change the type in one place (e.g. from int* nb to double* nb) and the "malloc()" will automatically be correct for the new type and won't need to be modified.

The advantage of the third solution is that there are more visual hints for the programmer. E.g. if you see nb=malloc(sizeof(int)); you don't need to find the type of nb to determine what it's allocating.

In theory; which is preferred depends on context (which advantage is more important at the time) and the former is more likely to be better. In practice, I have a habit of always doing the latter and then being disappointed that mistakes aren't detected by the compiler's type checking. :-)

Brendan
  • 35,656
  • 2
  • 39
  • 66
0

1) Normally, we declare and statically allocate int variable like below:
int nb;

2) If we want to create an array or for some reasons, we can declare a dynamically allocatable variable (a pointer):
int* nb;

In case 2) we need to allocated a memory block to the declared variable:
nb = (int*) malloc( sizeof(*nb) );

For single int storage we use sizeof(int) or sizeof(*nb).

Because nb is a pointer to int, typeof(*nb) is int.

9dan
  • 4,222
  • 2
  • 29
  • 44
  • 1
    this typecasting is not required here. – someone May 27 '14 at 07:30
  • @someone Not required, but very welcomed by code reviewers and static type analyzers. – 9dan May 27 '14 at 07:37
  • 2
    @9dan No, the explicit cast makes it _harder_ for static analysers to detect bugs in your code. If you are using the wrong type, then the static analyser might tell you. But if you do this cast, you make the job harder for the tool. Also, [this has been debated endlessly on SO](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). It is definitely _not_ welcomed by seasoned code reviewers, quite the contrary. As I wrote in my answer to that FAQ, the explicit cast only shows that the programmer is uncertain about how the C language works. – Lundin May 27 '14 at 07:50
  • @Lundin I confess, it is my C++ habit. I'll read the linked post. Thank you for giving me a chance to review my understanding. – 9dan May 27 '14 at 07:56
  • 1
    In C++ it is indeed both good and required practice, because C++ has a different type system than C. They are different, non-compatible languages. – Lundin May 27 '14 at 07:58