segmentation fault...please help

This is a discussion on segmentation fault...please help within the C Programming forums, part of the General Programming Boards category; the following problem sorts all sequences of k elements in a descending order...the algorithms are correct, as i tested them ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    16

    Unhappy segmentation fault...please help

    the following problem sorts all sequences of k elements in a descending order...the algorithms are correct, as i tested them on a static allocation program...
    i am having trouble with pointers and parameters transmition...
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    void sort(int*v,int m,int k)
    {
    	int ok=0;
    	int last =m+k-1, aux, i;
    	while(!ok) {
    		ok=1;
    		for(i=m; i<last; i++)
    			if(v[i]<v[i+1]) { aux=v[i];
    					     v[i]=v[i+1];
    					     v[i+1]=aux;
    					     ok=0;
    					}
    		last-=1;
    	}
    }
    
    void reading(int*v,int*n)
    {
    	int i;
    	printf("n= ");
    	scanf("%d",n);
    	v=(int*)malloc(*n*sizeof(int));
    	if(v==NULL)
    	printf("allocation error!");
    	for(i=0;i<*n;i++) {
    		printf("v[%d]= ",i);
    		scanf("%d",&v[i]);
    	}
    }
    
    void printing(int*v,int n)
    {
    	int i;
    	for(i=0;i<n;i++)
    		printf("%d ",v[i]);
    }
    
    
    void sort_array(int*v,int n,int k)
    {
    	int j=0;
    	while(j<n-k)
    	{
    		sort(v,j,k);
    		j+=k;
    	}
    	
    }
    
    int main()
    {
    	int n, k;
    	int*v;
    	printf("k= ");
    	scanf("%d",&k);
    	reading(v,&n);
    	sort_array(v,n,k);
    	printing(v,n);
    	printf("\n");
    	return 0;
    }
    i am compiling and executing under linux...

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    used gdb or some printf statements to locate the segfault.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you need to pass v by pointer into reading function
    currectly changes made inside reading function to this var are not visible outside the function
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You should malloc v in main(), or use reading() to return a pointer, because re-assigning the variable there (v=) will not be valid outside reading() unless the new address is returned.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    16
    thanks, guys now my program runs correctly, only that after execution says :null pointer assignment i have done the allocation in function main

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by liaa View Post
    thanks, guys now my program runs correctly, only that after execution says :null pointer assignment i have done the allocation in function main
    show your last code
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    16
    thanks, vart, i figured it out

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

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