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

1. ## 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;
}
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. 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;
}```

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.

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. How do I post the complete code? It's in a few different files? Do I just post it all in one message?

6. 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.

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. 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. Which OS/Compiler are you using?

10. Originally Posted by Salem
Which OS/Compiler are you using?
I'm using Dev C++ on Windows 7 Ultimate...with the Mingw compiler