求教!!!!!visual studio:写入位置 0x3e800000 时发生访问冲突

ljd008 2011-05-31 10:35:49
运行时出现:nsga2.exe 中的 0x00402f19 处未处理的异常: 0xC0000005: 写入位置 0x3e800000 时发生访问冲突。都说是指针问题,可是我就是看不出来哪的问题啊,代码贴上,在线等指导,非常感谢!!!!!!
/*This is the program used to evaluate the value of the function & errors
************************************************************************/

void func(population *pop_ptr);

void func(population *pop_ptr)
{

float *realx_ptr, /*Pointer to the array of x values*/
*binx_ptr, /* Pointer to the binary variables */
/*Pointer to the array of fitness function*/
x[2*maxvar], /* problem variables */
/*array of fitness values*/
*err_ptr, /*Pointer to the error */
cstr[maxcons];
int *gene_ptr;
float *fitn_ptr, f[maxfun];

int i,j,k,o;
float error, cc;
double Ec=32; //给定T,Q,C,R的极限值
double Eq=0.3; //给定T,Q,C,R的极限值
double Er=0.3; //给定T,Q,C,R的极限值
double Et=40; //给定T,Q,C,R的极限值

pop_ptr->ind_ptr= &(pop_ptr->ind[0]);

/*Initializing the max rank to zero*/
pop_ptr->maxrank = 0;
for(i = 0;i < popsize;i++)
{
pop_ptr->ind_ptr = &(pop_ptr->ind[i]);
realx_ptr = &(pop_ptr->ind_ptr->xreal[0]);
gene_ptr = &(pop_ptr->ind_ptr->genes[0]);

for(j = 0; j < nvar; j++)
{ // Real-coded variables
x[j] = *realx_ptr++; //每个个体
}

for(j = 0; j < chrom; j++)
{ // Binary-codced variables
x[nvar+j] = *gene_ptr++; ////指向每个基因
}

fitn_ptr= &(pop_ptr->ind_ptr->fitness[0]);
err_ptr = &(pop_ptr->ind_ptr->error);




f[0]=0;
f[1]=0;
f[2]=0;
f[3]=0;
for(o=0;o<chrom;o++)
{
if(x[o]==1)
{
f[0]+=T[o]+transT[o];
f[1]+=Q[o]*q[o];
f[2]+=C[o]+transC[o];
if(f[3]<R[o])
{ f[o]=R[o];}

}


}
f[0]=(f[0]/Et);
f[1]=(f[1]/Eq);
f[2]=(f[2]/Ec);
f[3]=(f[3]/Er);



for(k = 0 ; k < nfunc ;k++)
{
*(fitn_ptr+k)=f[k]; !!!!!!!!!!!!!!!!!!运行时候就停在这一行

}

for (k = 0;k < ncons; k++)
{
pop_ptr->ind_ptr->constr[k] = cstr[k];
}
error = 0.0;
for (k = 0;k < ncons;k++)
{
cc = cstr[k];
if(cc < 0.0)
error = error - cc;
}
*err_ptr = error;
}

/*---------------------------* RANKING *------------------------------*/

if(ncons == 0)
ranking(pop_ptr);
else
rankcon(pop_ptr);

return;


}





...全文
709 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljd008 2011-05-31
  • 打赏
  • 举报
回复
可是。。。。这该怎么看啊,请大虾指点一二。。。。
[Quote=引用 9 楼 ljt3969636 的回复:]

应该是数组越界,你看看崩溃时k的取值
[/Quote]
ljt3969636 2011-05-31
  • 打赏
  • 举报
回复
应该是数组越界,你看看崩溃时k的取值
ljd008 2011-05-31
  • 打赏
  • 举报
