Segmentation Fault?

This is a discussion on Segmentation Fault? within the C Programming forums, part of the General Programming Boards category; I realize a segmentation fault occurs when trying to access restricted memory. However, I have no clue why this is ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    44

    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++;
    	}
    
    }

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    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.
    Last edited by kermit; 10-08-2009 at 08:28 PM.

  3. #3
    Registered User jdragyn's Avatar
    Join Date
    Sep 2009
    Posts
    93
    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.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    44
    I removed sizeof() and instead passed an integer, still get a segmentation fault

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Post your modified code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 06:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 01:53 PM

Tags for this Thread


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