Incompatibility of C compiler between Inix and Linux
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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]++;
}
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.
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.
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.