当我运行我的程序时,出现了分段错误,所以我决定通过Valgrind检查它。当我这样做时,我收到了Valgrind的以下信息。我在使用这里描述的代码时遇到错误。任何想法发生了什么?这个错误在Valgrind中意味着什么
==21471== Invalid write of size 8
==21471== at 0x4802511: _vgnU_freeres (vg_preloaded.c:64)
==21471== by 0x38A715397F: ???
==21471== by 0x38A6E4D549: printf (in /lib64/libc-2.5.so)
==21471== by 0x401D52: call_func(int) (replication.cpp:752)
==21471== by 0x6137C7: ???
==21471== by 0x40621C: AdvanceFramesMT(void*) (pthreads.cpp:1020)
==21471== by 0x38A7A0673C: start_thread (in /lib64/libpthread-2.5.so)
==21471== by 0x38A6ED44BC: clone (in /lib64/libc-2.5.so)
==21471== Address 0x612ba8 is 14216 bytes inside data symbol "func_stack"
代码
static char func_stack[16384];
static ucontext_t uctx_main[16], uctx_func[16];
void call_func(int n)
{
printf("Message %d!", n);
}
if (getcontext(&uctx_func[tid]) == -1)
handle_error("getcontext");
uctx_func[tid].uc_stack.ss_sp = func_stack;
uctx_func[tid].uc_stack.ss_size = sizeof(func_stack);
uctx_func[tid].uc_link = &uctx_main[tid];
makecontext(&uctx_func[tid], (void(*)())call_func, 1, 2);
if (swapcontext(&uctx_main[tid], &uctx_func[tid]) == -1)
handle_error("swapcontext");
如果我只是复制你显示的代码,它适用于我,无论是在运行时还是在valgrind中。错误必须是因为别的。 –
您是否检查过“tid”始终位于“0”和“15”之间? –
您还没有在'replication.cpp'中的行752(或'pthreads.cpp'中的行1020,尽管这可能没有关系)。目前还不清楚它是否抱怨8字节的字符数组一次写入;但是这是不太可能的。虽然......或者,如果没有错位,那么它可能会被错误地对准,然后被次优化地对齐。 –