gpt4 book ai didi

c - 运行时检查失败 #2 - 变量 'h' 周围的堆栈已损坏

转载 作者:行者123 更新时间:2023-11-30 21:48:43 25 4
gpt4 key购买 nike

/**horn clauses**/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#define NN 10
#define Nc1 4
#define Nc2 2
#define Nc3 4
#define NH 100
#define RELAX 100
#define NT 100
#define COMBMAX 100
#define TOL 0.001
#define TRUE 1
#define FALSE 0
#define NEGBIAS -0.0
#define NCHCHECK 5

void main()
{
int c3[Nc3][3], c2[Nc2][2], c1[Nc1], i, j, k, l, y, z, n, m, NS[NN], Nbef[NN], iev, ievn, p, comb;
int NI[NN], NF[NN];
float zM, yM, nrelaxM, nrelaxMM;
float J1[NN], J2[NN][NN], J3[NN][NN][NN];
float Erg1, Erg2, Erg3, E1, E2, E3;
float devG, devHD, devHG, devHL;
float ES;
float Eav;
float min;
float h[NN];
float EavM;
float varians, variansR;
float sp, spR;
float EM, EAM;
float Hamming_Distance, HDG, HDL, HDGlobal, HDLocal;
int change, nchange, nrelax;
float nR, NRE;

FILE *outf;
srand(time(NULL));
outf=fopen("ss.text", "w");

printf("Ideal energy landscape of horn clauses \n\n");
fprintf(outf, "Ideal energy landscape of horn clauses \n\n");
printf(" no. of neurons : %d\n no. of learning events: %d\n RELAX time: %d\n no. of trials: %d\n COMBMAX: %d\n", NN, NH, RELAX, NT, COMBMAX);
fprintf(outf, " no. of neurons: %d\n no. of learning events: %d\n RELAX time: %d\n no. of trials: %d\n COMBMAX: %d\n", NN, NH, RELAX, NT, COMBMAX);
printf("Nc1= %d Nc2=%d Nc3=%d \n", Nc1, Nc2, Nc3);
fprintf(outf, "Nc1= %d Nc2=%d Nc3=%d \n", Nc1, Nc2, Nc3);
printf("TOL= %f\n", TOL);
fprintf(outf, "TOL= %f\n", TOL);

/**Initialize average energy**/

EavM = 0;
zM = 0;
yM = 0;
ES = -NN*NEGBIAS;
EM = 0;
EAM = 0;
z = 0;
y = 0;
Eav = 0;
HDG = 0;
HDL = 0;
HDGlobal = 0;
HDLocal = 0;
nrelaxM = 0.0;
nrelaxMM = 0.0;
Hamming_Distance = 0;
nR = 0;
NRE = 0;

/**Generating min energy supposed to be**/

/**for first order**/
for(i=0;i<Nc1;i++) { ES+=-0.5; }

/**for second order**/
for(i=0;i<Nc2;i++) { ES+=-0.25; }

/**for third order**/
for(i=0;i<Nc3;i++) { ES+=-0.125; }

printf("ES = %f \n\n", ES);
fprintf(outf, "ES = %f \n\n", ES);

/**loop for random clauses combination**/
for(comb=0; comb<COMBMAX;comb++)
{
/**generate random clauses**/
/**first order**/
for(i=0;i<Nc1;i++) {
stt:
c1[i]=(int)(((float)rand()/(float)RAND_MAX)*(float)NN);
for(j=0;j<i;j++) { if(c1[j]==c1[i]) goto stt; }
}

/**second order**/
for(i=0;i<Nc2;i++) {
stt1:
c2[i][0]=(int)(((float)rand()/(float)RAND_MAX)*(float)NN);
stt11:
c2[i][1]=(int)(((float)rand()/(float)RAND_MAX)*(float)NN);
if(c2[i][0]==c2[i][1]) goto stt11;

for(j=0;j<i;j++) { if(c2[j][0]==c2[i][0] && c2[j][1]==c2[i][1]) goto stt1; }
}

/**third order**/
for(i=0;i<Nc3;i++) {
stt2:
c3[i][0]=(int)(((float)rand()/(float)RAND_MAX)*(float)NN);
stt21:
c3[i][1]=(int)(((float)rand()/(float)RAND_MAX)*(float)NN);
if(c3[i][0]==c3[i][1]) goto stt21;
stt22:
c3[i][2]=(int)(((float)rand()/(float)RAND_MAX)*(float)NN);
if(c3[i][0]==c3[i][2]||c3[i][1]==c3[i][2]) goto stt22;

for(j=0;j<i;j++)
{ if(c3[j][0]==c3[i][0] && ((c3[j][1]==c3[i][1] && c3[j][2]==c3[i][2])|| (c3[j][1]==c3[i][2] && c3[j][2]==c3[i][1])) ) goto stt2; }
}

/**initialize synaptic strengths to 0**/
for(i=0;i<NN;i++)
{ J1[i]=NEGBIAS;
for(j=0;j<NN;j++)
{ J2[i][j]=0.0;
for(k=0;k<NN;k++)
{ J3[i][j][k]=0.0; }
}
}

/**derive J1 for first order**/
for(i=0;i<Nc1;i++)
{ J1[c1[i]]+=0.5; }

/**derive J2 for second order**/
for(i=0;i<Nc2;i++)
{
J2[c2[i][0]][c2[i][1]]+=0.25;
J2[c2[i][1]][c2[i][0]]+=0.25;
J1[c2[i][1]]+=-0.25;
J1[c2[i][0]]+=0.25;
}
/**derive J3 for third order**/
for(i=0;i<Nc3;i++)
{
J3[c3[i][0]][c3[i][1]][c3[i][2]]+=0.0625;
J3[c3[i][0]][c3[i][2]][c3[i][1]]+=0.0625;
J3[c3[i][1]][c3[i][2]][c3[i][0]]+=0.0625;
J3[c3[i][2]][c3[i][1]][c3[i][0]]+=0.0625;
J3[c3[i][2]][c3[i][0]][c3[i][1]]+=0.0625;
J3[c3[i][1]][c3[i][0]][c3[i][2]]+=0.0625;
J2[c3[i][1]][c3[i][0]]+=0.125;
J2[c3[i][0]][c3[i][1]]+=0.125;
J2[c3[i][1]][c3[i][2]]+=-0.125;
J2[c3[i][2]][c3[i][1]]+=-0.125;
J2[c3[i][0]][c3[i][2]]+=0.125;
J2[c3[i][2]][c3[i][0]]+=0.125;
J1[c3[i][2]]+=-0.125;
J1[c3[i][0]]+=0.125;
J1[c3[i][1]]+=-0.125;
}

/**loop to generate random numbers**/
ievn=0;
for(iev=0;iev<NH;iev++)
{ sth:
ievn++;
for(j=0;j<NN;j++)
{ if((float)rand()<(float)RAND_MAX/2.0)
NS[j]=-1;
else NS[j]=+1; }

/**checking clauses satisfaction by events**/
/**for first order**/
for(k=0;k<Nc1;k++)
{ if(NS[c1[k]]==-1) goto sth; }

/**for second order**/
for(k=0;k<Nc2;k++)
{ if(NS[c2[k][0]]==-1 && NS[c2[k][1]]==+1) goto sth; }

/**for third order**/
for(k=0;k<Nc3;k++)
{ if(NS[c3[k][0]]==-1 && NS[c3[k][1]]==+1 && NS[c3[k][2]]==+1) goto sth; }
}

/**looping number of trial**/
for(p=0;p<NT;p++)
{
/**initialize random starting point**/
for(m=0;m<NN;m++)
{ if((float)rand()<(float)RAND_MAX/2.0)
NS[m]=-1;
else NS[m]=+1;
NI[m]=NS[m]; }
/**looping energy relaxation**/
nrelax=0;
nchange=0;
do { /**calculating h**/
change=FALSE;
for(i=0;i<NN;i++)
{ j=((float)NN)*(((float)rand()/(float)RAND_MAX));
h[j]=0;
h[j]+=J1[j];
for(k=0;k<NN;k++)
{ h[j]+=J2[j][k]*NS[k];
for(l=0;l<NN;l++)
{ h[j]+=J3[j][k][l]*NS[k]*NS[l]; }
}
/**McCulloch Pitts updating rule**/
Nbef[j]=NS[j];
if(h[j]>=0) NS[j]=1;
else NS[j]=-1;
if(NS[j]!=Nbef[j]) change=TRUE;
}
if(change==TRUE) nchange=0;
else nchange+=1;
nrelax+=1;
}
while(nchange<NCHCHECK);

/**calculating final state**/
for(m=0;m<NN;m++) { NF[m]=NS[m]; }

/**energy calculation**/
Erg1=0; Erg2=0; Erg3=0;
E1=0; E2=0; E3=0;

for(l=0;l<NN;l++)
{ for(j=0;j<NN;j++)
{ for(k=0;k<NN;k++)
{ E1+=J3[l][j][k]*NS[l]*NS[j]*NS[k];
Erg1=-E1/3;
}
}
}

for(l=0;l<NN;l++)
{ for(j=0;j<NN;j++)
{ E2+=J2[l][j]*NS[l]*NS[j];
Erg2=-E2/2;
Erg2+=Erg1;
}
}
for(l=0;l<NN;l++)
{ E3+=J1[l]*NS[1];
Erg3=-E3;
Erg3+=Erg2;
}

/**calculating global and local minimum**/
if(fabs(ES-Erg3)<=TOL)
{ z+=1;
for(m=0;m<NN;m++) HDG+=0.5*(1-NI[m]*NF[m]);
}
if(fabs(ES-Erg3)>TOL)
{ y+=1;
for(m=0;m<NN;m++) HDL+=0.5*(1-NI[m]*NF[m]);
}

/**calculating EM**/
EM+=Erg3*Erg3;

/**calculating average energy**/
Eav+=Erg3/(float)NT;

/**calculating nR**/
nR+=nrelax*nrelax;

/**calculating relaxation**/
nrelaxM+=(nrelax-NCHCHECK)/((float)NT);
}
}

/**calculating average energy for all the combination**/
EavM+=Eav/(float)COMBMAX;
zM+=(float)z/((float)NT*(float)COMBMAX);
yM+=(float)y/((float)NT*(float)COMBMAX);
EAM+=EM/((float)NT*(float)COMBMAX);
nrelaxMM+=nrelaxM/(float)COMBMAX;
HDGlobal+=HDG/((float)NT*(float)COMBMAX*z);
HDLocal+= HDL/((float)NT*(float)COMBMAX*y);
NRE+=nR/((float)NT*(float)COMBMAX);

/**varians**/
varians=(float)EAM-pow(EavM,2);

/**standard deviation**/
sp=sqrt(varians);

/**calculating varians for relax**/
variansR=(float)NRE-pow(nrelaxMM,2);

/**standard deviation for relax**/
spR=sqrt(variansR);

/**calculating hamming distance**/
if(y==0)
Hamming_Distance+=HDGlobal;
else
Hamming_Distance+=HDGlobal+HDLocal;

/**deviation for hamming distance**/
devHD=sqrt(Hamming_Distance-(Hamming_Distance* Hamming_Distance));

/**deviation for global**/
devG=sqrt(zM-(zM*zM));

/**deviation for HDG**/
devHG=sqrt(HDGlobal-(HDGlobal*HDGlobal));

/**deviation for HDL**/
devHL=sqrt(HDLocal-(HDLocal* HDLocal));

/**printing out clauses(one set only)**/
printf("\n Clauses:\n\n");
fprintf(outf,"\n Clauses:\n\n");

for(i=0;i<Nc1;i++)
{ printf("c1[%d] : %d<-.\n",i,c1[i]);
fprintf(outf,"c1[%d] : %d<-.\n",i,c1[i]);
}

printf("\n");
fprintf(outf,"\n");

for(i=0;i<Nc2;i++)
{ printf("c2[%d] : %d<-%d.\n",i,c2[i][0],c2[i][1]);
fprintf(outf, "c2[%d] : %d<-%d.\n",i,c2[i][0],c2[i][1]);
}

for(i=0;i<Nc3;i++)
{ printf("c3[%d] : %d<-%d,%d.\n",i,c3[i][0],c3[i][1],c3[i][2]);
fprintf(outf, "c3[%d] : %d<-%d,%d.\n",i,c3[i][0],c3[i][1],c3[i][2]);
}

/**for synaptic weight**/
printf("for synaptic weight\n\n");
fprintf(outf,"for synaptic weight\n\n");
printf("\n zM devG EavM sp Hamming_Distance devHD HDGlobal devHG HDLocal devHL nrelax spR\n");
printf("%f %f %f %f %f %f %f %f %f %f %f %f\n",zM,devG,EavM,sp,Hamming_Distance,devHD,HDGlobal,devHG,HDLocal,devHL,nrelax,spR);
fprintf(outf, "\n zM devG EavM sp Hamming_Distance devHD HDGlobal devHG HDLocal devHL nrelax spR\n");
fprintf(outf, "%f %f %f %f %f %f %f %f %f %f %f %f\n",zM,devG,EavM,sp,Hamming_Distance,devHD,HDGlobal,devHG,HDLocal,devHL,nrelax,spR);


fclose(outf);
}

