LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 12-17-2004, 03:57 AM   #1
ss100
LQ Newbie
 
Registered: Dec 2004
Location: areepparampu
Posts: 17

Rep: Reputation: 0
Incompatibility of C compiler between Unix and Linux


Hello to all,

I had written a C program in an old Unix machine about six months ago which was running fine. Now the disk has been corrupted and I cannot access this program. However I had made a spare copy of this program in a Linux machine. On checking I find that the code is exactly the same (checking with a hard copy) but there is a segmentation error on running (compilation is OK). The program is about 500 lines long.
Kindly give me a few hints as to what I should check for to diagoize the problem.

Sincerely yours,

A. S. Padmanabhan,
Professor of Physical Chemistry

Last edited by ss100; 12-17-2004 at 04:20 AM.
 
Old 12-17-2004, 04:20 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Can you post a link to the code?
 
Old 12-17-2004, 05:01 AM   #3
ss100
LQ Newbie
 
Registered: Dec 2004
Location: areepparampu
Posts: 17

Original Poster
Rep: Reputation: 0
I have narrowed down my debugging to around 40 lines of code. But now I am flummoxed!! It runs perfectly well on Unix but not with Linux
/* this program is final Flory Huggins program, for a finite bounday, not necessarily symmetric about the origin.It handles big numbers*/
#include<stdio.h>
#define maxs 15
#define maxsp maxs+1
#define maxd 8
int p[maxsp][maxd],walk[maxs];
int steps,dim,nosw;
float avgdis;

void get_inp();
void put_res();
void gen_sarw();
int next_walk();
int next_step();
int sa();
int diff();
void set_pt();
float dis();
void initialise();
main()
{
get_inp();
gen_sarw();
put_res();
}
void get_inp()
{
printf("Enter the dimension and no: of steps\n");
scanf("%d%d",&dim,&steps);
}
void put_res()
{
printf("\nThe no: of self-avoiding random walks is %d ",nosw);
printf("\nThe average end to end distance is %f \n",avgdis);
}
void gen_sarw()
{
float sum;
int i;
initialise();
sum=dis();
while(next_walk(steps-1))
{
nosw++;
sum+=dis();
}
avgdis=sum/nosw;
}
void initialise()
{
int i,j;
for(i=0;i<steps;i++) walk[i]=-1;
for(i=0;i<=steps;i++)
{ p[i][0]=-i;
for(j=1;j<dim;j++) p[i][j]=0;
}
nosw=1;
}
float dis()
{
int i;
float sum=0.0;
for(i=0;i<dim;i++) sum+=p[steps][i]*p[steps][i];
return sum;
}
int next_walk(i)
int i;
{
if(next_step(i))
{
if(i==(steps-1)) return 1;
else next_walk(i+1);
}
else
{
if(i==0) return 0;
else next_walk(i-1);
}
}
int next_step(i)
int i;
{
if(walk[i]==dim) {walk[i]=0;return 0;}
else
{
if(walk[i]<0) walk[i]=-walk[i];
else walk[i]=-(++walk[i]);
if(sa(i)) return 1;
else next_step(i);
}
}
int sa(i)
int i;
{
int j=0,ok=1;
set_pt(i+1);
while(ok&&(j<i)) ok=ok && diff(i+1,j++);
return ok;
}
int diff(i,j)
int i,j;
{
int d=0,k=0;
while(!d && (k<dim)){
d=d||(p[i][k]!=p[j][k]);k++;}
return d;
}
void set_pt(i)
int i;
{
int j;
for(j=0;j<dim;j++) p[i][j]=p[i-1][j];
j=walk[i-1];
if(j<0) p[i][-j-1]--;
else p[i][j-1]++;
}

 
Old 12-17-2004, 05:06 AM   #4
kees-jan
Member
 
Registered: Sep 2004
Distribution: Debian, Ubuntu, BeatrIX, OpenWRT
Posts: 273

Rep: Reputation: 30
I suggest obtaining a core dump (check ulimit to enable them), and then do post-mortem debugging using gdb. That should tell you the location of the error.

Groetjes,

Kees-Jan
 
Old 12-17-2004, 05:13 AM   #5
ss100
LQ Newbie
 
Registered: Dec 2004
Location: areepparampu
Posts: 17

