0

o seguinte código gera um resultado inesperado, no final do loop as primeiras posições do vetor estão com lixo armazenado, alguém sabe dizer porque isso está acontecendo, e onde está o erro?

OBS. O código tem o mesmo comportamento tanto em C como em C++ (compiladores gcc e g++)

Código:

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


    int V = 10;
    int E = 20;

    int main() {
        double sol[3*E + V];
        int ind[3*E + V];
        double val[3*E + V];
        double ub[3*E + V];
        double obj[3*E + V];
        char **varname;
        char vtype[V];
        int erro;


        varname = (char**) malloc((3*E+V)*sizeof(char**));

        for (int i = 0; i < V; ++i) {
            obj[i] = 0;
            vtype[i] = 'B';
            ub[i] = 1;
            varname[i] = (char*) malloc(sizeof(char*)*10);
            sprintf(varname[i], "y_%i", i);
        }

        int k = 0;
        for (int i=V; i < (V+3*E); i+=3) {
            for(int j=0; j < 3; ++j) {
                obj[i+j] = 0;
                vtype[i+j] = 'B';
                ub[i+j] = 1;
                varname[i+j] = (char*) malloc(sizeof(char*)*15);
                sprintf(varname[i+j], "x_%i_%i_%i", 0, 0, j);
            }
            k++;
        }

        for (int i=0; i < (V+3*E); ++i) {
            printf(" obj[%d]: %.1f \t(%s), \t (%c) <= (%.1f)\n", i, obj[i], varname[i], vtype[i], ub[i]);
        }

        getchar();

    }

