Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6638713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:28:06+00:00 2026-05-25T23:28:06+00:00

It’s been a long (long…) time since I’ve done any C programming and I’m

  • 0

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 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:28:06+00:00Added an answer on May 25, 2026 at 11:28 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
I have been unable to fix a problem with Java Unicode and encoding. The
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.