-1

What does the following exactly mean?

void *malloc(size_t size);
int *p=(int*)malloc(23);

Does it mean that the OS allocates a memory of 23 bytes to int(though it requires just 4 bytes)? I know that to make malloc platform independent, we use sizeof, but what if we use the above syntax?

Also, even after writing malloc as above, when I type printf("%d",sizeof(*p)), I get the answer as 4. Why? Should'nt it return 23 since that's what the memory I allocated?

msc
  • 33,420
  • 29
  • 119
  • 214
Ananya
  • 35
  • 1
  • 6
  • 1
    23 bytes for an integer array it makes no sense... – Jean-François Fabre Apr 11 '17 at 12:14
  • 5
    Possible duplicate of [How sizeof(array) works at runtime?](http://stackoverflow.com/questions/10078283/how-sizeofarray-works-at-runtime) – Jean-François Fabre Apr 11 '17 at 12:14
  • 2
    `p` is `int*`, so `*p` is `int`. The size of an `int` often is `4` bytes. All good. :-) – alk Apr 11 '17 at 12:17
  • Also in C there is not need to cast the result of `malloc()`. C isn't C++. – alk Apr 11 '17 at 12:18
  • This looks very wrong. 1) Don't cast `void *` as returned by `malloc` & friends. 2) `23` bytes is a very uncommon size for an array of `int`. Even if `sizeof(int) == 1`, never rely on it. Only omit the element-size for `char` arrays. – too honest for this site Apr 11 '17 at 12:28
  • [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Apr 11 '17 at 12:38
  • first of all , thank u all,as I have never get such a huge response for any question. :) But want know why 23 byte not possible? It is a valid integer – Ananya Apr 11 '17 at 12:53
  • Another request to seniors , plz reduce the casting -ve vote for new learners. As this hampers learning them new things. Just a request :) – Ananya Apr 11 '17 at 12:59

4 Answers4

3

When you call malloc(23), it will return at least 23 bytes (maybe more).

What you use that space for is entirely up to you. You want to put an int in it? That is your business, but you wasted 19 bytes. You want to put a 22-byte string there? Fine.

sizeof measures the size of the TYPE you gave it. It does not know how much memory may be allocated behind it. So when you call sizeof(*p), it sees that *p has type int, and the size of an int is 4, so the result is 4.

There is no portable, widely accepted way to know how much memory has been allocated to a pointer.

abelenky
  • 63,815
  • 23
  • 109
  • 159
1

malloc allocates at least the number of bytes you pass as parameter, i.e. at least 23 bytes in your case, and it returns a pointer to the first byte of this allocated memory block (or NULL, if the memory could not be allocated at all).

The type of the pointer, e.g. int* then tells the compiler how to interpret the content of the memory block to which this pointer points to, regardless of the size you have actually allocated. If you have a pointer of type int*, then the content of the memory block is interpreted as an int, and the size of an int value (which is the outcome of *p, i.e. dereferencing a pointer to an int), is 4 in your architecture.

Note that a pointer is just a value representing a memory address, and it does not contain any information of how large this memory block has been allocated by you (and actually there is no way of finding out this allocated size when having only the pointer).

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
  • That's not complete. Relevant for the type of the object is the type of the first write-access. After that, the object's type is fixed and no other type may be used to access it (there is an exception for `char` type, but tl for a comment). – too honest for this site Apr 11 '17 at 12:35
  • @ Olaf Can u be a bit clear? Do u mean if malloc first do write access then the size of memory is fixed? – Ananya Apr 11 '17 at 13:33
0

Pointers returned by malloc() do not "carry with them" any information about the size of the original allocation. They are just pointers, all they can say is an address in memory. Nothing more.

And your code is fine, assuming of course that sizeof (int) <= 23 is true, which it will be for typical platforms.

It wastes memory, and is extremely confusing to anyone reading the code (because: what's the point?!) but it will work and be correct according to the language's specification.

I would recommend the following usage:

int *p = malloc(sizeof *p);

This:

  • Drops the pointless cast.
  • Only allocates the amount that is needed for a single int.

Note that this (like any call to malloc()) will likely return more than sizeof (int) bytes of usable memory, but you're not allowed to step outside the amount you asked for so that doesn't matter.

unwind
  • 391,730
  • 64
  • 469
  • 606
0

If you expect p to point to a single integer, 23 bytes more than enough for all known implementations. If on the other hand you're allocating space for an array of int, you may not have as much as you think.

Assuming an int is 4 bytes, you have space for 5 plus 3 extra bytes.

As for the sizeof operator, it known nothing of dynamically allocated memory. It only sees the type of the given expression. Since *p is an int, it will always return 4 (again, assuming an int is 4 bytes on your platform.

dbush
  • 205,898
  • 23
  • 218
  • 273