Thread: Dynamic Allocating and I having trouble figuring out what is wrong with my code.

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    7

    Dynamic Allocating and I having trouble figuring out what is wrong with my code.

    I have asked several friends for help. I have no idea what is going wrong with my program. It's a multifile program so I will just post the functions that are being used in my test. I'm not quite sure if I am allocating the memory properly but I assume I am because of the if statement. Well here is my code. By the way please correct me if you see any bad habits or wrong conventions. I take constructive criticism very well. Heck even harsh criticism.

    Code:
    int test_reduce(char * err_messages)
    {
       Fraction inputs[REDUCE_COUNT] = REDUCE_INPUT;
       Fraction corrects[REDUCE_COUNT] = REDUCE_CORRECT;
       Fraction computed[REDUCE_COUNT] = {};
       Fraction in_fraction = {0,1}, correct = {0,1}, result;
    
    
       int i = 0, errors = 0;
    
    
       for(i = 0; i < REDUCE_COUNT; i++) {
          in_fraction = inputs[i];
          correct = corrects[i];
          result = in_fraction;
          reduce_fraction(&result);
    
    
          if (!equalFractions(result,correct)) {
    	 if (!errors) {
    	    strcat(err_messages,"\n\nReduce Errors:\n");
    	    errors = 1; 
    	 }
    	 sprintf(err_messages,"%s(%d,%d)\nreceived (%d,%d), correct (%d,%d)\n\n",
    		 err_messages,in_fraction.numer,in_fraction.denom,result.numer,
    		 result.denom,correct.numer,correct.denom);
          }
       }
       return !errors;
    }
    
    int gcd(int a, int b)
    {
       if (a == 0 && b == 0) 
       {
          printf("Illegal args to gcd: %d, %d\n",a,b);
          exit(1);
       }
       a = abs(a);
       b = abs(b);
       
       if (a == 0)
          return b;
       if (b == 0)
          return a;
       return gcd(b,a%b);
    }
    
    void reduce_fraction(Fraction *R)
    {   
         int cmnDenom;
         Fraction *reduce;
         reduce = (Fraction*) malloc(sizeof(reduce));
         //reduce = (reduce*)malloc(sizeof(reduce));
         if(reduce != NULL)
         {
             printf( "R->numer: %d\nR->denom: %d\n\n", R->numer,R->denom);
         
             cmnDenom = gcd(R->numer,R->denom);
             reduce->numer = R->numer/cmnDenom;
             reduce->denom = R->denom/cmnDenom;
    
    
             *R = *reduce;
         }
         
         free(reduce);
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    void reduce_fraction(Fraction *R)
    {  
         int cmnDenom;
         Fraction *reduce;
         // reduce = (Fraction*) malloc(sizeof(reduce)); // you want allocate space for a Fraction not for a pointer to a fraction
         reduce = (Fraction*)malloc(sizeof(Fraction));
         if(reduce != NULL)
         {
             printf( "R->numer: %d\nR->denom: %d\n\n", R->numer,R->denom);
          
             cmnDenom = gcd(R->numer,R->denom);
             reduce->numer = R->numer/cmnDenom;
             reduce->denom = R->denom/cmnDenom;
     
     
             *R = *reduce;
         }
          
         free(reduce);
    }
    I don't see a reason why you would want to dynamically allocate the Fraction
    Code:
    void reduce_fraction(Fraction *R)
    {  
         int cmnDenom;
         Fraction reduce;
         printf( "R->numer: %d\nR->denom: %d\n\n", R->numer,R->denom);
          
         cmnDenom = gcd(R->numer,R->denom);
         reduce.numer = R->numer/cmnDenom;
         reduce.denom = R->denom/cmnDenom;
      
         *R = reduce;
    }
    Last edited by ZuK; 04-21-2012 at 06:10 PM.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    thanks for the reply.
    So when do you have to allocate? Like can you give me some sample cases.

    But whenever I run this my gcd function keeps returning my error for both pass-ins were 0. I keeps returning the illegal arg error.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by dadiezel07 View Post
    So when do you have to allocate? Like can you give me some sample cases.
    I'd use malloc() only if the size of the data is too big for the stack. Stackspace is usually a few MB.
    Another reason might be when the size of the data is known only at runtime. Like when you read from files.

    But whenever I run this my gcd function keeps returning my error for both pass-ins were 0. I keeps returning the illegal arg error.
    Not enough info. Post complete, compilable code.

    Kurt

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    How do I post the complete code? It's in a few different files? Do I just post it all in one message?

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    FYI, your allocation was wrong. It should have been:
    Code:
    reduce = malloc(sizeof(*reduce));
    However, you certainly didn't require dynamic memory allocation anyway.

    But whenever I run this my gcd function keeps returning my error for both pass-ins were 0. I keeps returning the illegal arg error.
    Code:
       if (a == 0 && b == 0)
         {
           printf("Illegal args to gcd: %d, %d\n",a,b);
           exit(1);
        }
    Because that's precisely what you've written the code to do. In fact it makes perfect sense for it to treat both arguments being zero as an error.
    If it didn't do that then you'd just get a division by zero afterwards instead. Which as it happens points towards the real problem in that you should not be calling gcd at all if the arguments aren't suitable for it, and also shouldn't be doing the subsequent division.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    Yes I understand why its returning the error but what I don't understand is why it's not passing in the correct numbers from the test file. Is there a way to post my complete code from all files? or a website to help with it? or do I just post it all in one message?

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    If it didn't do that then you'd just get a division by zero afterwards instead. Which as it happens points towards the real problem in that you should not be calling gcd at all if the arguments aren't suitable for it, and also shouldn't be doing the subsequent division.
    I don't really understand what you were saying here can you rephrase it for me please.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Which OS/Compiler are you using?
    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.

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    Quote Originally Posted by Salem View Post
    Which OS/Compiler are you using?
    I'm using Dev C++ on Windows 7 Ultimate...with the Mingw compiler

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by dadiezel07 View Post
    Yes I understand why its returning the error but what I don't understand is why it's not passing in the correct numbers from the test file. Is there a way to post my complete code from all files? or a website to help with it? or do I just post it all in one message?
    This is the first time you've mentioned that the input was coming from file. If you're having trouble reading data in correctly from file, then why didn't you post that part instead?

    We don't need to see everything. Just show us the code that reads it in from file, up to where it calls this bit of code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    Thank you all for all your replies. Turns out my teacher's test file was not working properly. He sent me the updated test file and everything worked beautifully!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. figuring out code
    By Thegame16 in forum C Programming
    Replies: 16
    Last Post: 09-23-2011, 06:47 PM
  2. Having trouble figuring out how ostream functors work
    By CodeMonkey in forum C++ Programming
    Replies: 3
    Last Post: 07-12-2011, 04:31 PM
  3. Trouble changing from Arrays to dynamic pointers
    By hpotsirhC in forum C Programming
    Replies: 13
    Last Post: 06-17-2011, 12:13 PM
  4. dynamic memory allocating error
    By valhall in forum C Programming
    Replies: 2
    Last Post: 04-04-2003, 10:49 AM
  5. Allocating dynamic memory for structs
    By Nevyn in forum C Programming
    Replies: 4
    Last Post: 09-17-2001, 11:54 AM