- 論壇徽章:
- 0
|
本來想用結(jié)構(gòu)指針獲取某處已經(jīng)分配了內(nèi)存的塊數(shù)據(jù).
結(jié)果發(fā)現(xiàn)返回的指針為空, 百思不得其解, 特求助.
程序如下:typedef struct Blk
{
int aa;
int dd;
}BLOCK, *pBLOCK;
pBLOCK blk = (pBLOCK)malloc(sizeof(BLOCK));
int test(BLOCK *out)
{
out = blk;
printf("\n source pointer %p", out);
printf("\n 0x%x", out->aa);
printf("\n 0x%x", out->dd);
return 0;
}
// 唯一的應(yīng)用程序?qū)ο?br />
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// 初始化 MFC 并在失敗時顯示錯誤
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: 更改錯誤代碼以符合您的需要
_tprintf(_T("錯誤: MFC 初始化失敗\n"));
nRetCode = 1;
}
blk->aa =0x1234;
//blk->bb = L"first";
//blk->cc = new CString(L"second");
blk->dd = 0x4321;
pBLOCK out=NULL;
test(out);
printf("\n when out, pointer:%p", out);
printf("\n gloabl pointer:%p", blk);
return nRetCode;
}
|
運(yùn)行結(jié)果:
source pointer 003AF8B8
0x1234
0x4321
when out, pointer:00000000
gloabl pointer:003AF8B8請按任意鍵繼續(xù). . . |
|