segmentation fault

hi
i have a class with a member which is a pointer to an object on the heap.
(the object is created from outside the class and is passed to it as a reference in the constructor).

when i try to access it from void functions, everything is ok.

but when i try to access it from value returning functions, i get a segmentation fault.
(the functions are public members of the class)

is it something i'm missing, or is just impossible? (to access from value returning funcs..)

thanks
SEGFAULT is an error passed when you try to access memory outside of your programs memory, like creating a buffer and accessing an element with an index greater than or equal to its size. I don't think we'll be able to tell you what's going on without a little bit of code.
yeah, i figured..


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

class candidate{
private:
	...
        int id;
        myClock *clock;
	...

public:
	candidate (int _id, myClock &_clock ); //constructor
	int getID();
	void waiting();
};

candidate::candidate
		(int _id, myClock &_clock)
{
	id = _id;
	clock = &_clock;
	
}

int candidate::getID(){
	(*clock).some_method();  // <=this generates segfault
        return id;
}


void candidate::waiting(){
	 (*clock).some_method(); // <===== this doesnt
	
}


i dont understand why the myClock object is not visible from the getID() method.


anything?
You didn't get PiMaster's post.
Typically, it has nothing to do with value-returning or void function, that's not the main point, at least.
Trying locating the exact place that generates segfault by inserting cout statements, and pay special attention to:
1. whether an object still exists in memory before you access it
2. have you accessed something outside the boundaries (array, vector, etc)
Topic archived. No new replies allowed.