C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-08-2009, 07:14 PM   #1
Registered User
 
Join Date: Oct 2009
Posts: 14
Segmentation Fault?

I realize a segmentation fault occurs when trying to access restricted memory. However, I have no clue why this is happening. Here's my code. It's converting a decimal number to a binary number.

Code:
void tobinary(int x, char s[]);

int main() {
	char hold[4];
	tobinary(5, hold);
	
	return 0;
}



void tobinary(int x, char s[]){
	
	int k=0,n=0;
	int neg=0;
	int sLength=sizeof(s);
	int remain;
	int temp[sLength];
	
	if (x<0) {
		x=-x;
		neg=1;
	}
		
	do {
		remain=x%2;
		x=x/2;
		temp[k++]=remain+'0';
	} while (x >0);

	if(neg) {
		temp[k++]='-';
	}
	else {
		temp[k++]=' ';
	}
	
	while (k>=0) {
		s[n++]=temp[--k];
	}
	
	s[n-1]=0;	

	k=0;
	while (k<4) {
		printf("%d", s[k]);
		k++;
	}

}
TIMBERings is offline   Reply With Quote
Old 10-08-2009, 07:25 PM   #2
...
 
kermit's Avatar
 
Join Date: Jan 2003
Posts: 1,190
Code:
sLength=sizeof(s);
That bit there is going to cause some trouble for you. sizeof(s) gives you the size of a pointer on your machine. If you want the size of an array which is outside of the function, you will need to calculate the size and then pass that into the function as a parameter.

Read 6.1-6.4b here to get an idea of why this is so.
__________________
Got Ed?

sys-sizes

Last edited by kermit; 10-08-2009 at 07:28 PM.
kermit is offline   Reply With Quote
Old 10-08-2009, 07:25 PM   #3
Registered User
 
jdragyn's Avatar
 
Join Date: Sep 2009
Posts: 30
sizeof() only works on an array in the scope that the array was defined. You define your array in main() and then use sizeof() in a different function, which won't work. In this case it returns the size of a pointer (which is what it sees s as) instead of the size of the array you allocated in main().

You could pass a 3rd argument to tobinary() to tell it specifically how big to make your temp array.
jdragyn is offline   Reply With Quote
Old 10-08-2009, 07:29 PM   #4
Registered User
 
Join Date: Oct 2009
Posts: 14
I removed sizeof() and instead passed an integer, still get a segmentation fault
TIMBERings is offline   Reply With Quote
Old 10-08-2009, 07:30 PM   #5
...
 
kermit's Avatar
 
Join Date: Jan 2003
Posts: 1,190
Post your modified code.
__________________
Got Ed?

sys-sizes
kermit is offline   Reply With Quote
Reply

Tags
code, fault, segmentation

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault problem odedbobi Linux Programming 1 11-19-2008 03:36 AM
Segmentation fault bennyandthejets C++ Programming 7 09-07-2005 05:04 PM
Segmentation fault NoUse C Programming 4 03-26-2005 03:29 PM
Locating A Segmentation Fault Stack Overflow C Programming 12 12-14-2004 01:33 PM
Segmentation fault... alvifarooq C++ Programming 14 09-26-2004 12:53 PM


All times are GMT -6. The time now is 07:23 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