473,382 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

pointers : segmentation fault under Linux

Hi, i have this simple data structure:

struct nodeProject {
char key[100];
char name[1000];
struct linkProject *plink;
};

struct linkProject {
struct nodeProject *node;
struct linkProject *next;
};
Trough the program i make a structure, but then if i want to access
let's say:

nodeProject->linkProject->nodeProject->name

(nodeProject and linkProject are used just to make clear what data types
i have, otherwise that are pointers) i get an error under LINUX (GCC),
but under WIndows everything works fine.

If the "depth" doesn't involve linkProject, everything works fine (ex.
nodeProject->name).

Any idea? I think i got that data structure wrong somehow.

Tnx,
Greg

Jul 22 '05 #1
5 1574
> struct nodeProject {
char key[100];
char name[1000];
struct linkProject *plink;
};

struct linkProject {
struct nodeProject *node;
struct linkProject *next;
};
Trough the program i make a structure, but then if i want to access
let's say:

nodeProject->linkProject->nodeProject->name

-----------------------------|
If it doesn`t work left of --| , I guess you haven't initialized linkProject
pointer.
So it writes somewhere into the memory, and a segmentation fault can be
thrown or not, depending on the value which the pointer has.
I always use a constructor in oder to set the pointers to 0 and test for
validy:

if (nodeProject && nodeProject->linkProject && ... )
nodeProject->linkProject->nodeProject->name ...

Greetings
Ernst
Jul 22 '05 #2
Ernst Murnleitner wrote:
struct nodeProject {
char key[100];
char name[1000];
struct linkProject *plink;
};

struct linkProject {
struct nodeProject *node;
struct linkProject *next;
};
Trough the program i make a structure, but then if i want to access
let's say:

nodeProject->linkProject->nodeProject->name


-----------------------------|
If it doesn`t work left of --| , I guess you haven't initialized linkProject
pointer.
So it writes somewhere into the memory, and a segmentation fault can be
thrown or not, depending on the value which the pointer has.
I always use a constructor in oder to set the pointers to 0 and test for
validy:

if (nodeProject && nodeProject->linkProject && ... )
nodeProject->linkProject->nodeProject->name ...

Greetings
Ernst

I am afraid i have, under Windows (Dev-C++) everything works fine :-(
and the values are printed out correctly (name values).

any other idea?

br,
Greg

Jul 22 '05 #3
Gregor Rot wrote:
Ernst Murnleitner wrote:
struct nodeProject {
char key[100];
char name[1000];
struct linkProject *plink;
};

struct linkProject {
struct nodeProject *node;
struct linkProject *next;
};
Trough the program i make a structure, but then if i want to access
let's say:

nodeProject->linkProject->nodeProject->name

-----------------------------|
If it doesn`t work left of --| , I guess you haven't initialized
linkProject
pointer.
So it writes somewhere into the memory, and a segmentation fault can be
thrown or not, depending on the value which the pointer has.
I always use a constructor in oder to set the pointers to 0 and test for
validy:

if (nodeProject && nodeProject->linkProject && ... )
nodeProject->linkProject->nodeProject->name ...

Greetings
Ernst

I am afraid i have, under Windows (Dev-C++) everything works fine :-(
and the values are printed out correctly (name values).

any other idea?

br,
Greg


Jupy, it works! Your advice helped. I checked every pointer connection
and found out, that because i was reading data frm a file and checking
#10 for end of line, under Linux the pointer structure didn't fill up
(Linux doesn't use #10#13 and EOLN only #13 i suppose?)

tnx,
br,
Greg

Jul 22 '05 #4
Hi!

Gregor Rot <za*********@yahoo.com> writes:
any other idea?


Run the program in the debugger?

Good luck,
Chris Dams
Jul 22 '05 #5
On Wed, 07 Jan 2004 17:22:25 +0100, Gregor Rot wrote:
Jupy, it works! Your advice helped. I checked every pointer connection
and found out, that because i was reading data frm a file and checking
#10 for end of line, under Linux the pointer structure didn't fill up
(Linux doesn't use #10#13 and EOLN only #13 i suppose?)


That is the kind of stuf that using text files shields you from :-)

You seem to have a text file that you open in binary mode. Sometimes text
and binary are mixed in one file, but in that case you have to take care
to define the text format. As you found out, text formats differ from OS
to OS in the way they indicate EOL (f.i. Unix uses '\n', dos/windows uses
"\n\r", classic mac used '\r'). Opening a file in text mode translates
this on the fly to '\n'.

If you do have a mixed text/binary file, define the text format yourself.
Either use '\n' because it is easier in C/C++, or use "\r\n" because most
of the internet protocols use it.

If you don't have a mixed file, use text mode and all will be easy and
well.

BTW, you might do some more error checking, this should have been cought
by the routine reading the file.

HTH,
M4
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script...
5
by: Allan | last post by:
Hi all, I've a C program which was originally developed under Sun Solaris(OS,2.6,gcc 2.95). I'm trying to migrate it to our linux system(Mandrake 9.0,2.4.19,gcc 3.2). Following is the...
9
by: Narendran Kumaraguru Nathan | last post by:
Hi all, I am fairly experianced in C. I am writing a program in which I'm getting a segmentation fault. The problem is that it is getting the segmentation fault when executing calloc. I tried...
3
by: I_have_nothing | last post by:
Hi! I am new in C. I got a lots of "Segmentation Fault"s in my code. I guess One possibility is: if " int array_i; " is declard and the code trys to access "array_i", a Segmentation Fault will...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
5
by: silverburgh.meryl | last post by:
Hi, I have a segmentation fault in line 66 of GroupResult.h and I can't figure out why that causes any problem, I appreciate if anyone can help. line 66 of Result.h: 66 size_t size()...
3
by: Kiran | last post by:
Hello All, In my program, I have a main thread which is the GUI (wxPython) and then a thread which goes and reads data from a socket. The reason this is in a different thread is because the data...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.