We help IT Professionals succeed at work.
Get Started

Why does HeapAlloc() allocates more memory than I ask it to under Vista, but not under XP?

meadowsps
meadowsps asked
on
2,498 Views
Last Modified: 2013-11-27
Hi,

We have some DLL code that we developed which has been running for years under XP with no problem. The basic problem seems to be that the routine HeapSize() returns a different value under Vista than it did under XP, or that HeapAlloc() allocates more than the number of bytes specified, we are not sure which one.

You see, in our code, we often allocate zero-length pointers that are destined to be filled with data later on. We rely upon the call to HeapSize() to return the correct size of the allocated pointer, and if the pointer is a zero-length pointer, then we expect a zero value to be returned so we know the buffer has yet to be filled with any data.

On Windows XP, this has worked flawlessly. However, on Vista, it seems that when allocating a zero-length pointer, the value returned from HeapSize() is often >0, typically it is a '1'.

Here is a small snippet of sample code:

            unsigned char * dataPtr = 0;
            int32 pointerSize = 0;
           
            dataPtr = (unsigned char *) HeapAlloc(GetProcessHeap(), 0, 0);
            pointerSize = HeapSize( GetProcessHeap(), 0, dataPtr );
           
In the above series of calls, the pointerSize value is returned as a  '1', even though the allocated memory was specified to be zero.

We have tried explicitly setting the length of the ponter to zero immediately after the call to HeapAlloc(), and then checking the size. In this case, the really odd thing is on the first call to HeapSize(), the size is correctly returned as a 0. However, all subsequent calls to HeapSize return a value of '8'.
           
We have no explanation for the behavior, other than perhaps some strange Vista issue related to zero-length pointers. The documentation for HeapAlloc() says that it will allocate "at least" the number of bytes requested. Not sure what that means. Does this mean you cannot rely upon the allocated block being exactly the same as the requested size? That might explain what is going on.

Thanks for your help... JK
Comment
Watch Question
Senior Software Engineer (Avast)
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 12 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE