LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-19-2006, 01:26 AM   #1
rs_vijay
Member
 
Registered: Aug 2006
Location: Allahabad,India
Distribution: Fedora Core 5
Posts: 41

Rep: Reputation: 15
Thumbs up "glibc detected -> program aborted" plz help in debugging..


hi guyz..
i am new to program in the unix environment. I was coding a prog to implement merge sort algo here a fragment (as i feel might have caused the error) goes..
Code:
 void msort(int *array,int start,int end)
{
 if start=end then return back;
 if end-start=1 implies there are only two elements swap if necessary n return;
 msort(array,start,(start+end)/2);
 msort(array,(start+end)/2,end);
/* at this point we have two sorted arrays one frm start to(start+end)/2 rest the other*/
 /*create a dummy array of required size merge the two sorted arrays by comparing cunterpart elements n swapping if necessary n store in this dummy array*/
 /*now make array point to dummy*/ 
  array = dummy;
  /*at every recursive step dummy is newly created that fits the whole size of the array (i.e frm start to end) n so this shud not create a memory conflict as dummy is local to each function*/
 /* when everything is done array is sorted*/
}
i have written this code n running this gave me the error "glibc detected Aborted n there were some address variables i suppose

so as a primary step to debug i tried tracing the path at every new recursive step n so i printed the start and end values immediately after declarations n the final sorted array at the end of each recursive step n .
even then i found everything worked well n the steps were printed in order as the ought to be until this "glibc" was encountered

i feel that i amn't very precise till this step n so i post my code , so still if u are unable to figure out plz copy n paste this code n compile it so that u may get an idea where the real error goes..
here is the code...

Code:
	#include"stdio.h"
	#include"malloc.h"
	#include"stdlib.h"

	void msort(int *,int,int);

 main()
{
 int *arr,n,i;
 int start,end;
 printf("Input n (no. of elements in the array) : "); //n is size of array
 scanf("%d",&n);
 arr = (int *)malloc(4*n);
 for(i=0;i<n;i++)
    scanf("%d",arr+i);
 msort(arr,0,n-1);   //passing the entire arry with start=0 end=n-1
}

 void msort(int *p,int start,int end)
{
 int temp,i,mid=(start+end)/2,j;
 int *q=(int *)malloc(4*((end-start)+1)),*dummy;
 printf("Entered msort with start=%d end=%d\n",start,end);
 if(!q)
 {
   printf("failure allocating memory\n");
	exit(0);
 }
 if(end-start == 1 && start<end)     //array has only two elements
 {
	if(p[start] > p[end])
	{
		temp = p[start];
		p[start] = p[end];
		p[end] = temp;
	}
	return;
 }
 else if(start == end)   // array has a single element
	return;
 msort(p,start,(start+end)/2);
 msort(p,(start+end)/2+1,end);
 j=start;
 if(mid+1 == end)             //mid is (start+end)/2 n array has three elements
 {
	if(p[start+1] < p[end])
	{
		q[j++] = p[start];
		q[j++] = p[start+1];
		q[j++] = p[end];
	}
	else
	{
		if(p[start] < p[end])
		{
			q[j++] = p[start];
			q[j++] = p[end];
			q[j++] = p[start+1];
		}
		else
		{
			q[j++] = p[end];
			q[j++] = p[start];
			q[j++] = p[start+1];
		}
	}
 }
 else
 {	
	 for(i=start,j=start ; i<mid ; i++)
	 {
		if(p[i] > p[i+mid])
		{
			temp = p[i];
			p[i] = p[i+mid];
			p[i+mid] = temp;
		}
		q[j++] = p[i];
		q[j++] = p[i+mid];
	 }
}
 dummy=p;    //this is not the dummy array as i talked of earlier its rather q
 p=q;
 free(dummy);
 printf("Now array is : ");
 for(i=start;i<=end;i++)
   printf("%d ",p[i]);
 puts("");
}
hope now u got my problem
plzz help me n thnx in advance....
 
Old 11-19-2006, 02:02 AM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
main should always be 'int'
also in main, i think your scanf is wrong. you want to store the values in the array at those certain indices.
change the scanf loop (in main) and store the values in 'arr[i]' not 'arr + i'

sorry i dont have time right now to look at the rest, but change that and see if it helps at all.. good luck.
 
Old 11-19-2006, 03:24 AM   #3
rs_vijay
Member
 
Registered: Aug 2006
Location: Allahabad,India
Distribution: Fedora Core 5
Posts: 41

Original Poster
Rep: Reputation: 15
thnx for ur reply sir..

but return type of main defaults to int n that shud rather be a warning if i don't specify it
n secondly in scanf arr+i is nothing but &arr[i] n that shudn't cause a problem

so there's something wrong in the recursive part which needs to be trapped

plzz correct me if I am wrong.. n thnx for the same
 
Old 11-19-2006, 12:46 PM   #4
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
after the scanf and before the msort call can you verify that its filling the array properly by printing it? ive never used C, but i didnt know that was valid..
 
Old 11-20-2006, 04:41 AM   #5
rs_vijay
Member
 
Registered: Aug 2006
Location: Allahabad,India
Distribution: Fedora Core 5
Posts: 41

Original Poster
Rep: Reputation: 15
yeah it worked n that was valid ...
 
Old 11-20-2006, 07:25 PM   #6
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Hi rs_vijay,

I played a little with your source. I didn't find the solution but there are 2 things you should look:
  • you are allocating memory as q pointer at each iteration and freeing the p that you received as an argument; as the function is void, it does not return anything to the caller and the caller continues to work on a freed memory;
  • you are merging the elements of both halves of the array without verify if the array is multiple of 2; then you can access memory not allocated or you don't access all elements of the array.
I was looking at internet and the wikipedia discusses on the topic. The Portuguese page has examples in several programing languages. In the C example, the auxiliary array (p) is passed as an argument for use in all iterations of the sort.
 
Old 11-22-2006, 12:16 AM   #7
rs_vijay
Member
 
Registered: Aug 2006
Location: Allahabad,India
Distribution: Fedora Core 5
Posts: 41

Original Poster
Rep: Reputation: 15
thanx sir..

i was thinking of some sort of problem like this , i have just removed the code fragment that frees the pointer as u mentioned n that worked for me

actually this caused the error as u quoted there
Code:
free(dummy)
n now its working fine thnx for ur suggestions and related articles..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
helix player not working says "*** glibc detected *** double free or corruption(out)" shyamsundar Linux - Newbie 0 09-05-2006 02:14 PM
Can't exec "firefox 1.5", "prompts glibc detected" SPo2 Linux - General 1 06-04-2006 11:02 PM
Wine error: "glibc detected ... invlaid pointer" resaguk Slackware 2 11-08-2004 09:06 AM
wish "ABORTED" tcl/tk 8.4 gentoo 1.4 Daem0hn Linux - Software 2 03-16-2004 05:12 AM
mutt "Aborted unmodified message." tommyj27 Linux - Software 0 05-16-2003 02:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration