Jump to content
  • Revista PROGRAMAR: Já está disponível a edição #60 da revista programar. Faz já o download aqui!

Sign in to follow this  
askno

Apontadores ou nao...

Recommended Posts

askno

AlgoritmoGenetico.cpp

-----------------------------------------------------------------------------------

void AlgoritmoGenetico :: imprimeCromossomas (Cromossoma **c) {
  int numGenes = linhas*colunas;
  int *array = new int[numGenes+1];
  if (!array)
    exit(1);
  //else exit(1);
  for (int i = 0; i < pop_size; i++) {
    c[i] -> getGenes (array, numGenes);
    printf ("%d: ", i);
    for (int j = 0; j < numGenes+1; j++)
      printf ("%d ", array[i]);
    printf ("\n");
  }
  delete [] array;
}
**/
void AlgoritmoGenetico :: imprimeCromossomas (Cromossoma **c) {
  int numGenes = linhas*colunas;
  int *array = new int[numGenes+1];
  if (!array)
    exit(1);
  for (int i = 0; i < pop_size; i++) {
    array = c[i] -> getGenes (array, numGenes);
    cout << i << ": ";
    for (int j = 0; j < numGenes+1; j++)
      cout << array[i] << " ";
    endl(cout);
  }
  delete [] array;

void AlgoritmoGenetico :: imprimeCromossomas (Cromossoma **c) {
  int numGenes = linhas*colunas;
  int *array;
  for (int i = 0; i < pop_size; i++) {
    array = c[i] -> getGenes ();
    printf ("%d: ", i);
    for (int j = 0; j < numGenes+1; j++)
      printf ("%d ", array[i]);
    printf ("\n");
  }
}

Cromossoma.cpp

--------------------------------------------------------------------------------------------------------------

int* Cromossoma::getGenes(int *destino, int numGenes) {
  if (this -> numGenes != numGenes || destino == NULL) {
    printf ("ERRO Cromossoma.cpp: Numero de genes diferentes\n");
    return NULL;
  }
  for (int i = 0; i < numGenes+1; i++)
    destino[i] = genes[i];
  return destino;
}

int* Cromossoma :: getGenes () {
  return genes;
}

Boas é o seguinte as 3 funções de imprimir, imprimem lixo e no valgrind diz que o new simplesmente nao aloca memoria...

96: 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414 7414

97: 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098 7098

98: 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016 1016

99: 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096 7096

100: 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532 532

==9829==

==9829== Invalid read of size 4

==9829==    at 0x804CEB2: AlgoritmoGenetico::imprimeCromossomas(Cromossoma**) (AlgoritmoGenetico.cpp:68)

==9829==    by 0x804D671: AlgoritmoGenetico::geracao(int, int) (AlgoritmoGenetico.cpp:107)

==9829==    by 0x804D79B: AlgoritmoGenetico::AlgoritmoGenetico(Tarefa**, int, int, int) (AlgoritmoGenetico.cpp:11)

==9829==    by 0x804BBDE: main (SmallBang.cpp:29)

==9829==  Address 0x48229f4 is 0 bytes after a block of size 404 alloc'd

==9829==    at 0x402630E: operator new[](unsigned int) (vg_replace_malloc.c:268)

==9829==    by 0x804CE37: AlgoritmoGenetico::imprimeCromossomas(Cromossoma**) (AlgoritmoGenetico.cpp:60)

==9829==    by 0x804D671: AlgoritmoGenetico::geracao(int, int) (AlgoritmoGenetico.cpp:107)

==9829==    by 0x804D79B: AlgoritmoGenetico::AlgoritmoGenetico(Tarefa**, int, int, int) (AlgoritmoGenetico.cpp:11)

==9829==    by 0x804BBDE: main (SmallBang.cpp:29)

101: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

Se meter esta função

void AlgoritmoGenetico :: imprimeCromossomas (Cromossoma **c) {
  for (int i = 0; i < 5; i++) {
    printf ("%d: ", i);
    c[i] -> imprimeCromossoma ();
  }
}

Ja imprime sem qualquer tipo de erro...

Poderiam-me dizer o kek eu tou a fazer de mal nas funções acima?

Obrigado cumprimentos

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.