Hello
It seems that the same function, with different input values, return the first value.
This program compiled with gcc (GCC) 4.9.1
#include <stdio.h>
char * Time(long ms) { // from start time
static char timeOfDay[9] = "00:00:00";
char s = (ms/1000) % 60;
long h = ms / 3600000;
int m = (ms/1000 - h*3600)/60;
timeOfDay[0] = 48+h/10;
timeOfDay[1] = 48+h%10;
timeOfDay[3] = 48+m/10;
timeOfDay[4] = 48+m%10;
timeOfDay[6] = 48+s/10;
timeOfDay[7] = 48+s%10;
return timeOfDay;
}
int main(int argc, char * argv[]) {
printf("7 %s 0 %s \n",Time(7000),Time(0));
printf(" 0 %s \n",Time(0));
}
outputs
7 00:00:07 0 00:00:07
0 00:00:00
My be it is my error because this happens also when compiled with cint 5.16.19
Best regards
John Rossati
Yes, it is your error: you are printing the content of the same static buffer twice, after your function to populate it has been called for the second time; you have no sequence point to ensure that
printf()will ever see its intermediate state, between the two calls toTime().To clarify, here's what happens when your
printf()call is executed:All function calls, to determine its arguments, are invoked in unspecified order, and their return values, (the pointer to the same static buffer, in the case of your two
Time()calls), are pushed on to the stack. At this point, you have two copies of the same pointer on the stack, and the static buffer content is as left by the second call toTime().Control is then passed to
printf(); it reads its arguments from the stack, and finds the two identical pointers, so it prints the same content twice. Your results suggest that the twoTime()calls were processed in right-to-left order, which is logical, but by no means guaranteed; it doesn't matter: at the time whenprintf()interpolates the buffer content into the output stream, it can have only one value.Last edit: Keith Marshall 2014-11-06
Diff:
BTW, where did you get gcc-4.9.1? We haven't published any packages for this version yet, so it can't be ours; thus, were this not your bug, it certainly would not be ours.
Thanks
I downloaded from http://nuwen.net (Stephan T. Lavavej).
John Rossati
----Messaggio originale----
Da: keithmarshall@users.sf.net
Data: 06/11/2014 12.41
A: "[mingw:bugs] "2242@bugs.mingw.p.re.sf.net
Ogg: [mingw:bugs] #2242 printf error?
BTW, where did you get gcc-4.9.1? We haven't published any packages for this version yet, so it can't be ours; thus, were this not your bug, it certainly would not be ours.
[bugs:#2242] printf error?
Status: closed
Group: OTHER
Labels: printf
Created: Thu Nov 06, 2014 08:51 AM UTC by El Condor
Last Updated: Thu Nov 06, 2014 11:23 AM UTC
Owner: nobody
Hello
It seems that the same function, with different input values, return the first value.
This program compiled with gcc (GCC) 4.9.1
include <stdio.h></stdio.h>
char * Time(long ms) { // from start time
static char timeOfDay[9] = "00:00:00";
char s = (ms/1000) % 60;
long h = ms / 3600000;
int m = (ms/1000 - h*3600)/60;
timeOfDay[0] = 48+h/10;
timeOfDay[1] = 48+h%10;
timeOfDay[3] = 48+m/10;
timeOfDay[4] = 48+m%10;
timeOfDay[6] = 48+s/10;
timeOfDay[7] = 48+s%10;
return timeOfDay;
}
int main(int argc, char * argv[]) {
printf("7 %s 0 %s \n",Time(7000),Time(0));
printf(" 0 %s \n",Time(0));
}
outputs
7 00:00:07 0 00:00:07
0 00:00:00
My be it is my error because this happens also when compiled with cint 5.16.19
Best regards
John Rossati
Sent from sourceforge.net because you indicated interest in https://sourceforge.net/p/mingw/bugs/2242/
To unsubscribe from further messages, please visit https://sourceforge.net/auth/subscriptions/
Related
Issues:
#2242