Have you ever wrote a statement like..
int *pnValue = new int[];
One of my friend asked me what will happen if the above statement is executed. Executed ??? I though the code won't even compile. Surprisingly it compile and even returned a pointer. Wow that was some thing unbelievable.
OK now the question is, what will be size of memory that pnValue points?
The two API's in windows that allocates and de allocates the memeory are HeapAlloc and HeepFree. The CRT functions malloc and free are actually wrappers the above API. The new, new[], delete, delete[] are again another wrapper around the malloc and free. So when ever you allocates some memory using new or new[], it will finally reach the HeapAlloc function. This function is defined as
LPVOID WINAPI HeapAlloc(
__in HANDLE hHeap,
__in DWORD dwFlags,
__in SIZE_T dwBytes
);
From the above definition, you can see that the third parameter to this function is the number of BYTES to be allocated . So to find out what "new int[]" returns, we can put a break point in the entry point of HeapAlloc and check the value of dwBytes( in dis assembly ).
when I tried, the dwBytes turned out be 1 !!! This one byte cannot even hold one int variable. That means any further operation using such a pointer will possibly crash.
Another interesting thing is "int *pnValue = new int[0];" also returns a pointer pointing a memory of 1 byte long.
int *pnValue = new int[];
One of my friend asked me what will happen if the above statement is executed. Executed ??? I though the code won't even compile. Surprisingly it compile and even returned a pointer. Wow that was some thing unbelievable.
OK now the question is, what will be size of memory that pnValue points?
The two API's in windows that allocates and de allocates the memeory are HeapAlloc and HeepFree. The CRT functions malloc and free are actually wrappers the above API. The new, new[], delete, delete[] are again another wrapper around the malloc and free. So when ever you allocates some memory using new or new[], it will finally reach the HeapAlloc function. This function is defined as
LPVOID WINAPI HeapAlloc(
__in HANDLE hHeap,
__in DWORD dwFlags,
__in SIZE_T dwBytes
);
From the above definition, you can see that the third parameter to this function is the number of BYTES to be allocated . So to find out what "new int[]" returns, we can put a break point in the entry point of HeapAlloc and check the value of dwBytes( in dis assembly ).
when I tried, the dwBytes turned out be 1 !!! This one byte cannot even hold one int variable. That means any further operation using such a pointer will possibly crash.
Another interesting thing is "int *pnValue = new int[0];" also returns a pointer pointing a memory of 1 byte long.
Hi!
ReplyDeleteFirst I apologize for not commenting on topic.
I've found your article Listing Used Files on CodeProject. Great piece of work.
I don't know much about c++, mostly I build stuff in c#, but I am in need of something like this.
I would like a dll where I would send in processID and it would return the list of files which that process has opened.
I think you could easily change your code to suit my needs. This should work for xp and beyond (32 an 64 bit). I have tried your demo dll but it doesn't work on vista 64. It works great on xp 32 though.
Of course I would be willing to pay you for your work.
If you are interested please send me an email to andrej(dot)fiser(at)gmail(dot)com
And feel free to delete this comment. Thanks.