pointers : segmentation fault under Linux

G

Gregor Rot

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
 
E

Ernst Murnleitner

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
 
G

Gregor Rot

Ernst said:
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
 
G

Gregor Rot

Gregor said:
Ernst said:
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
 
M

Martijn Lievaart

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top