Original Poster
Rep: Reputation: 0
Thanks,
Will get back to you, if I need to.
A. S. Padmanabhan
Professor in Physical Chemsitry
 
Old 12-17-2004, 12:04 PM   #6
bm17
Member
 
Registered: Sep 2004
Location: Santa Cruz, CA, USA
Distribution: Redhat 9.0
Posts: 104

Rep: Reputation: 15
I built the code (with gcc 3.4) and ran it (with dim=2, steps=3) and it just sat there chewing up CPU. I killed it after a minute or so.
 
Old 12-17-2004, 07:44 PM   #7
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Please post the code in code tags so the indentations are preserved -- that code is nearly impossible to read! However, since I don't see anything with pointers, my guess is that one of your array indeces is going out of bounds and accessing memory that does not belong to you.

Because of the vagaraties of how your code actually executes (stack frames, buffers and padding in the program mempory space, etc.) this may not cause a crash on one C implementation (it's still an error though), but cause a crash on another. I've seen this happen before. My advice would be to step through the program in gdb and see if one of your array indices is going out of bounds.
 
Old 12-20-2004, 12:54 AM   #8
ss100
LQ Newbie
 
Registered: Dec 2004
Location: areepparampu
Posts: 17

Original Poster
Rep: Reputation: 0
Hellol all,
Sorry for the logn silence. I go back to work from the week end ony today. I used the suggestions made to fix the problem. There three differences between the two implementations of the c compiler (not three erors in my program) theat i found. In case anyone is interested:
(1) The newer version of REDHAT that I use has a more accurate implementation of the recursion facility. In my program I had to make two changes to make it work:
(a)In the subroutine int next_walk(i) the last line should read else return next_walk(i-1) nd not next_walk(i-1). Earlier versions of Unx and some versions of Linux (LInux Mandrake) were returning the value to the calling program even when they were not asked to.
(b)ditto in the program next_step(i) the last line shoud read return next_step(i) and not next_step(i)
(2) in subroutine int sa(i) the line :
while (ok&&(j<i)) ok=ok&&diff(i=1,j++) should be ignored if i=0. However it is not being ignored but is being set to 0 and then being retured which subsequently causes the program to fail. I fixed this by changing (j<i) to (j<=i) but I am not happy about this as I am wasting cpu time in large calculations. If any one has any suggestion I would be grateful to hear a reply.
If these THREE corrections are made the program wil run and for dim = 2 and steps =
3 36
4 100
5 284
6 780
7 2172
8 5916
9 16268
Further values can be obtained from the back pages of the book 'The Self-Avoiding Walk" by Neal Madras and Gordon Slade from Birkhausser.
Thaks a lot,
A. S. Padmanabhan,
Professor of Physical Chemistry
 
Old 12-20-2004, 03:24 AM   #9
ss100
LQ Newbie
 
Registered: Dec 2004
Location: areepparampu
Posts: 17

Original Poster
Rep: Reputation: 0
Hello all once again,
Sorry about the poor spelling and grammar in my previous reply. It was written in a hurry.
I just found that the third correction that I had mentioned in my last posting was unnecessary if the first two are taken care off. That is the problem with the subroutine sa() disappears if the first two are taken care off. So there are only two changes required i.e. the last line of next_walk() is else return next_walk(i-1) and not return next_walk(i-1) and the last line of next_step() is else return next_step(i) and not else next_step(i).
So what it boils down to is should an int or float subroutine return a value even when it is not asked to. I would have thought the answer is yes. So may be the earlier versions of Linux and Unix were right after all.
A. S. Padmanabhan
Professor of Physical Chemistry
 
  


Reply



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
Router incompatibility? PenguinPride Linux - Networking 1 11-19-2005 02:32 PM
PDF incompatibility Linux Mac OS X lel800 Linux - Software 7 12-20-2004 07:11 PM
Incompatibility of c compiler between unix and Linux ss100 Linux - Software 3 12-17-2004 08:33 AM
GTK 1.xx-2.4 incompatibility GDuru Linux - Newbie 1 10-18-2003 05:21 PM
pavilion linux incompatibility dennis_demski Linux - Software 2 02-11-2002 10:14 AM

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

All times are GMT -5. The time now is 12:48 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