Thread: Access Violation / Seg Fault

  1. #16
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Check for NULL before accessing any pointer ever, Korhedron. Your lead node should be seg faulting.

  2. #17
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by dwks View Post
    Never, ever call a destructor directly.
    There is only but one precious exception to this rule that I have encountered on a regular basis. Using the placement new operator. If you don't know what that even is, then you should not be toying with manual destructors.

  3. #18
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Thanks for all the help ! I've solved that last problem by side-stepping it - checking for NULL, and if so, just skipping the action - which isn't technically correct in the game, but I'll fix that later. Right now, as the debugger keeps pointing out segfaults as I continue, I'm more worried about the fact that the program is starting to look like Swiss cheese. I clearly haven't catered for the exception of a node being the leading or trailing node, so I need a way to cater for it now.

    You'll probably recommend std::list again, but knowing these lists will probably change at some point - is there anything else you'd suggest ? Should I write a list class and inherit my Angel and Mortal classes from that ?

    Thanks again for all the great help, I'm learning a lot,
    Q

  4. #19
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    There is only but one precious exception to this rule that I have encountered on a regular basis. Using the placement new operator. If you don't know what that even is, then you should not be toying with manual destructors.
    Yes, it's true. http://yosefk.com/c++fqa/dtor.html#fqa-11.9
    But I think we can safely assume that the OP doesn't need to know that at the moment.

    [edit]
    Should I write a list class and inherit my Angel and Mortal classes from that ?
    That sounds like a plausible idea. However, it would make more sense to create a list class that can handle Angels and Mortals. Are you familiar with templates? [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by master5001 View Post
    There is only but one precious exception to this rule that I have encountered on a regular basis. Using the placement new operator. If you don't know what that even is, then you should not be toying with manual destructors.
    That is correct. Placement new is not a common operation in normal programs, so I'd say that it's fair to say "Never call the destructor", without adding the exception case.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    dwks, I'm not familiar with templates, but I wasn't familiar with linked lists until this project, nor did I understand the importance of pointers. This is the largest C++ app I've written, and I haven't actually written in a few year prior to the last few weeks. So, I'm learning everything as I go.

    Would you suggest templates then ? I've looked into inheritance, polymorphism and the like, and understand the concepts, so could probably go about writing a class to manage my lists. It probably wouldn't be much good, but it would at least be less messy :P

  7. #22
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    This thread is suffering from too many programmers spoiling the top-down reading order. Some of us should stop posting, or go out and design a bulletin board that takes into account when users started typing their posts. Or automatically refreshes the page (in the background, without disturbing your typing) every ten or thirty seconds. Or something.

    [edit] See what I mean? Okay.
    dwks, I'm not familiar with templates, but I wasn't familiar with linked lists until this project, nor did I understand the importance of pointers. This is the largest C++ app I've written, and I haven't actually written in a few year prior to the last few weeks. So, I'm learning everything as I go.

    Would you suggest templates then ? I've looked into inheritance, polymorphism and the like, and understand the concepts, so could probably go about writing a class to manage my lists. It probably wouldn't be much good, but it would at least be less messy :P
    Maybe you should just make use of what you already know -- templates would suit the problem better, but hey.

    Just call the inherited classes AngelList and MortalList or something like that, not just "Angel". It's a collection of Angels. [/edit]
    Last edited by dwks; 09-05-2008 at 02:47 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Korhedron View Post
    dwks, I'm not familiar with templates, but I wasn't familiar with linked lists until this project, nor did I understand the importance of pointers. This is the largest C++ app I've written, and I haven't actually written in a few year prior to the last few weeks. So, I'm learning everything as I go.

    Would you suggest templates then ? I've looked into inheritance, polymorphism and the like, and understand the concepts, so could probably go about writing a class to manage my lists. It probably wouldn't be much good, but it would at least be less messy :P
    I personally would avoid templates for now - yes, you can get very powerful programs in a few lines with templates, but it's also a more advanced concept.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #24
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I do agree. I need to get more in the habit of quoting the person I am replying to often because I notice that I frequently slip in between a post I am replying to and another person who is replying to the same thing, but then it looks as if they are questioning me. Or it looks like duplicate information. Or when I end up having someone slip in between me and my intended person I am responding to, I look like I am arguing.

    Korhedron, the first person to reply to you mentioned a template that does the linked list functions for you. I highly recommend reading up on pointers a bit before tackling templates, however. If you are having difficulty with pointers, you should understand them before going on to things that are too advanced since they are a staple of the language.

  10. #25
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Just to return to this lists class :

    All of my problems are arising from me accessing the previous or next pointer on a node that doesn't have a previous or next node. So, technically, all I need is to :

    1. Make sure all pointers are NULL when declared
    2. Check for NULL before doing any operation that uses these pointers.

    I don't see how writing a list object will help. It'll remove about four lines of code in total ( the stuff duplicated in Angel and Mortal classes ), but I'm struggling to see that I can include code to manage my pointers inside the object itself. The objects are what makes the list, and the operations on pointers are done "between objects", not from inside an object... In this way, I'm very confused.

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are TWO advantages of doing a list class:
    1. You separate the list handling from your other code - so you can work on making the list class work correctly separately from making other code work correctly [you have a list component that is "known good", so you don't need to worry about it].

    2. It teaches you how to use inheritance to achieve more complex stuff. This is of course a completely non-technical reason - but an equally good one when you are learning. [And if the purpose here wasn't to learn, then we'd be back at recommendin std::list].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #27
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Mats, thanks again. Though, I'm still struggling to imagine placing the list management code INSIDE the objects themselves. I can put in my next and prev pointers, but the code from going from one node to the other, and the code for deleting a node - that can't fit inside the actual object, so that code won't be a "known good", it'll just be exactly the same as I currently have.

    Or am I entirely deluded ? :P

  13. #28
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by Korhedron View Post
    Just to return to this lists class :

    All of my problems are arising from me accessing the previous or next pointer on a node that doesn't have a previous or next node. So, technically, all I need is to :

    1. Make sure all pointers are NULL when declared
    2. Check for NULL before doing any operation that uses these pointers.
    Yeah, pretty much. Plus never calling destructors, but that's probably not something you'll do once you know you shouldn't.

    I don't see how writing a list object will help. It'll remove about four lines of code in total ( the stuff duplicated in Angel and Mortal classes ), but I'm struggling to see that I can include code to manage my pointers inside the object itself. The objects are what makes the list, and the operations on pointers are done "between objects", not from inside an object... In this way, I'm very confused.
    Some operations, like deleting a list item, you simply can't really do from "inside" one of the list objects. You do sort of need a controller class or function or whatever.

    But I think you're right, a list object wouldn't help much in this case -- because it's a linked list. You can't really have a AngelList::delete() method for linked lists.

    Deleting objects is the hard part, I think. You have to keep in mind the fact that the root object could change. (So maybe returning a new root from the delete() function or whatever would be a good idea.) The list could become completely empty.

    One mistake I consistently made when I was creating my first linked lists, something that you're doing too, was assume that the linked list always had at least one object. It just confuses everything. Write your code carefully, so that the root object can be NULL.

    [edit] Beaten again -- by two posts! What is it with this thread?

    but the code from going from one node to the other
    That's easy -- just call traverse() or whatever in the next node.
    and the code for deleting a node - that can't fit inside the actual object
    Yeah, that's a bit tougher. It would probably be easier to have an AngelList to do that . . . .
    [/edit]
    Last edited by dwks; 09-05-2008 at 03:31 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #29
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Something like this (actually, the Angel and Mortal are nodes, not lists - because the list is a separate thing.

    Code:
    class Node
    {
        public:
           Node *next;
           Node *prev;
           Node() : next(0), prev(0) {};
    };
    
    class List
    {
        private: 
           Node *first;
           Node *last;
        public:
           List() : first(0), last(0);
           void Insert(Node &);
           void Remove(Node &);
    };
    
    class Angel : public Node
    {
       ... your angel definition goes here ...       
    };
    
    class Mortal : public Node
    {
       ... 
    }
    
    List AngelList;
    List MortalList;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #30
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Also, doing this gives you a natural progression to learning templates, because the problems you'll encounter working with this code are the main motivation behind templates.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. access violation segmentation fault raised in your program
    By dinamit875 in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2009, 11:44 AM
  2. Getting Access Violation (Segmentation Fault)
    By Brownie in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2008, 11:43 AM
  3. Replies: 3
    Last Post: 07-17-2008, 08:45 AM
  4. access violation (segmentation fault)
    By MarlonDean in forum C++ Programming
    Replies: 7
    Last Post: 05-16-2008, 05:02 AM
  5. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM