CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 1999
    Location
    Mumbai, India
    Posts
    207

    Post On allocation from the heap

    Hi,

    When we allocate memory from the heap using malloc(), calloc()
    or new, we get a valid pointer to the allocated memory.

    Given this pointer, is it possible to get the number of bytes allocated?

    Thanks in advance.
    Thank you.
    Sayan
    ====================

    Sayan Mukherjee
    [Email: [email protected]]

  2. #2
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664
    There are no standard ways of doing this. Some compilers let you access the internal heap data structure, via some functions, to find out certain stuff, like mem block size etc. In VC6 and VC7, you can use _msize on a block of memory to find out the size of that block. It is not portable code--I would normally not use that way... but you may have good reasons for it. Hope it helps.

  3. #3
    Join Date
    Feb 2002
    Posts
    5,757
    One solution is to determine the size of the object you cast the pointer to. In other words, the size of the allocation depends on the object you cast. Consider sizeof().

    Kuphryn

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by kuphryn
    One solution is to determine the size of the object you cast the pointer to. In other words, the size of the allocation depends on the object you cast. Consider sizeof().

    Kuphryn
    'sizeof()' does not work with dynamically allocated memory...it would return the size of the pointer which is always the same (e.g. 4 bytes on a windows system)...

  5. #5
    Join Date
    Feb 2002
    Posts
    5,757
    Correct. I mentioned the object, not the pointer to it.

    Kuphryn

  6. #6
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    You could always write your own version - say allocate.
    Code:
    void* allocate (int bytes)
    {
        int* result = (int*) malloc (sizeof (bytes) + bytes);
        *result = bytes;
        return (void*)(result + 1);
    }
    
    int allocateSize (void* whatever)
    {
        int* sizeinfo = (int*) whatever;
        sizeinfo--;
        return *sizeinfo;
    }
    You'll have to do similar fiddles for free, calloc and realloc.
    Succinct is verbose for terse

  7. #7
    Join Date
    Dec 2002
    Location
    London, UK
    Posts
    4
    Alignment be damned, eh?

    Don't do that. It's non-portable (not to mention horribly written -- in C, don't cast the return value of malloc(); in C++ don't use malloc()), because you potentially foul up the alignment of the memory. As such, it's a trick that implementations can do, but users cannot.

    You'll have to store the information in a separate data structure to do this safely.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured