C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-10-2006, 04:19 AM   #1
Registered User
 
Join Date: Nov 2006
Location: japan
Posts: 104
Question 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...
(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;
}
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
nacho4d is offline   Reply With Quote
Old 12-10-2006, 04:50 AM   #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;
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.

Last edited by sl4nted; 12-10-2006 at 04:54 AM.
sl4nted is offline   Reply With Quote
Old 12-10-2006, 05:34 AM   #3
Protocol Test Engineer
 
ssharish2005's Avatar
 
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
ssharish2005 is offline   Reply With Quote
Old 12-10-2006, 07:05 AM   #4
CSharpener
 
vart's Avatar
 
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   Reply With Quote
Old 12-10-2006, 07:08 PM   #5
Registered User
 
Join Date: Nov 2006
Location: japan
Posts: 104
Talking 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.

nacho4d is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 08:43 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22