开发者

void pointer conditional raising segfault

I'm using a double-linked list and getting some odd performance regarding edge cases when we're dealing with previous or next pointers that are null. GDB returns the following error:

Program received signal SIGSEGV, Segmentation fault.
0x0804a9c1 in DLinkDelete (delete=0xd8c9d33c) at test.c:213
213             if (prevdl && prevdl->next) 
(gdb) p开发者_JAVA百科 prevdl
$39 = (DoubleLink *) 0xdadadada
(gdb) p prevdl->next
$40 = (void *) 0x0

DoubleLink is a struct with the following format:

typedef struct
{
    void *next;
    void *prev;
} DoubleLink;

Why would a segmentation fault be raised here?


Presumably prevdl points to invalid memory ? Come on, does 0xdadadada really look like a valid address to you ? "It's too idiotic to be schizophrenic".


the 0xdadadada address seems very weird although it is not NULL. Maybe you're trying to read a memory that is not allowed to you.


The value:

0xdadadada

looks suspiciously unlike a real pointer value. Just because a pointer is not NULL does not mean it is valid. And if you use an invalid pointer, you will get aseg fault, or whatever.


0xdadadada looks a bit suspicious, perhaps prevdl hasn't been properly initialised? Or maybe it's been stomped on by some other code.

Maybe you should try running the test under valgrind and see if it reports anything?


0xdadadada doesn't sound like a very likely value for a valid pointer... most likely a debugging initialiser.


Specifically, it means you're accessing data that hasn't been initialized at all. Question -- it this the first attempt to use this structure?

In any case, check your initialization code; when you create the node, I'd bet the very first node, which has no previous, you aren't setting its value, but in the code above you are trying to access it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