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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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.

  3. #3
    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

  4. #4
    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?

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