1

What is the difference between the following two statements?

ptr = malloc (400);

and

ptr = malloc (100 * sizeof(int)) 

How does it work? Is there any difference between them?

types of ptr 'int'

sakib
  • 29
  • 6

3 Answers3

1

It depends on your architecture. On a 64-bit machine, the int size should be 8 bytes and 4 bytes on a 32-bit. Though it is not a rule, your 64-bit compiler might register having 4 bytes instead.

So the difference is that the allocated memory might vary depending on the architecture you are targeting and what the compiler decides.

This is also true for the size of your pointers.

millsp
  • 1,259
  • 1
  • 10
  • 23
0

No there should not be any. Malloc allocates contiguous memory chunk in bytes at runtime, so as long as integer takes 4 bytes, then no problem.

You can also refer this, for more clarity.

Also since you did not mention the type of pointer you are using .. the 2 ways makes no difference. Suppose you wanted an integer type then here is an example:

int *ptr;
ptr = malloc(100 * sizeof *ptr);
KL_KISNE_DEKHA_HAI
  • 649
  • 11
  • 26
  • Hello all, why the negative review? am i not clear with the explanation? – KL_KISNE_DEKHA_HAI May 21 '20 at 10:12
  • Well, why are you casting the result? – klutt May 21 '20 at 10:45
  • I suppose you would not write `char c=(char)'a';` or `long x=(long)5;`? – klutt May 21 '20 at 10:59
  • it is well within limits of char and long.. but here it is return void pointer... – KL_KISNE_DEKHA_HAI May 21 '20 at 11:29
  • Are you saying that as long as the implicit casts can be made safely, then there's no need to do explicit casts? Well, that's what I'm saying too. ;) – klutt May 21 '20 at 11:33
  • Ok thanks .. but is my answer wrong according to question? – KL_KISNE_DEKHA_HAI May 21 '20 at 11:37
  • I did not downvote, but I think such unnecessary casts may deserve one. Also, I'm all for advocating `sizeof *ptr` instead of `sizeof int` because it's way superior in almost every way. Other than that, I cannot find anything that is plain wrong, but I realized that this question was more complicated than I first thought. That's why I deleted my answer. – klutt May 21 '20 at 11:43
  • 1
    Not my downvote, but: https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – dbush May 21 '20 at 12:58
  • Thanks man. It surely teaches me a thing. But i also confirmed it using the link which i mentioned, is it fake or something? – KL_KISNE_DEKHA_HAI May 21 '20 at 13:09
  • 2
    While `malloc` does the same thing regardless of how its argument is computed, the fact this is “as long as integer takes 4 bytes” shows that `400` and `100 * size(int)` are not the same thing. They convey different meanings to the reader and will operate differently when `sizeof(int)` is not four. This distinction is important, and students should be taught to pay attention to it, not to think of `400` and `100 * sizeof(int)` as the same. In fact, `sizeof(int)` is itself not usually the best option; `100 * sizeof *ptr` is typically better. – Eric Postpischil May 21 '20 at 13:31
  • Thank you Eric. I am also a college student and did not know about this. I too have been using it as taught to me. Thanks for the correction. Can you check my answer now? and suggest any more edits if required. – KL_KISNE_DEKHA_HAI May 21 '20 at 13:40
0

The first form allocates 400 bytes.

The second form allocates enough space for 100 int objects.

These are not necessarily the same thing, as the size of an int can be 2, 4, or more bytes wide depending on the platform.

If you need to set aside space for N objects of a given type, use the following idiom:

T *ptr = malloc( N * sizeof *ptr ); // for any object type T

sizeof *ptr is equivalent to sizeof (T).

John Bode
  • 119,563
  • 19
  • 122
  • 198