1
float a, b;
float sa() { return a;};
int main() {
  a = 10;
  b = sa();
  printf("%f", b);
  return 0;
}

这是我的代码的简化版本。我相信程序应该打印 10,但它给了我非常小的数字,比如 -65550,并不总是相同但非常相似。

我使用调试器在变量a返回之前检查了变量的值,它是 10,所以函数返回 10,但 b 设置为 -65550 之类的值。我不明白为什么会这样。

我会很感激一些情报。

提前致谢。

这是完整的代码:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>

int dimensiuni, nrBitiSolutie, bitiPeDimensiune, gasitInbunatatire, nrRulari;
float limInf, limSup, precizie, valoareFunctie, minim, minimNou, T;
char solutie[100000];
float solutieReala[100];

void generateRandomSolution();
void bitesToFloat();
void rastrigin();
void rosenbrock();
float nextFirstFit();
float nextBestFit();


void main() {
    int k;
    T = 10;
    gasitInbunatatire = 1;
    srand ( time(NULL) );
    printf("Introduceti numarul de dimensiuni: ");
    scanf("%d", &dimensiuni);
    printf("Introduceti limita inferioara si cea superioara: ");
    scanf("%f%f", &limInf, &limSup);
    printf("Introduceti precizia: ");
    scanf("%f", &precizie);

    //calculam numarul de biti necesari ca sa reprezentam solutia
    nrBitiSolutie = dimensiuni * ceil(log(limSup-limInf * pow(10, precizie)))/log(2.0);
    bitiPeDimensiune = nrBitiSolutie/dimensiuni;

    //generam o solutie random
    generateRandomSolution();
    bitesToFloat();
    rastrigin();
    minim = valoareFunctie;

    printf("Pornim de la %f\n", minim);

    while( (nrRulari < 10000) && (T > 0.001)) {
        minimNou = sa(); //error occurs here. sa() returns about 200 but minimNou is set to -65550
        if (minimNou < minim) {
            printf("Minim nou: %f\n", minimNou);
            minim = minimNou;
            T *= 0.995;
        }
        nrRulari++;
    }
    printf("Minimul aproximat: %f\n", minim);
    system("pause");
}

void generateRandomSolution() {
    int l;
    for (l = 0; l < nrBitiSolutie; l++) solutie[l] = rand()%2;
}

void bitesToFloat() {
    int i, parcurse = 1, gasite = 0;
    int variabila = 0;
    float nr;
    for (i = 0; i < nrBitiSolutie; i++) {
        variabila = variabila<<1 | (int)solutie[i];
        if(parcurse == bitiPeDimensiune) {
            nr = (float)variabila / (float)pow(2, bitiPeDimensiune);
            nr *= limSup-limInf;
            nr += limInf;
            nr *= pow(10, precizie);
            nr = (int)nr;
            nr /= pow(10, precizie);
            parcurse = 0;
            solutieReala[gasite++] = nr;
            variabila = 0;
        }
        parcurse++;
    }
}

void rastrigin() {
    int i;
    valoareFunctie = 10 * dimensiuni;
    for (i = 0; i < dimensiuni; i++) {
        valoareFunctie += pow((float)solutieReala[i], 2) - 10 * (float)cos(2 * 3.14 * (float)solutieReala[i]);
    }
}

void rosenbrock() {
    int i;
    valoareFunctie = 0;
    for (i = 0; i < dimensiuni - 1; i++) {
        valoareFunctie += 100 * pow((solutieReala[i+1] - pow(solutieReala[i], 2)), 2) + pow((1-solutieReala[i]), 2);
    }
}

float sa() {
    int j;
    for (j = 0; j < nrBitiSolutie; j++) {
        solutie[j] = solutie[j] == 0 ? 1 : 0;
        bitesToFloat();
        rastrigin();
        if (valoareFunctie < minim) return valoareFunctie;
        else if ( (rand()/INT_MAX) < exp((minim - valoareFunctie)/T) )  
            return valoareFunctie;
        else solutie[j] = solutie[j] == 0 ? 1 : 0;
    }
    return minim;
}

error occurs here我已经用注释标记了错误发生的位置

4

1 回答 1

5

您错误地简化了代码。在您的简化中,您sa()在调用它之前定义了它。但是在您的完整程序中,您sa()在定义它之前调用它。在没有声明的情况下,假定函数返回int。由于您的函数实际上返回 a float,因此结果未定义。(在这种情况下,您将从浮点堆栈顶部读取一个垃圾值,然后浮点堆栈将下溢,事情从那里走下坡路。)

于 2012-12-06T23:04:46.193 回答