Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Problem porting C program form redhat6.1 to redhat5.2

Posted on 2000-03-24
6
262 Views
Last Modified: 2010-05-18
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?
0
Comment
Question by:ruadhan
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:marcjb
ID: 2653220
void main(void)
{
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
0
 
LVL 10

Expert Comment

by:makerp
ID: 2653256
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...
0
 

Author Comment

by:ruadhan
ID: 2653381
even with the NULL assignment the segmentation fault occurs
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 10

Accepted Solution

by:
makerp earned 50 total points
ID: 2653510


void main(void)
{
int x;
int *y;

//the problem occurs in the next line
x=*y;
}


your problem is as follows.

x is an automatic ingeter that has been decalred but given no value (fine)

y is a pointer variable that at present points to an undefined area of memory. (fine)

you then make the contents of x equal what the *y points at. which is nothing. even if it pointed to null this would still cause an error bescase you would be making the value of x equal to value of NULL or address x000000000.

what you need to do is first of all allocate some memory for *y and then assign it a value. OR make y point to another pointer which is valid

i.e

int *y;

y = (int*)malloc(sizeof(int));
if(y)
{
     *y = 10;
}

x = *y;

now y is pointing to a valid owned piece of memory.

even if you did not assign *y a value an error would not occur, x would just have an unpredictable result in. this is because a malloc does not set the contents of a piece of memeory it just give you it, therefore referncing it before an assignment will yield its previous contents.
 
0
 

Author Comment

by:ruadhan
ID: 2653596
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.
0
 
LVL 10

Expert Comment

by:makerp
ID: 2653633
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 ??



 
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Intel debugger, idb, only seeing .c and .h, but no .f90 1 368
delete-remove 14 97
A  &&  B  || (C||D) how it works in c 7 101
C qsort compare function issue 6 16
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question