Thread: Help Problems in memory

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    3

    Help Problems in memory

    Code:
    int *genes;
    
    Cromossoma :: Cromossoma(int numGenes_) {
      numGenes = numGenes_;
      genes = new int[numGenes+1];
      if (genes == NULL) {
        printf ("ERROR Cromossoma.cpp: MEMORY\n");
        exit (1);
      }
      for (int i = 0; i < numGenes+1;i++)
        genes[i] = 1;
    }
    
    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;
    }
    --------------------------------------------------------------------------------------

    Code:
    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");
      }
    }
    
    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++) {
        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;
    }
    Any of this functions will give this in valgrind

    96: 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543 8543
    97: 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502 8502
    98: 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610 6610
    99: 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218 218
    100: 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803 3803
    ==10891==
    ==10891== Invalid read of size 4
    ==10891== at 0x804CEB2: AlgoritmoGenetico::imprimeCromossomas(Cromossom... .......) (AlgoritmoGenetico.cpp:65)
    ==10891== by 0x804D65F: AlgoritmoGenetico::geracao(int, int) (AlgoritmoGenetico.cpp:104)
    ==10891== by 0x804D789: AlgoritmoGenetico::AlgoritmoGenetico(Taref........ .., int, int, int) (AlgoritmoGenetico.cpp:11)
    ==10891== by 0x804BBDE: main (SmallBang.cpp:29)
    ==10891== Address 0x4822df4 is 0 bytes after a block of size 404 alloc'd
    ==10891== at 0x402630E: operator new[](unsigned int) (vg_replace_malloc.c:268)
    ==10891== by 0x804CE37: AlgoritmoGenetico::imprimeCromossomas(Cromossom... .......) (AlgoritmoGenetico.cpp:57)
    ==10891== by 0x804D65F: AlgoritmoGenetico::geracao(int, int) (AlgoritmoGenetico.cpp:104)
    ==10891== by 0x804D789: AlgoritmoGenetico::AlgoritmoGenetico(Taref........ .., int, int, int) (AlgoritmoGenetico.cpp:11)
    ==10891== 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 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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
    ..............
    and so on
    ..............
    298: 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 0 0 0 0 0 0
    299: 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 0 0 0 0 0 0
    ==10891==
    ==10891== ERROR SUMMARY: 60297 errors from 3 contexts (suppressed: 17 from 1)
    ==10891== malloc/free: in use at exit: 0 bytes in 0 blocks.
    ==10891== malloc/free: 79,446 allocs, 79,446 frees, 1,492,250 bytes allocated.
    ==10891== For counts of detected errors, rerun with: -v
    ==10891== All heap blocks were freed -- no leaks are possible.
    andre@andre-laptop:~/Dropbox/Work/JSOLVER5$



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

    If I use instead this function


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

    293: 2217 6734 9079 4082 7642 5543 8580 8763 5994 2045 8481 1273 4784 6369 4736 2589 1232 8880 9276 9890 9193 6273 8870 150 4295 4270 9055 3703 1129 7789 5060 7703 874 4138 8136 4867 6032 6716 9982 2025 8760 8462 3298 9895 1183 4385 8835 2414 3264 4463 2303 8808 7087 7524 5309 1382 8146 715 1436 5626 4856 2847 3328 5729 6984 7815 6948 9368 4530 6929 1392 9642 1742 1041 5888 2924 1778 1075 5338 1393 5537 3992 201 2623 7868 5509 356 6013 2576 8143 7990 7431 989 1317 9511 4325 9131 6458 3692 13 3386
    294: 2043 5944 1326 9679 549 1913 6392 8284 1941 6081 4013 4785 7067 8216 5497 3824 3821 7625 3714 2422 1048 8824 5419 5181 1157 7643 297 9157 6053 2220 4429 1449 8163 5754 7479 5064 4019 3871 3347 5959 9951 3711 7095 7017 1927 2591 7193 5747 6567 906 4520 3967 6081 9939 9147 7238 3933 5795 2746 9985 8015 7174 7785 2529 2928 5264 7592 3298 5486 7290 5608 5436 7353 2702 8805 9279 5292 2349 5025 8211 3254 5897 2177 9335 5835 7675 2924 6119 3470 5669 6104 7836 2842 3888 6716 2121 5503 660 5418 988 7949
    295: 1887 9143 5498 2240 6892 9590 9001 6016 3615 5278 2476 2917 6974 8340 6776 5272 6179 1398 8444 4835 6936 5013 3322 8243 740 6610 6894 6382 3247 4001 2903 5603 9496 4753 4195 6387 694 3195 2402 4308 4825 1230 7225 1798 5921 352 7069 8452 8101 1864 3286 5036 6876 2959 9630 3968 5920 6524 349 9166 6876 9603 1120 6371 4355 5314 2758 1401 4861 1511 2060 6037 9092 5636 4186 5013 5987 7606 3464 4088 9469 3101 5475 2696 2411 5105 6663 8330 7980 7011 3847 4855 2966 4966 1226 3672 6632 335 5072 7844 1845
    296: 2186 6317 1613 5286 1547 7496 652 5891 7543 3422 1517 9396 9168 5268 5916 2873 4375 5225 6639 5868 6753 4469 4363 1552 7779 1162 6908 1051 8480 9519 7256 5376 2188 5220 7013 3734 2715 7664 9624 6609 7437 1140 2357 2957 2759 8272 5829 7134 9848 8819 3001 6600 3287 7363 8151 1065 4877 1411 8468 9708 7281 2075 5083 9468 7294 2095 3201 6360 6110 9176 2968 9899 6668 1676 2855 9426 9947 5035 6559 6146 3854 5912 2746 3492 9626 7248 909 4502 5010 5728 561 2291 7802 5643 8110 1447 4089 7663 7806 199 6838
    297: 2057 681 7745 9457 4125 4922 6588 8511 9465 6344 9752 5740 613 266 1899 1391 8112 4474 5501 8232 5063 7628 208 3198 1700 3054 5659 3684 9741 6859 6048 492 3891 3793 9948 4367 8714 6535 2878 8178 9230 8981 269 9843 5598 2167 7585 3709 2992 9437 8293 4406 3416 8500 7603 5115 7906 9613 5150 7646 6471 7550 8137 6714 1342 4436 1080 6407 971 309 4584 6552 9289 1204 6394 1238 3370 330 4947 6361 9766 3239 7119 3182 8090 4721 4648 2347 4334 6150 6344 7156 3699 832 3869 5040 5268 1301 7798 6238 1609
    298: 2657 798 6365 8510 3030 7422 2842 5537 3268 1491 9863 8998 8640 8836 8959 5253 7341 2211 5931 7593 1077 2124 8776 6772 5990 7914 1459 8590 9744 8631 8831 3661 5781 1547 8523 5162 5320 1364 699 8588 9207 561 3937 4198 5748 2896 9450 3089 5106 1733 7033 6183 208 2160 9306 2550 73 7116 7491 6168 2098 6321 9829 7878 4220 8351 9392 9539 6066 90 4478 1624 7002 8415 2173 2749 1310 7975 2189 2767 9707 5573 5301 9914 7732 4606 2463 4156 8073 9953 324 171 2626 6504 4400 3197 1206 3791 2735 7271 232
    299: 2260 3963 5324 6733 1173 4244 3976 621 9247 5490 6305 3957 9199 5842 1890 6045 158 4354 1816 1847 6714 8472 3684 2014 930 6651 442 1324 5989 1112 4485 603 1426 9808 3688 2599 4051 7663 3219 9650 9504 5875 9958 5054 1716 1848 1098 8225 2553 9266 72 9266 4089 107 7631 1371 3110 4425 2694 9098 5536 3530 6052 3313 9689 9739 5911 3740 3753 5482 3389 9608 1356 3346 4662 9424 1545 2111 7648 449 1376 4071 6067 5465 530 3697 6835 3639 8121 5880 2736 8 5762 5139 3321 5450 4878 5583 5541 4982 7416
    ==10926==
    ==10926== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 17 from 1)
    ==10926== malloc/free: in use at exit: 0 bytes in 0 blocks.
    ==10926== malloc/free: 79,279 allocs, 79,279 frees, 1,489,070 bytes allocated.
    ==10926== For counts of detected errors, rerun with: -v
    ==10926== All heap blocks were freed -- no leaks are possible.
    andre@andre-laptop:~/Dropbox/Work/JSOLVER5$



    No errors with this second function... Don't understand why the other 3 don't work?
    If anyone could help I would apreciate!
    Thanks!
    Last edited by askno; 12-28-2009 at 07:21 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    for (int i = 0; i < numGenes+1; i++)
    This looks fishy. If you have 10 genes, the highest index you can access is gene[9]. I think that +1 is what is causing your problems.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    3
    It was purposed, when I make new I consider that this is because the position 0 of the array is where I put the fitness of the cromossoma.

    Code:
    Cromossoma :: Cromossoma(int numGenes_) {
      numGenes = numGenes_;
      genes = new int[numGenes+1];

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

    by the way I forgot to paste the print function of Cromossoma.cpp

    Code:
    void Cromossoma :: imprimeCromossoma() {
      for (int i = 0; i < numGenes+1; i++)
        printf ("%d ", genes[i]);
      printf ("\n");
    }

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    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;
    }
    Shouldn't array[i] be array[j] there?
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    3
    Thank you very much... I was already doubting and questioning my knowlege at C lolol... I was already thinking that c++ was complety different from c++ with the handling of pointers...
    The problem is that im new in using valgrind debbuger... and It was showing that was a malloc
    ==10891== Invalid read of size 4
    ==10891== at 0x804CEB2: AlgoritmoGenetico::imprimeCromossomas(Cromossom... .......) (AlgoritmoGenetico.cpp:65)
    ==10891== by 0x804D65F: AlgoritmoGenetico::geracao(int, int) (AlgoritmoGenetico.cpp:104)
    ==10891== by 0x804D789: AlgoritmoGenetico::AlgoritmoGenetico(Taref........ .., int, int, int) (AlgoritmoGenetico.cpp:11)
    ==10891== by 0x804BBDE: main (SmallBang.cpp:29)
    ==10891== Address 0x4822df4 is 0 bytes after a block of size 404 alloc'd
    ==10891== at 0x402630E: operator new[](unsigned int) (vg_replace_malloc.c:268)
    ==10891== by 0x804CE37: AlgoritmoGenetico::imprimeCromossomas(Cromossom... .......) (AlgoritmoGenetico.cpp:57)
    ==10891== by 0x804D65F: AlgoritmoGenetico::geracao(int, int) (AlgoritmoGenetico.cpp:104)

    so I thinked that was related to the new operator... Thanks..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Fragmentation with Dynamic FIFO Queue
    By fguy817817 in forum Linux Programming
    Replies: 17
    Last Post: 10-31-2009, 04:17 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Weird memory problems (MS-VC++)
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2006, 08:01 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. stl::vector problems (memory?)
    By aker_y3k in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2003, 11:21 AM