Thread: I dont understand how this can bugged

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    If you don't mind everyone seeing the whole code (it's not propretary or anything), then I would suggest.

    - delete the contents of Project\Debug\* and Project\Release\*
    - Create a zip file of Project
    - Rename it to say project.zip.txt (so the board will accept it)
    - Upload as an attachment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #17
    Registered User
    Join Date
    Dec 2009
    Posts
    39
    ok so I couldn't get the board attachement to work so I uploaded it here:
    Code:
    http://www.opaalkust.com/Download/project%20AD3.rar
    Run it and the command window will ask for your input. Enter this:
    Code:
    2 3 0 <=
    1 2 3 11 22 33
    I put a breakpoint at the start of SimAnAlgorithm_run to verify the values of the arguments.

    Ask away if you can't find your way around my messy code!

  3. #18
    Registered User
    Join Date
    Dec 2009
    Posts
    39
    anyone please? this project is due in two days...

  4. #19
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    This doesn't even COMPILE, let alone run

    Code:
    gcc -ggdb3 -Wall -pedantic --std=c99 AlgorithmHandler.c GeneticAlgorithm.c Helpers.c InputHandler.c SimAnAlgorithm.c main.c -o proj
    AlgorithmHandler.c: In function ‘AlgorithmHandler_verifyComparator’:
    AlgorithmHandler.c:70: warning: control reaches end of non-void function
    AlgorithmHandler.c: In function ‘AlgorithmHandler_genCritter’:
    AlgorithmHandler.c:87: error: too many arguments to function ‘AlgorithmHandler_genNucleotide’
    AlgorithmHandler.c: At top level:
    AlgorithmHandler.c:103: error: conflicting types for ‘AlgorithmHandler_genNucleotide’
    AlgorithmHandler.h:90: error: previous declaration of ‘AlgorithmHandler_genNucleotide’ was here
    AlgorithmHandler.c: In function ‘AlgorithmHandler_setFittnes’:
    AlgorithmHandler.c:376: warning: unused variable ‘f’
    AlgorithmHandler.c:375: warning: unused variable ‘j’
    AlgorithmHandler.c: In function ‘AlgorithmHandler_doMutationNucleotide’:
    AlgorithmHandler.c:590: warning: ‘return’ with a value, in function returning void
    AlgorithmHandler.c:531: warning: unused variable ‘j’
    AlgorithmHandler.c:531: warning: unused variable ‘i’
    GeneticAlgorithm.c: In function ‘GeneticAlgorithm_defaultPool’:
    GeneticAlgorithm.c:32: warning: unused variable ‘genome’
    GeneticAlgorithm.c: In function ‘GeneticAlgorithm_doCrossover’:
    GeneticAlgorithm.c:101: warning: unused variable ‘tmp’
    GeneticAlgorithm.c: In function ‘GeneticAlgorithm_selectNextGeneration’:
    GeneticAlgorithm.c:228: warning: unused variable ‘j’
    GeneticAlgorithm.c: In function ‘GeneticAlgorithm_printPool’:
    GeneticAlgorithm.c:276: warning: passing argument 1 of ‘AlgorithmHandler_CritterToString’ from incompatible pointer type
    main.c: In function ‘runProgram’:
    main.c:50: warning: unused variable ‘InvariantRows’
    main.c: In function ‘test’:
    main.c:131: warning: unused variable ‘fitt’
    Last edited by rags_to_riches; 11-28-2011 at 12:39 PM.

  5. #20
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    show us the include file definition of the function.

    and how are you determining the arguments have random values? with the debugger or printf's?

    one approach is to start by commenting all code out of the function and just printing the arguments. if they are wrong at that point then the bug is in the caller. if the arguments are ok then something in the function itself is corrupting the arguments, probably via the stack. add code back little by little until the error occurs to at least find what is causing the problem.
    Last edited by dmh2000; 11-28-2011 at 12:34 PM. Reason: added

  6. #21
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well it seems to me that you freed something, then tried to make use of it.

    Code:
    #0  0x00402f94 in AlgorithmHandler_copyCritter (to=0x3960e0, from=0x395878) at C:\Users\sc\Downloads\project AD3\project AD3\project AD3\AlgorithmHandler.c:154
    #1  0x00402458 in SimAnAlgorithm_genNeighbour (settings=0x393378, parent=0x395878) at C:\Users\sc\Downloads\project AD3\project AD3\project AD3\SimAnAlgorithm.c:49
    #2  0x00402342 in SimAnAlgorithm_run (settings=0x393378, maxRuns=1000, fittnessLimit=18, restartPropability=5) at C:\Users\sc\Downloads\project AD3\project AD3\project AD3\SimAnAlgorithm.c:16
    #3  0x004026d6 in runProgram () at C:\Users\sc\Downloads\project AD3\project AD3\project AD3\main.c:113
    #4  0x00402514 in main () at C:\Users\sc\Downloads\project AD3\project AD3\project AD3\main.c:31
    149	
    150		// allocate memory for genome
    151		to->genome=(struct AlgorithmHandler_Genome * )malloc(sizeof(struct AlgorithmHandler_Genome));
    152	
    153		// copy genome attributes
    154		to->genome->length=from->genome->length;
    155	
    156		// allocate memory for root nucleotide
    157		to->genome->root=(struct AlgorithmHandler_Nucleotide *)malloc(sizeof(struct AlgorithmHandler_Nucleotide));
    158	
    $3 = (struct AlgorithmHandler_Critter *) 0x395878
    $4 = {
      age = -1426050767, 
      fittness = -1.21979282e-012, 
      diff = -1.14094669e+038, 
      genome = 0xfeeefeee
    }
    The last output is from "print *from"
    Notice the value in genome - this is supposed to be a pointer.
    The feeefeee in this case indicates that this block of memory had been freed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #22
    Registered User
    Join Date
    Dec 2009
    Posts
    39
    @Salem:
    I trimmed the source so thet I don't call any function that frees any memory before trying to call SimAnAlgorithm_run() & it didn't solve the problem.


    @dmh2000:
    SimAnAlgorithm.c has this as first line:
    Code:
    #include "SimAnAlgorithm.h"
    SimAnAlgorithm.h contents:
    Code:
    #ifndef SIMANALGORITHM_DEFINED
    #define SIMANALGORITHM_DEFINED
    
    #include "AlgorithmHandler.h"
    
    
    struct AlgorithmHandler_Critter * SimAnAlgorithm_run(
    											struct AlgorithmHandler_Settings * settings, 
    											int maxRuns, 
    											float fittnessLimit, 
    											int restartPropability);
    struct AlgorithmHandler_Critter * SimAnAlgorithm_genNeighbour(
    											struct AlgorithmHandler_Settings * settings,
    											struct AlgorithmHandler_Critter * parent);
    
    int SimAnAlgorithm_getTransitionPropability(float eC, 
    											float eBuur, 
    											float temp);
    
    float SimAnAlgorithm_getTemp(				float progress);
    float SimAnAlgorithm_getE(					float fittness);
    
    #endif
    how are you determining the arguments have random values? with the debugger or printf's?
    I use the debugger.

    @rags_to_riches:
    I compile with the build in VS 2010 compiler







    EDIT:
    I've been trying to compile with gcc -std=c99 -lm. Seems like VS 2010 was missing some bugs. After working out most obvious ones, I'm left with this:
    Code:
    main.o: In function `runProgram':
    main.c:(.text+0x225): undefined reference to `SimAnAlgorithm_run'
    collect2: ld returned 1 exit status
    make: *** [test] Error 1
    I guess something must be amiss with my function definition. I've been trying to find out what, but no success so far.

    EDIT 2:
    I've uploaded the updated project source:
    Code:
    http://www.opaalkust.com/Download/project%20AD3.rar
    Last edited by klmdb; 11-28-2011 at 02:56 PM.

  8. #23
    Registered User
    Join Date
    Dec 2009
    Posts
    39
    UPDATE: I found out what the problem was: I was running the VS builder in 'release' mode instead of 'debug' mode. This screwed up the values I saw while debugging.

    The algorithm is still buggy though; there's some bad pointers I haven't fixed. At least now I know what to look for.

    Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i dont understand this bug, please help me :(
    By Grey Kliche in forum C++ Programming
    Replies: 12
    Last Post: 08-09-2011, 08:03 AM
  2. i dont understand the problem
    By jorgejags in forum C Programming
    Replies: 4
    Last Post: 10-03-2008, 01:05 PM
  3. i dont understand bit
    By joker_tony in forum C Programming
    Replies: 2
    Last Post: 03-27-2008, 12:15 AM
  4. What I dont understand...
    By nomi in forum C# Programming
    Replies: 7
    Last Post: 01-20-2004, 02:00 AM
  5. dont understand VS.NET
    By Qui in forum Windows Programming
    Replies: 6
    Last Post: 10-15-2003, 07:36 PM