Some data disapears from pointer when calling a function

时间:2016-04-07 10:41:42

标签: c function pointers function-call

[EDIT] The main problem was that the fitness of my evolution returned the same value every time after changing some int into float values. The misterious point is that i restarted the computer and it surprisingly worked again.

I'm calling a function from my main, when i debug the code, the variables contain data, but in the header of the function, when i debug, my data is lost and the reference on memory is the same (i'm compiling with visual Studio 2013), this only happens in some of the variables (you can check which ones in the pictures below)

int main(){
    float resultados[NUMCROMOSOMAS][CANTIDADMEDICIONES];
    int in[CANTIDADMEDICIONES][NUMVAR];
    char gramaticas[NUMCROMOSOMAS][LONGITUDCADENA];
    int mejorValorIndividuo[100];
    char variableNames[NUMVAR + 1];
    float fitness[NUMCROMOSOMAS];
    char mejorindividuo[LONGITUDCADENA];
    float medicionesObtenidas[NUMCROMOSOMAS][CANTIDADMEDICIONES];
    int i,j;

(Initializations, some of the relevant ones are)

    for (i = 0; i < NUMCROMOSOMAS; i++)
        fitness[i] = 0.0;


    for (i = 0; i < CANTIDADMEDICIONES; i++)
        in[i][0] = i;

Yeah, that was a bidimensional array using one column

And here is the main loop of my program

    int curr = MAXINT;
    i = 0;
    while ( isNotGoodEnough(curr) ){
        i++;
        curr = generacion(poblacion, results, input, collectedData, gramaticas, mejorindividuo, variableNames, fitness);
    }
    return poblacion[0][0];
}

The header of my function is this:

int generacion(int poblacion[NUMCROMOSOMAS][SIZECROMOSOMAS], 
               float resultados[NUMCROMOSOMAS][CANTIDADMEDICIONES], 
               int in[CANTIDADMEDICIONES][NUMVAR],
               float valoresEsperados[NUMCROMOSOMAS][CANTIDADMEDICIONES], 
               char gramaticas[NUMCROMOSOMAS][LONGITUDCADENA], 
               char * mejorIndividuo, 
               char variableNames[NUMVAR], 
               float fitness[NUMCROMOSOMAS]){

Here is the compiler before the calling before the calling Here is the compiler right after the calling enter image description here

What i'm doing wrong?

1 个答案:

答案 0 :(得分:4)

When you have an argument declaration like

int in[CANTIDADMEDICIONES][NUMVAR]

that's not really what the compiler uses, what it translates it to is

int (*in)[NUMVAR]

In other words in is a pointer and not an array.

What you're seeing in the debugger in the function is the pointer, but since the size of the data pointed to by the pointer is unknown the debugger can't show you the data directly. If you explicitly, in the debugger when in the function, check in[0] you will see that the data is correct.

In other words, it's not a problem with the code, it's how the debugger displays (or rather doesn't display) the data.