From: SourceForge.net <no...@so...> - 2003-12-07 06:08:35
|
Bugs item #855619, was opened at 2003-12-07 02:55 Message generated for change (Comment added) made by jbannon You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=102435&aid=855619&group_id=2435 Category: None Group: None Status: Open Resolution: None Priority: 5 Submitted By: Peter Michaux (petermichaux) Assigned to: Nobody/Anonymous (nobody) Summary: strtol & errno problem? Initial Comment: Here is my test code #include <stdio.h> #include <string.h> #include <errno.h> #include <stdlib.h> int main (void){ char line[]="r45"; int value; char *p; errno=-5; printf("I set errno to: %d\n",errno); printf("line: %s\n",line); value = strtol (line, &p, 10); printf("value: %d\n"); printf("errno: %d\n"); return 0; } Here is the output using Dev-C++ which uses mingw I set errno to: -5 line: r45 value: 0 errno: -5 Here is the ouput using gcc 3.3 on Mac OS X I set errno to: -5 line: r45 value: -1073742712 errno: 0 According to "C A Reference Manual-fourth edition" by Harbinson page 336 neither of these implementations are giving the correct ouput and errno combination. The value should be set to zero and errno should be set to ERANGE which is 34 for mingw and can't be zero for gcc. What's going on? Peter ---------------------------------------------------------------------- Comment By: James Bannon (jbannon) Date: 2003-12-07 06:08 Message: Logged In: YES user_id=734384 Peter, I don't think this is a bug. I think the problem lies in the definition of the variable p. You have char *p; and haven't initialised it to point to anything meaningful (i.e., to the address of a valid memory block) so somewhere a block is being overwritten and you get a garbage result. Technically, the behaviour is undefined so that's why you see the difference in the two outputs. Another possibility in some implementations is a crash with a core dump. You need to allocate memory using malloc or similar. Something like this: char *p = (char *) malloc (20 * sizeof (char)); This will allocate enough space for 19 characters + a terminating null which should be enough to hold the result of the call to strtol (). After using p, you need to free it using a call to free as in free (p); Alternatively, you could declare a fixed size buffer, say like char p[20]; and this would avoid the calls to malloc () and free (). Hope this helps, Best regards. ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=102435&aid=855619&group_id=2435 |