Saída:

    Success time: 0 memory: 2296 signal:0
     obj[0]: 156842099844.5     (y_0),   (B) <= (1.0)
     obj[1]: 156842099844.5     (y_1),   (B) <= (1.0)
     obj[2]: 156842099844.5     (y_2),   (B) <= (1.0)
     obj[3]: 156842099844.5     (y_3),   (B) <= (1.0)
     obj[4]: 156842099844.5     (y_4),   (B) <= (1.0)
     obj[5]: 156842099844.5     (y_5),   (B) <= (1.0)
     obj[6]: 0.0    (y_6),   (B) <= (1.0)
     obj[7]: 0.0    (y_7),   (B) <= (1.0)
     obj[8]: 0.0    (y_8),   (B) <= (1.0)
     obj[9]: 0.0    (y_9),   (B) <= (1.0)
     obj[10]: 0.0   (x_0_0_0),   (B) <= (1.0)
     obj[11]: 0.0   (x_0_0_1),   (B) <= (1.0)
     obj[12]: 0.0   (x_0_0_2),   (B) <= (1.0)
     obj[13]: 0.0   (x_0_0_0),   (B) <= (1.0)
     obj[14]: 0.0   (x_0_0_1),   (B) <= (1.0)
     obj[15]: 0.0   (x_0_0_2),   (B) <= (1.0)
     obj[16]: 0.0   (x_0_0_0),   (B) <= (1.0)
     obj[17]: 0.0   (x_0_0_1),   (B) <= (1.0)
     obj[18]: 0.0   (x_0_0_2),   (B) <= (1.0)
     obj[19]: 0.0   (x_0_0_0),   (B) <= (1.0)
     obj[20]: 0.0   (x_0_0_1),   (B) <= (1.0)
     obj[21]: 0.0   (x_0_0_2),   (B) <= (1.0)
     obj[22]: 0.0   (x_0_0_0),   (B) <= (1.0)
     obj[23]: 0.0   (x_0_0_1),   (B) <= (1.0)
     obj[24]: 0.0   (x_0_0_2),   (B) <= (1.0)
     obj[25]: 0.0   (x_0_0_0),   (B) <= (1.0)
     obj[26]: 0.0   (x_0_0_1),   (B) <= (1.0)
     obj[27]: 0.0   (x_0_0_2),   (B) <= (1.0)
     obj[28]: 0.0   (x_0_0_0),   (B) <= (1.0)
     obj[29]: 0.0   (x_0_0_1),   (B) <= (1.0)
     obj[30]: 0.0   (x_0_0_2),   (B) <= (1.0)
     obj[31]: 0.0   (x_0_0_0),   (B) <= (1.0)
     obj[32]: 0.0   (x_0_0_1),   (B) <= (1.0)
     obj[33]: 0.0   (x_0_0_2),   (B) <= (1.0)
     obj[34]: 0.0   (x_0_0_0),   (B) <= (1.0)
     obj[35]: 0.0   (x_0_0_1),   (B) <= (1.0)
     obj[36]: 0.0   (x_0_0_2),   (B) <= (1.0)
     obj[37]: 0.0   (x_0_0_0),   (B) <= (1.0)
     obj[38]: 0.0   (x_0_0_1),   (B) <= (1.0)
     obj[39]: 0.0   (x_0_0_2),   (B) <= (1.0)
     obj[40]: 0.0   (x_0_0_0),   (B) <= (1.0)
     obj[41]: 0.0   (x_0_0_1),   (B) <= (1.0)
     obj[42]: 0.0   (x_0_0_2),   (B) <= (1.0)
     obj[43]: 0.0   (x_0_0_0),   (B) <= (1.0)
     obj[44]: 0.0   (x_0_0_1),   (B) <= (1.0)
     obj[45]: 0.0   (x_0_0_2),   (B) <= (1.0)
     obj[46]: 0.0   (x_0_0_0),   (B) <= (1.0)
     obj[47]: 0.0   (x_0_0_1),   (B) <= (1.0)
     obj[48]: 0.0   (x_0_0_2),   (B) <= (1.0)
     obj[49]: 0.0   (x_0_0_0),   (B) <= (1.0)
     obj[50]: 0.0   (x_0_0_1),   (B) <= (1.0)
     obj[51]: 0.0   (x_0_0_2),   (B) <= (1.0)
     obj[52]: 0.0   (x_0_0_0),   (B) <= (1.0)
     obj[53]: 0.0   (x_0_0_1),   (B) <= (1.0)
     obj[54]: 0.0   (x_0_0_2),   (B) <= (1.0)
     obj[55]: 0.0   (x_0_0_0),   (B) <= (1.0)
     obj[56]: 0.0   (x_0_0_1),   (B) <= (1.0)
     obj[57]: 0.0   (x_0_0_2),   (B) <= (1.0)
     obj[58]: 0.0   (x_0_0_0),   (B) <= (1.0)
     obj[59]: 0.0   (x_0_0_1),   (B) <= (1.0)
     obj[60]: 0.0   (x_0_0_2),   (B) <= (1.0)
     obj[61]: 0.0   (x_0_0_0),   (B) <= (1.0)
     obj[62]: 0.0   (x_0_0_1),   (B) <= (1.0)
     obj[63]: 0.0   (x_0_0_2),   (B) <= (1.0)
     obj[64]: 0.0   (x_0_0_0),   (B) <= (1.0)
     obj[65]: 0.0   (x_0_0_1),   (B) <= (1.0)
     obj[66]: 0.0   (x_0_0_2),   (B) <= (1.0)
     obj[67]: 0.0   (x_0_0_0),   (B) <= (1.0)
     obj[68]: 0.0   (x_0_0_1),   (B) <= (1.0)
     obj[69]: 0.0   (x_0_0_2),   (B) <= (1.0)

Abraços!

1 Resposta 1

2

Seu array vtype tem apenas tamanho 10, mas você escreve nele como se ele tivesse o mesmo tamanho dos outros. Da forma como as variáveis locais são estruturadas, você acaba escrevendo em cima da memória dos outros arrays. Neste caso você está escrevendo por cima do array obj, o que acaba gerando o lixo que você observou.

1
  • Putz! Isso mesmo! Muito obrigado!
    – Rafael
    Commented 29/09/2016 às 16:35

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .