![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Nov 2006 Location: japan
Posts: 104
| (Euclid Algorithm (extended)) Well, I realized that there is no necesity of pointers, so I did it without them. Here is the code: Code: //1206_2_03school.c
#include<stdio.h>
#include<math.h>
typedef struct euclid{
int m;
int a;
int b;
}Euclid;
Euclid ext_gcd(int m, int n){
Euclid euclidp, euclidp2;
if(n==0) {
euclidp.m=m;
euclidp.a=1;
euclidp.b=0;
printf("ext_gcd(%d,%d)=%d=(%d)(%d)+(%d)(%d)\n",m,n,euclidp.m,m,euclidp.a,n,euclidp.b);
return euclidp;
}
euclidp=ext_gcd(n, m%n);
euclidp2.m=euclidp.m;
euclidp2.a=euclidp.b;
euclidp2.b=(euclidp.a)-floor(m/n)*(euclidp.b);
printf("ext_gcd(%d,%d)=%d=(%d)(%d)+(%d)(%d)\n",m,n,euclidp2.m,m,euclidp2.a,n,euclidp2.b);
return euclidp2;
}
int main(){
printf("Euclid Algorithm (Extended)\n");
ext_gcd(954,285);
return 0;
}
Code: i222-151-15-224:~/Desktop nacho$ gcc 1206_2_03school.c i222-151-15-224:~/Desktop nacho$ ./a.out Euclid Algorithm (Extended) ext_gcd(3,0)=3=(3)(1)+(0)(0) ext_gcd(12,3)=3=(12)(0)+(3)(1) ext_gcd(87,12)=3=(87)(1)+(12)(-7) ext_gcd(99,87)=3=(99)(-7)+(87)(8) ext_gcd(285,99)=3=(285)(8)+(99)(-23) ext_gcd(954,285)=3=(954)(-23)+(285)(77) i222-151-15-224:~/Desktop nacho$ when I try to change the code in order to use pointers and try to compile it, there is a bus error. Here is my code and the debbuger result. Code: #include<stdio.h>
#include<math.h>
typedef struct euclid{
int m;
int a;
int b;
}Euclid;
Euclid ext_gcd(int m, int n){
Euclid *euclidp, *euclidp2;
if(n==0) {
euclidp->m=m;
euclidp->a=1;
euclidp->b=0;
printf("ext_gcd(%d,%d)=%d=(%d)(%d)+(%d)(%d)\n",m,n,euclidp->m,m,euclidp->a,n,euclidp->b);
return *euclidp;
}
*euclidp=ext_gcd(n, m%n);
euclidp2->m=euclidp->m;
euclidp2->a=euclidp->b;
euclidp2->b=(euclidp->a)-floor(m/n)*(euclidp->b);
printf("ext_gcd(%d,%d)=%d=(%d)(%d)+(%d)(%d)\n",m,n,euclidp2->m,m,euclidp2->a,n,euclidp2->b);
return *euclidp2;
}
int main(){
printf("Euclid Algorithm (Extended)\n");
ext_gcd(954,285);
return 0;
}
(GNU debugger, and other data about my compiler, machine, etc is below) Code: i222-151-15-224:~/Desktop nacho$ gcc 1206_2.c i222-151-15-224:~/Desktop nacho$ ./a.out Euclid Algorithm (Extended) Bus error i222-151-15-224:~/Desktop nacho$ gcc 1206_2.c -o 1206_2 i222-151-15-224:~/Desktop nacho$ gdb 1206_2 GNU gdb 6.3.50-20050815 (Apple version gdb-563) (Wed Jul 19 05:10:58 GMT 2006) Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-apple-darwin"...Reading symbols for shared libraries .. done (gdb) run Starting program: /Users/nacho/Desktop/1206_2 Reading symbols for shared libraries . done Euclid Algorithm (Extended) Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_PROTECTION_FAILURE at address: 0x00000000 0x00001d9b in ext_gcd () (gdb) quit The program is running. Exit anyway? (y or n) y I feel that with this problem I have understood Structures, typesDefinitions..., functions that return a user defined type..., members, etc. but I still have a doubt about pointers... I would like to get the same result as the first program. But with pointers. Thanks |
| nacho4d is offline | |
| | #2 |
| Registered User Join Date: Nov 2006
Posts: 176
| I think you should read the FAQ on pointers. but in short a pointer is an address, so when you do Euclid *euclidp, *euclidp2; you have 2 variables that are lets say 32 bits each. what they start as, who knows. Code: typedef struct euclid{
int m;
int a;
int b;
}Euclid;
so you start calling Code: euclidp->m=m; euclidp->a=1; euclidp->b=0; Last edited by sl4nted; 12-10-2006 at 04:54 AM. |
| sl4nted is offline | |
| | #3 |
| Protocol Test Engineer Join Date: Sep 2005 Location: fseek(UK)
Posts: 1,316
| Code: Euclid *ext_gcd(int m, int n)
{
Euclid *euclidp, *euclidp2;
euclidp = malloc(sizeof(Euclid));
euclidp2 = malloc(sizeof(Euclid));
if(n==0)
{
euclidp->m=m;
euclidp->a=1;
euclidp->b=0;
printf("ext_gcd(%d,%d)=%d=(%d)(%d)+(%d)(%d)\n",m,n,euclidp->m,m,euclidp->a,n,euclidp->b);
return euclidp;
}
euclidp=ext_gcd(n, m%n);
euclidp2->m=euclidp->m;
euclidp2->a=euclidp->b;
euclidp2->b=(euclidp->a)-floor(m/n)*(euclidp->b);
printf("ext_gcd(%d,%d)=%d=(%d)(%d)+(%d)(%d)\n",m,n,euclidp2->m,m,euclidp2->a,n,euclidp2->b);
return euclidp2;
}
|
| ssharish2005 is offline | |
| | #4 |
| CSharpener Join Date: Oct 2006
Posts: 5,242
| the problem only with a lot of memory leaks
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
| | #5 |
| Registered User Join Date: Nov 2006 Location: japan
Posts: 104
| Yeah... You are right, I should have read the pointers FAQ before,..., I read something more about pointers and could do it by myself some hours ago. But thank you all People! And Certaintly.. there is Memory Leak, That's is why I did it without pointers before. But I still wanted to learn about pointers. |
| nacho4d is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| pointers | InvariantLoop | C Programming | 13 | 02-04-2005 09:32 AM |
| Binary Search Trees Part III | Prelude | A Brief History of Cprogramming.com | 16 | 10-02-2004 03:00 PM |
| Request for comments | Prelude | A Brief History of Cprogramming.com | 15 | 01-02-2004 10:33 AM |
| Euclid Algorithm | Unregistered | C Programming | 2 | 07-01-2002 10:27 PM |