-2

[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?

Kaostias
  • 321
  • 4
  • 24

1 Answers1

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • It is perhaps surprising (not only to the OP) that the debugger appears to display elements of other 2D-array arguments just fine. What's the difference between `poblacion` and `in`? – Peter - Reinstate Monica Apr 07 '16 at 10:55
  • Ah, a closer look reveals the issue. As you say correctly, the formal parameter `in` of `generacion()` is a pointer to an array. The debugger by default displays the data behind a pointer. It knows how to display an array: It displays all elements, each in braces. It does that for `poblacion`, which is a pointer to an array of 128 ints. It does that for `in` as well, if we look closely; `in` just happens to be a pointer to an array of 1 int (`NUMVAR` is 1) which has the value 0. The debugger does not display the contents of `in+1` or `poblacion+1` (because that may not be accessible memory). – Peter - Reinstate Monica Apr 07 '16 at 11:08
  • @PeterA.Schneider So if in's size was bigger it could be show? – Kaostias Apr 07 '16 at 11:43
  • @Kaostias It *is* shown already (note the little `{0}` in the value column). Because `NUMVAR` is 1, `main`'s `in` is a 2-dimensional array with only a single column; as an argument to a function it is "adjusted" to a pointer to the first row, which is the said array with a single element, `0`, which is correctly displayed. If you open a watch window you can modify the expression to evaluate and e.g. let the watch display `in[1]` or `in[1][0]`. – Peter - Reinstate Monica Apr 07 '16 at 12:05