15,473
社区成员




#ifdef _WIN64
return HeapAlloc(_crtheap, 0, size ? size : 1);
#else /* _WIN64 */
if (__active_heap == __SYSTEM_HEAP) {
return HeapAlloc(_crtheap, 0, size ? size : 1);
} else
if ( __active_heap == __V6_HEAP ) {
if (pvReturn = V6_HeapAlloc(size)) {
return pvReturn;
}
}
#ifdef CRTDLL
else if ( __active_heap == __V5_HEAP )
{
if (pvReturn = V5_HeapAlloc(size)) {
return pvReturn;
}
}
#endif /* CRTDLL */
if ( (_crtheap = HeapCreate( mtflag ? 0 : HEAP_NO_SERIALIZE,
BYTES_PER_PAGE, 0 )) == NULL )
return 0;
intptr_t __cdecl _get_heap_handle(void)
{
_ASSERTE(_crtheap);
return (intptr_t)_crtheap;
}
#include <stdio.h>
#include <windows.h>
#include <iostream.h>
#include <process.h>
#define UM_MSG1 WM_USER+1
#define UM_MSG2 WM_USER+2
DWORD WINAPI Thread1(LPVOID para)
{
DWORD dwThreadId = *(DWORD *)para;
DWORD i=0;
TCHAR *p;
char strTmp[100];
while(TRUE)
{
Sleep(1700);
p=new TCHAR[10];
sprintf(strTmp,"Hello %d %x",i++,p);
PostThreadMessage(dwThreadId,UM_MSG1,(WPARAM)strTmp,(LPARAM)p);
}
return 0;
}
DWORD WINAPI Thread2(LPVOID para)
{
char strTmp[100];
DWORD dwThreadId = *(DWORD *)para;
DWORD i=0;
TCHAR *p;
while(TRUE)
{
Sleep(3000);
p=new TCHAR[10];
sprintf(strTmp,"World %d %x",i++,p);
PostThreadMessage(dwThreadId,UM_MSG2,(WPARAM)strTmp,(LPARAM)p);
}
return 0;
}
int main()
{
DWORD dwValue =GetCurrentThreadId();
HANDLE hThread1 = CreateThread(NULL,0,&Thread1,&dwValue,0,NULL);
HANDLE hThread2 = CreateThread(NULL,0,&Thread2,&dwValue,0,NULL);
CloseHandle(hThread1);
CloseHandle(hThread2);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
switch(msg.message)
{
case UM_MSG1:
case UM_MSG2:
printf("msg:0x%x %s %x\n",msg.message,msg.wParam,msg.lParam);
delete (TCHAR *)msg.lParam; //注释掉这句你就会看到堆内存地址变化
break;
default:
printf("Unknown msg:0x%x\n",msg.message);
break;
}
//Sleep(1);
}
return 0;
}
TCHAR* text=new TCHAR[10];
::PostThreadMessage(g_ThreadID_A,WM_USER + 130,0,(LPARAM)text);
TCHAR* p_ch=(TCHAR*)msg.lParam;
delete []p_ch;