关于C语言中回调函数的问题!!
路飞-总裁 2009-10-15 06:20:34 纯C,VS2005下运行如下代码:(其中函数musicopentimer也为静态的)
static void MediaNotify(void * pUser, AEEMediaCmdNotify * pCmdNotify)
{
CApp * pMe = (CApp *)pUser;
if (pCmdNotify->nCmd != MM_CMD_PLAY)
return;
switch(pCmdNotify->nStatus) {
case MM_STATUS_START:
break;
case MM_STATUS_ABORT:
case MM_STATUS_DONE:
if(IMEDIA_Play(pMe->m_pMedia) != SUCCESS)
{
ISHELL_SetTimer(pMe->a.m_pIShell,0,(PFNNOTIFY)musicopentimer,pMe);
}
else
{
ISHELL_CancelTimer(pMe->a.m_pIShell,(PFNNOTIFY)musicopentimer,pMe);
}
}
}
以上代码运行时一切正常,但我在程序中加入如下代码,运行至该函数尾时会报如下错误,
Run-Time Check Failure #2 - Stack around the variable 'nVolume' was corrupted.
static void MediaNotify(void * pUser, AEEMediaCmdNotify * pCmdNotify)
{
CApp * pMe = (CApp *)pUser;
if (pCmdNotify->nCmd != MM_CMD_PLAY)
return;
switch(pCmdNotify->nStatus) {
case MM_STATUS_START:
break;
case MM_STATUS_ABORT:
case MM_STATUS_DONE:
if(IMEDIA_Play(pMe->m_pMedia) != SUCCESS)
{
ISHELL_SetTimer(pMe->a.m_pIShell,0,(PFNNOTIFY)musicopentimer,pMe);
}
else
{
ISHELL_CancelTimer(pMe->a.m_pIShell,(PFNNOTIFY)musicopentimer,pMe);
}
}
{
uint16 nVolume;
int nError;
nError = IMEDIA_GetVolume(pMe->m_pMedia, &nVolume);
DBGPRINTF_FATAL("nError = %d, nVolume = %d", nError, nVolume);
}
}
根据错误提示,我在变量nVolume和nError之前分别加上了static,这样运行的时候就没有问题了。
上面两个变量加上static之后该变量是在程序运行前分配空间,并不是函数中的临时变量。
请问以上两个变量不能使用局部非静态的临时变量是因为该函数是静态函数的原因还是回调函数的原因呢?为啥不能直接使用?