# Euclid Algorithm (extended)Part2 Doubt about pointers - INITIALIZATION

• 12-10-2006
nacho4d
Euclid Algorithm (extended)Part2 Doubt about pointers - INITIALIZATION
As you know..., I have been trying to implement the Euclid Algorithm (finds the Greates Common Divisor)in C using functions, structures, and pointers...

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; }```
ans when running:
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\$```
but i still have a doubt..

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; }```
debuggers result:
(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```
Before I was told that my pointers were not initialized.. but... I still dont get it. How can I initialize them? using malloc? if malloc is the solution... can anyone tell me the answer please?

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
• 12-10-2006
sl4nted
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;```
this structure is 3 ints, so approximatly 96 bits of information.

so you start calling
Code:

```                euclidp->m=m;                 euclidp->a=1;                 euclidp->b=0;```
the computer says to itself, I have to start at the address stored in euclidp (since its a pointer). and set the first 32 bits to the integer m. then I have to goto the next 32 bits and set that integer to 1. But wait you didn't give euclipd a known value. So 1 who knows where it will look for the first 32 bits, and b, The 2nd 32 bits are god knows what. You may not even be allowed to be looking at the memory. So you use malloc to allocate a set amount of memory. It then returns to you the starting address of that memory. euclidp = malloc(96) now the pointer has an address that you know has 96 bits of your own memory. whats beyond those 96 bits. Donno, might be yours might not.
• 12-10-2006
ssharish2005
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
• 12-10-2006
vart
the problem only with a lot of memory leaks
• 12-10-2006
nacho4d
Thanks
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.

:)