Catching Exceptions with Struct

closed account (Lv0f92yv)
I am trying to create a linked list out of structs. I can do this with classes, though I am now trying with structs.

Is there a way to catch exceptions when accessing a garbage pointer? (pointer that isn't pointing to anything)?

IE when printing the contents of the list to the screen, can I loop until one of the 'next' pointers doesn't point to anything valid (and catch the exception here)?

Here are my list and node structs:

1
2
3
4
5
6
7
8
9
10
struct node_t {
		node_t* next;
		node_t* prev;
		int dat;
	};

struct list_t {
		node_t* head;
		node_t* tail;
	};
I can do this with classes, though I am now trying with structs.
structs and classes are almost semantically identical in C++. The only difference is that the default access for structs is public, while it's private for classes.

I can do this with classes, though I am now trying with structs.
There are few things uglier than that. Is that what you did with classes? I'm guessing not, otherwise you wouldn't be asking about it. So why are you trying something so awful?
closed account (Lv0f92yv)
I have made my own linked list classes and used them successfully, but I have not used structs in a long time, and to practice using them in various ways, I intentionally decided to go with something ugly like this.

IMO one would never use structs for this type of thing when they could use classes, but I don't have the experience to decide that for others.

I suppose the answer to catching exceptions to accomplish my goal is no?
I have not used structs in a long time, and to practice using them in various ways
IMO one would never use structs for this type of thing when they could use classes
Again, I don't see where you're going with this. Structs and classes are the same thing. Your code there and this are equivalent:
1
2
3
4
5
6
7
8
9
10
11
12
class node_t {
public:
		node_t* next;
		node_t* prev;
		int dat;
	};

class list_t {
public:
		node_t* head;
		node_t* tail;
	};


I suppose the answer to catching exceptions to accomplish my goal is no?
No. Not unless you really have to, and you never really have to.
Just do what everybody does and mark the head and tail by zeroing prev and next correspondingly.

Besides, now that I'm a bit more lucid, I'm pretty sure it's not even possible to catch illegal reads/writes.
closed account (Lv0f92yv)
I'm pretty sure it's not even possible to catch illegal reads/writes.


Thanks - that is mainly what I was asking.

As far as zeroing the pointers, this is done easily in classes using a constructor, which (I don't think) there are for structs. Thus I was looking for an alternative method, since it seems a bit brutish to manually set them once I create my list object, and every time after adding/removing an element. Though I suppose this is what I get for using structs for this.

The exception thing was my main question, so thanks for your responses.
In C++, structs and classes are basically the *same*. I think you are meaning more "Do it in C, rather than C++."
closed account (Lv0f92yv)
That does explain what I'm getting at a little clearer. Can I write methods that belong to structs in C++ then, and/or constructors? If so, then this would probably solve my problem.
Topic archived. No new replies allowed.