它给出了运行时检查失败#2 - 变量“h”周围的堆栈已损坏。有人可以帮助我吗?

“horn.exe”:已加载“C:\Users\ACER\Desktop\mcm2\Documents\Visual Studio 2010\Projects\minor\Debug\horn.exe”,已加载符号。“horn.exe”:已加载“C:\Windows\SysWOW64\ntdll.dll”,无法找到或打开 PDB 文件“horn.exe”:已加载“C:\Windows\SysWOW64\kernel32.dll”,无法找到或打开 PDB 文件“horn.exe”:已加载“C:\Windows\SysWOW64\KernelBase.dll”,无法找到或打开 PDB 文件“horn.exe”:已加载“C:\Windows\SysWOW64\msvcr100d.dll”,已加载符号。运行时检查失败#2 - 变量“h”周围的堆栈已损坏。程序“[4144]minor.exe: Native”已退出,代码为 0 (0x0)。

这是完整的代码。抱歉,如果我引起任何人不必要的争论..

最佳答案

您可能正在访问 float h[NN] 数组,其索引超出范围(正确的索引值为 0, 1,2,3...,(NN-1)),因此您正在破坏 h 周围的堆栈。

您可能需要仔细检查用于访问数组的索引值。

关于c - 运行时检查失败 #2 - 变量 'h' 周围的堆栈已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23426703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com