It's been a long (long...) time since I've done any C programming and I'm stuck on what should be a really simple issue. I have a simple function that's calling another function that is allocating some memory on the heap, populating it with a structure and returning a pointer back. Everything looks great, except when the pointer is returned to the calling function, it's address is corrupted.
Here's the relevant code:
Called function:
struct server_type *init_gwserver(Cfg* cfg)
{
struct server_type *res = NULL;
// Lots of irrelevant code removed here - doesn't reference res.
res = gw_malloc(sizeof(struct server_type));
gw_assert(res != NULL);
res->server_start = gwserver_start; // Pointer to a function
res->server_stop = gwserver_stop; // Pointer to another function
return res;
}
Calling function:
struct server_type *do_init_server(Cfg *cfg)
{
struct server_type *res = NULL;
res = (struct server_type *)init_gwserver(cfg);
if (res) {
return res;
}
}
OK, here's the weirdness. When "res" is returned to the calling function, the address the pointer is pointing to changes, and becomes an invalid memory reference. Here's a quick run through with GDB. Comments (//...) are mine...
Breakpoint 1, init_gwserver (cfg=0x100214e30) at init_gwserver.c:339
339 res->server_stop = gwserver_stop;
(gdb) print /x res
$18 = 0x100215b10
// Pointer above looks fine and seems to be a valid address
(gdb) p *res
$14 = {
type = 0x100215460,
sql_enter = 0x100003820 <gwserver_start>,
sql_leave = 0x100005ae0 <gwserver_stop>,
}
// Contents of the pointer are looking great
(gdb) next
340 return res;
(gdb) print /x res
$19 = 0x100215b10
// Pointer is unchanged - should return this 开发者_C百科value just fine.
(gdb) next
do_init_server (cfg=0x100214e30) at init_server.c:52
52 if (res) {
(gdb) print /x res
$20 = 0x215b10
// WOW - what the hell happened to the address pointed to by res? It lost the leading 100.
(gdb) p *res
Cannot access memory at address 0x215b10
// Yep, it's definitely now an invalid pointer. Crash time...`
This is probably something really, really simple, but I can't put my finger on it. Anyone got suggestions?
Thanks! Toby.
This cast looks suspicious: (struct server_type *)init_gwserver(cfg)
.
If the cast is necessary to make the code compile then it indicates that there is not a correct prototype visible for init_gwserver
at the call site.
If there is no prototype visible, the compiler will make the assumption that the function returns an int
. When the return value is retrieved as something that it is not, depending on the implementation, its value may be corrupted and when converted back to a pointer information may have been irretrievably lost.
The cast should not be necessary and if removing it causes a compile error it indicates that you need to add (or #include
) a correct prototype for init_gwserver
before the point where you attempt to call it.
精彩评论