-4

=======================================================================================

HI

If we given a dynamically allocated pointer,

how can we determine the size of allocated memory?

For Ex, int *p = malloc(8); /*How can I get size of allocated memory */

======================================================================================

Nishith Goswami
  • 353
  • 5
  • 13
  • 4
    http://stackoverflow.com/questions/1281686/determine-size-of-dynamically-allocated-memory-in-c – Michael M. Sep 11 '13 at 08:23
  • Here are some duplicate questions: http://stackoverflow.com/questions/232691/how-can-i-get-the-size-of-an-array-from-a-pointer-in-c http://stackoverflow.com/questions/12131925/getting-the-size-of-a-malloc-only-with-the-returned-pointer http://stackoverflow.com/questions/1208644/how-can-i-get-the-size-of-a-memory-block-allocated-using-malloc http://stackoverflow.com/questions/1428679/any-function-to-query-the-size-of-an-allocated-block – nos Sep 11 '13 at 08:25
  • Did you have a specific language in mind? – paxdiablo Sep 11 '13 at 08:29
  • You should always include a language tag, otherwise experts in the language you're talking about may not find their way here, not to mention figuring out that you're actually talking about that language. You should also include some code, even if it seems trivial. – Bernhard Barker Sep 11 '13 at 08:32

1 Answers1

1

You can't, at least not without seriously dirty hacks. You should know how much you allocated, and you shouldn't care about the memory you didn't allocate.

Amadan
  • 191,408
  • 23
  • 240
  • 301