读书人

新人贴~C语言关于指针释放有关问题

发布时间: 2012-04-17 15:06:33 作者: rapoo

新人贴~~!C语言关于指针释放问题
问题是这样的,我对pYaw指针申请完内存空间后,在函数的末尾释放该内存空间,结果出现了意想不到的debug error :DAMAGE: after Normal block (#1459) at 0x00387230.

实在想不到哪错了,把这段代码单独提出来测试也是通过的。代码如下:
void AngleSinglStepCount( List *phead, unsigned int step)
{
unsigned int num_of_Yaw = g_NumOfNode;
double * pYaw = NULL;
Node * temp = * phead;

pYaw = ( double* )calloc( num_of_Yaw, sizeof(double) );

for( unsigned int i = 0; i < num_of_Yaw && temp->pNext != NULL; i++ )
{
pYaw[ i ] = temp->item.Rotation.Yaw;
temp = temp->pNext;
}
for( unsigned int j = 0; j < 10; j++ )
{
for( i = 1; i <= num_of_Yaw; i++ )
{
if( pYaw[ i ] - pYaw[ i - 1 ] < -200 )
pYaw[ i ] = 360 + pYaw[ i ];
if( pYaw[ i ] - pYaw[ i - 1 ] > 200 )
pYaw[ i ] = pYaw[ i ] - 360;
}
}
g_StepDirction = (double * )calloc( step, sizeof(double));
for( i = 0 ; i < step; i++ )
{
g_StepDirction[ i ] = pYaw[ g_StepArry[ i + 1 ] + 34 ] - pYaw[ g_StepArry[ 1 ] + 34 ];
printf("\n Step Direction %d: %f ", i, g_StepDirction[ i ] );
}
printf("\n");

free( pYaw );
}

[解决办法]

C/C++ code
for( i = 0 ; i < step; i++ )  {    int i1= g_StepArry[ i + 1 ] + 34;    if (i1<0 || num_of_Yaw<=i1) {printf("i1==%d not in [0..%d] error!\n",i1,num_of_Yaw-1);scanf("%d",&i1);}    int i2= g_StepArry[ 1 ] + 34;    if (i2<0 || num_of_Yaw<=i2) {printf("i2==%d not in [0..%d] error!\n",i2,num_of_Yaw-1);scanf("%d",&i2);}  g_StepDirction[ i ] = pYaw[ i1 ] - pYaw[ i2 ];  printf("\n Step Direction %d: %f ", i, g_StepDirction[ i ] );  } 

读书人网 >C语言

热点推荐