pointers : segmentation fault under Linux

Discussion in 'C++' started by Gregor Rot, Jan 7, 2004.

  1. Gregor Rot

    Gregor Rot Guest

    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
     
    Gregor Rot, Jan 7, 2004
    #1
    1. Advertising

  2. > 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
     
    Ernst Murnleitner, Jan 7, 2004
    #2
    1. Advertising

  3. Gregor Rot

    Gregor Rot Guest

    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
     
    Gregor Rot, Jan 7, 2004
    #3
  4. Gregor Rot

    Gregor Rot Guest

    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
     
    Gregor Rot, Jan 7, 2004
    #4
  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
     
    Martijn Lievaart, Jan 7, 2004
    #5
  6. Gregor Rot

    Chris Dams Guest

    Hi!

    Gregor Rot <> writes:

    >any other idea?


    Run the program in the debugger?

    Good luck,
    Chris Dams
     
    Chris Dams, Jan 7, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley
    Replies:
    17
    Views:
    896
  2. Joel
    Replies:
    4
    Views:
    9,338
    John Harrison
    Oct 11, 2004
  3. damian birchler

    having problems with pointers (segmentation fault)

    damian birchler, Oct 7, 2004, in forum: C Programming
    Replies:
    6
    Views:
    399
    Barry Schwarz
    Oct 8, 2004
  4. Jess
    Replies:
    27
    Views:
    811
    Default User
    Apr 27, 2007
  5. zm
    Replies:
    2
    Views:
    122
Loading...

Share This Page