回复
您说的很有帮助,现在我把double都改为float ,可是运行结果还是一样的,这是为什么呢?我把反汇编贴上:
for(k = 0 ; k < nfunc ;k++)
00402ED9 mov dword ptr [k],0
00402EE3 jmp func+314h (402EF4h)
00402EE5 mov eax,dword ptr [k]
00402EEB add eax,1
00402EEE mov dword ptr [k],eax
00402EF4 mov eax,dword ptr [k]
00402EFA cmp eax,dword ptr [_nfunc (0ADE260h)]
00402F00 jge func+340h (402F20h)
{
*(fitn_ptr+k)=f[k];
00402F02 mov eax,dword ptr [k]
00402F08 mov ecx,dword ptr [fitn_ptr]
00402F0E mov edx,dword ptr [k]
00402F14 fld dword ptr f[edx*4]
00402F1B fstp dword ptr [ecx+eax*4] 还是停在这了!!!!

}

[Quote=引用 7 楼 ljt3969636 的回复:]

先说一个问题哈
float *fitn_ptr是float *
double fitness[maxfun];fitness是double类型

fitn_ptr= &(pop_ptr->ind_ptr->fitness[0]);

float *指向double,那么一般float是四字节,double是8字节

*(fitn_ptr+k)=f[k];这里将是用四字节的……
[/Quote]
ljt3969636 2011-05-31
  • 打赏
  • 举报
回复
先说一个问题哈
float *fitn_ptr是float *
double fitness[maxfun];fitness是double类型

fitn_ptr= &(pop_ptr->ind_ptr->fitness[0]);

float *指向double,那么一般float是四字节,double是8字节

*(fitn_ptr+k)=f[k];这里将是用四字节的步长和取值,去赋值一个8字节的东西的中的四字节~~
CrackValue 2011-05-31
  • 打赏
  • 举报
回复
2楼对的,
ljd008 2011-05-31
  • 打赏
  • 举报
回复
而且 指针变量显示:fitn_ptr= CXX0030: 错误: 无法计算表达式的值
这是怎么回事啊??求指导
[Quote=引用 2 楼 luciferisnotsatan 的回复:]

0xC0000005这个错误大多是 指针问题,数组越界造成的。
单步调试下
[/Quote]
ljd008 2011-05-31
  • 打赏
  • 举报
回复
申请了啊,指向两个结构体变量;
typedef struct /*individual properties*/
{
int genes[maxchrom], /*bianry chromosome*/
rank, /*Rank of the individual*/
flag; /*Flag for ranking*/
float xreal[maxvar], /*list of real variables*/
xbin[maxchrom]; /*list of decoded value of the chromosome */
double fitness[maxfun];/*Fitness values */
float
constr[maxcons], /*Constraints values*/
cub_len, /*crowding distance of the individual*/
error; /* overall constraint violation for the individual*/
}individual; /*Structure defining individual*/


typedef struct
{
int maxrank; /*Maximum rank present in the population*/
float rankrat[maxpop]; /*Rank Ratio*/
int rankno[maxpop]; /*Individual at different ranks*/
individual ind[maxpop], /*Different Individuals*/
*ind_ptr;
}population ; /*Popuation Structure*/

[Quote=引用 1 楼 ljt3969636 的回复:]

为pop_ptr->ind_ptr->fitness[0]中的fitness[0]申请空间了吗??
[/Quote]
ouyh12345 2011-05-31
  • 打赏
  • 举报
回复
*(fitn_ptr+k)=f[k]; !!!!!!!!!!!!!!!!!!运行时候就停在这一行

崩溃后,选重试,查看调用堆栈,回溯到此行后,看变量的值
luciferisnotsatan 2011-05-31
  • 打赏
  • 举报
回复
0xC0000005这个错误大多是 指针问题,数组越界造成的。
单步调试下
ljt3969636 2011-05-31
  • 打赏
  • 举报
回复
为pop_ptr->ind_ptr->fitness[0]中的fitness[0]申请空间了吗??
如此美丽的你 2011-05-31
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 luciferisnotsatan 的回复:]
0xC0000005这个错误大多是 指针问题,数组越界造成的。
单步调试下
[/Quote]
++
赵4老师 2011-05-31
  • 打赏
  • 举报
回复
崩溃的时候进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