ruadhan
asked on
Problem porting C program form redhat6.1 to redhat5.2
I have written a C programand compiled with the version of gcc which comes with redhat6.1 - I need to use the code on a machine which has redhat5.2 installed. It runs fine on 6.1 but gives a segmentation fault on 5.2. here's the code:
void main(void)
{
int x;
int *y;
//the problem occurs in the next line
x=*y;
}
The access to the *y pointer causes a segmentation fault on 5.2 but works fine on 6.1 Can anybody help?
void main(void)
{
int x;
int *y;
//the problem occurs in the next line
x=*y;
}
The access to the *y pointer causes a segmentation fault on 5.2 but works fine on 6.1 Can anybody help?
thats right your *y pointer should be at least set to NULL
i.e.
int *y = NULL;
at the moment your pointer could be pointing any whare in memeory. i.e a segment of memeory that you dont own etc...
i.e.
int *y = NULL;
at the moment your pointer could be pointing any whare in memeory. i.e a segment of memeory that you dont own etc...
ASKER
even with the NULL assignment the segmentation fault occurs
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hi makerp - the new code works. it still bugs me though, why it the original code works correctly in redhat6.1 and slackware, but not in redhat5.2. any ideas? (the gcc versions in rh5.2 and slackware are the same, but differnt i think in rh6.1)
cheers.
cheers.
possibly because the compilier has intervined in redhat6.1 and given x 0, cos y = NULL. NULL does equate to 0;
OR
Luck/Missfortune
OR
the memory that an unitilalized pointer points to in redhat6.1 is diiferent than the other compiler.
to be honest the redhat6.1 compiler should have bomded out on this ??
OR
Luck/Missfortune
OR
the memory that an unitilalized pointer points to in redhat6.1 is diiferent than the other compiler.
to be honest the redhat6.1 compiler should have bomded out on this ??
{
int x;
int *y;
//the problem occurs in the next line
x=*y;
}
This probably causes the segmentation fault because the pointer y is not initialized. I assume that the results of such actions are technically undefined, meaning that different builds of the code will behave differently, and can have adverse affects. The pointer should be initialized before it is used.
Also, it should be noted that
void main
is incorrect according to the C Standard. main should return an int, or some systems may experience problems (See Plauger's book on the C Standard).
Hope this helps,
Marc