1

Possible Duplicates:
How can I get the size of an array from a pointer in C?
Any tool to find size of memory allocated dynamically using malloc/realloc?

How can I get the size of a pointer of a segment of memory, mallocked by function malloc, if only given a pointer

???

Community
  • 1
  • 1
StevenWang
  • 3,625
  • 4
  • 30
  • 40
  • there isn't a portable way of doing it. – lijie Nov 29 '10 at 06:16
  • Why do you need it? If you tell the reason, probably people at SO might be able to suggest a solution for you. – Jay Nov 29 '10 at 06:17
  • 1
    Many dups ... searching ... http://stackoverflow.com/questions/2373749/any-tool-to-find-size-of-memory-allocated-dynamically-using-malloc-realloc ... http://stackoverflow.com/questions/3886539/how-to-find-how-much-space-is-allocated-by-a-call-to-malloc ... http://stackoverflow.com/questions/2478240/how-i-return-the-size-of-the-pointer-that-i-have-allocate-with-malloc – dmckee --- ex-moderator kitten Nov 29 '10 at 06:20
  • -1 for not searching before posting. – Nyan Nov 29 '10 at 06:23
  • And there are others, but I ran out of editing time on my comment. Here's a pretty early one: http://stackoverflow.com/questions/232691/how-can-i-get-the-size-of-an-array-from-a-pointer-in-c . Surprisingly few pointers between various instances for this question. – dmckee --- ex-moderator kitten Nov 29 '10 at 06:26
  • 1
    @Nyan: I, too, suspect that Macroideal didn't search, but you can't know for certain. You'll note how different the titles of the duplicates I found are. – dmckee --- ex-moderator kitten Nov 29 '10 at 06:30

5 Answers5

2

Here is an example:

typedef struct _BlockHeader {
    int Size;
} BlockHeader;

void *MyMalloc(int size) {
    char *ptr = malloc(size + sizeof(BlockHeader));
    BlockHeader *hdr = (BlockHeader*)ptr;
    hdr->Size = size;
    return (ptr + sizeof(BlockHeader));
}

void MyFree(void *ptr) {
    char *myptr = (char*)ptr;
    free(myptr - sizeof(BlockHeader));
}

int GetPtrMallocedSize(void *ptr) {
    char *myptr = (char*)ptr;
    BlockHeader *hdr = myptr - sizeof(BlockHeader);
    return (hdr->size);
}

Pratically each memory block starts with an header, collecting memory block information. Of course also realloc and calloc shall be implemented, but memory block cannot be used by other software which expect "normal" malloced memory.

Another way to do this is to hash the pointer, and collect memory information by hooking the malloc/free functions.

I know that MS CRT uses this to keep track of heap overflow and memory leaks. I don't know if there are some sideeffect... actually I've never used it.

Luca
  • 11,646
  • 11
  • 70
  • 125
1

You cannot, at least not without knowing the internals of your implementations version of malloc and how it's data structure of allocated blocks looks like.

SiegeX
  • 135,741
  • 24
  • 144
  • 154
1

You can't. Since you allocated the memory in the first place, you're responsible for keeping track of what and how much you allocated.

casablanca
  • 69,683
  • 7
  • 133
  • 150
0

You get the size of the pointer p with sizeof(p).

If you ask for the size of the memory chunk, that is used to fulfill the malloc call, this might be version specific.

harper
  • 13,345
  • 8
  • 56
  • 105
0

There is no standard for this, so its implementation specific. On windows however, you can use _msize.

Necrolis
  • 25,836
  • 3
  • 63
  • 101