: Hi everybody,
: I'm getting a segmentation fault on an application I'm developing and I
: have no clue what is causing it. I'm a code pig newbie, so any
: help/suggestions are most welcome. Here's an excerpt of the problem:
Whenever a C program chokes on a return statement the most likely
reason is that the function you're returning from (or a function called
by it) has trashed the stack. The stack contains the address to return
to in the function that called this one, and if it gets damagedthe
return statement will return to the bogus address, somewhere "off in
the weeds", a place where there is unlikly to be valid executable code.
You don't sow us the source for Do_get_string(), but it seems likely
that that is where your problem is, or else it may be in a misuse of
the "struct client_t *c" whose we also don't see.
Fred
: Using RedHat Linux with egcs-1.1.2-24 on a 2.2.10 kernel.
: A section of my code calls a function named Do_get_double -
:> int rc;
:>
:> ...
:>
:> /* wait for the player to send back an answer */
:> printf("Starting...\n");
:> rc = Do_get_double(c, x);
:> printf("We're back in Do_coords_dialog with an rc of %d.\n", rc);
:>
:> ...
: Do_get_double calls Do_get_string which is set up to fail with an error
: code of 3 (S_ERROR) -
:>int Do_get_double(struct client_t *c, double *theDouble)
:>{
:> char tmpDouble[SZ_NUMBER];
:> int rc;
:>
:> rc = Do_get_string(c, tmpDouble, SZ_NUMBER);
:> printf("Here I am with a return code of %d.\n", rc);
:>
:> if (rc == S_ERROR) {
:> printf("(3) Do_get_string failed with %d.\n", rc);
:> return S_ERROR;
:> }
:> else if (rc == S_TIMEOUT) {
:> printf("(2) Do_get_string failed with %d.\n", rc);
:> return S_TIMEOUT;
:> }
:> else if (rc == S_CANCEL) {
:> printf("(1) Do_get_string failed with %d.\n", rc);
:> return S_CANCEL;
:> }
:>
:> *theDouble = floor(strtod(tmpDouble, NULL));
:>
:> return S_NORM;
:>}
: In an include file, I have:
:>/* socket return values */
:>#define S_NORM 0 /* socket returned data */
:>#define S_CANCEL 1 /* player canceled selection */
:>#define S_TIMEOUT 2 /* player ran out of time */
:>#define S_ERROR 3 /* socket is closed */
: When I run the code, here's what I get -
: Starting...
: Here I am with a return code of 3.
: (3) Do_get_string failed with 3.
: Segmentation fault (core dumped)
: So, the program seems to be segmenting on the return statement. I have
: removed the constants and the passed by reference double, but the
: problem persists. In addition, the print statements have somtimes
: returned strange values for rc in Do_get_double.
: Judging from previous articles, using g++ may solve this problem, but I
: wanted make sure I wasn't doing something stupid of that there isn't an
: easier workaround.
: Could I get some sage advice from any of the wize and powerful Linux
: gurus? Thanks in advance.
: Brian
--
But God demonstrates his own love for us in this:
While we were still sinners,
Christ died for us.
------------------------------- Romans 5:8 (niv) ------------------------------