The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Results in Debug and Release mode are different
Discuss Results in Debug and Release mode are different in the C Programming forum on Dev Shed. Results in Debug and Release mode are different C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 24th, 2006, 06:15 AM
|
|
Contributing User
|
|
Join Date: Jan 2006
Location: somewhere
Posts: 33
Time spent in forums: 2 Days 5 h 48 m 11 sec
Reputation Power: 8
|
|
|
Results in Debug and Release mode are different
Hi!,
I've created three functions that would work together in order to generate a date is string YYYY-MM-DD format.
The code works fine in Debug mode.The problem is when I try to run this code in the Release mode.
It (the program) gives me two different results when I run it in Debug and Release mode.
And no compile error and warning are present when I compile it in those two modes respectively.
here is the code
Code:
#include <string.h>
#include <stdio.h>
#include <time.h>
#define HOUR 261
#define MINUTE 262
#define DAY 263
#define MONTH 264
#define YEAR 265
void padUpString(char *str, char* filler, int len, int fillAtTheEnd)
{
int i;
char newstr[50]="";
int strLen = strlen(str);
int diff = len - strLen;
if (strLen < len)
{
for (i=0; i< len ; i++)
{
if (fillAtTheEnd)
{
if( i >= strLen) *(str+strLen++)=*filler;
}else
{
if( i < diff) strcat(newstr,filler);
}
}
if (!fillAtTheEnd)
{
strcat(newstr,str);
strcpy(str,newstr);
}
}
}
int getCurrentTimeValue(int timeElement)
{
struct tm *tm_today;
time_t t;
t = time(NULL);
tm_today = localtime(&t);
//printf("timeElement is %d\n",timeElement);
if (timeElement == HOUR)
{
return tm_today->tm_hour;
}
if (timeElement == MINUTE)
{
return tm_today->tm_min;
}
if (timeElement == DAY)
{
//printf("DAY is %d\n",timeElement); //for debug purposes
return tm_today->tm_mday;
}
if (timeElement == MONTH)
{
//printf("MONTH is %d\n",timeElement);//for debug purposes
return tm_today->tm_mon + 1;
}
if (timeElement == YEAR)
{
//printf("YEAR is %d\n",timeElement);//for debug purposes
return tm_today->tm_year + 1900;
}
return -1;
}
void getProcessDate(char *procDate)
{
//YYYY-MM-DD
char currentTime[11];
char year[4];
char day[2];
char month[2];
sprintf(year,"%d",getCurrentTimeValue(YEAR));
sprintf(month,"%d",getCurrentTimeValue(MONTH));
sprintf(day,"%d",getCurrentTimeValue(DAY));
printf("\nCheck date\n"); // for testing purpose only
printf("year [%s]\n",year);
printf("month [%s]\n",month);
printf("day [%s]\n",day);
padUpString(month,"0",2,0);
padUpString(day,"0",2,0);
printf("\nAfter padding\n");// for testing purpose only
printf("month [%s]\n",month);
printf("day [%s]\n",day);
sprintf(currentTime,"%s-%s-%s",year,month,day);
currentTime[10] = '\0';
memcpy(procDate,currentTime,sizeof(currentTime));
}
int main()
{
char date[30];
getProcessDate(date);
printf("%s\n",date);
return 0;
}
the output
Debug Mode
Quote:
Check date
year [2006]
month [5]
day [24]
After padding
month [05]
day [24]
2006-05-24 |
Release Mode
Quote:
Check date
year [2006]
month []
day [24]
After padding
month [00]
day [2400]
-00-2400 |
as you can see the "month" in Release mode is blank,which I suspect is the one causing the problem.
Does anyone know why the results are different?
and how would I fix such problem?
I'm using MVC6 in Win2K
Regards,
dlare9
Last edited by dlare9 : May 24th, 2006 at 06:19 AM.
Reason: forgot to include tool being used
|

May 24th, 2006, 06:20 AM
|
|
|
As an aside - see if your Windows compiler supports strftime() which will do what you want in one line of code. strftime() behavior is defined for ANSI C, but with Windows there is no guarantee it's there.
Code:
#include <time.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
char tmp[256]={0x0};
time_t lt=time(NULL);
struct tm *ptr=localtime(<);
char *yyyymmdd_fmt="%Y-%m-%d";
strftime(tmp,sizeof(tmp),yyyymmdd_fmt,ptr);
printf("%s\n", tmp);
return 0;
}
Last edited by jim mcnamara : May 24th, 2006 at 06:34 AM.
|

May 24th, 2006, 06:23 AM
|
|
|
|
Your array sizes in the getCurrentTimeValue function are wrong. Your array size MUST include space for the null terminator.
|

May 24th, 2006, 08:05 AM
|
|
Contributing User
|
|
Join Date: Jan 2006
Location: somewhere
Posts: 33
Time spent in forums: 2 Days 5 h 48 m 11 sec
Reputation Power: 8
|
|
Quote: | Originally Posted by ptr2void Your array sizes in the getCurrentTimeValue function are wrong. Your array size MUST include space for the null terminator. |
I think your referring to getProcessDate function.
If that is the case, your'e right!
Code:
// array declaration in getProcessDate.
char currentTime[11];
char year[5];
char day[3];
char month[3];
changing the array declaration solve the problem.
But I'm curious though why in the Debug mode this type of error is not present?
P.S.
as jim mcnamara suggested. I'm thinking of using strftime().The function does exist in Windows and if I had known about this function I would have used this a long time ago.
thanks for the quick reply
Regards,
dlare9
|

May 24th, 2006, 11:30 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
|
Apparently, when you compile for debugging, the compiler puts some extra space at the ends of some of your arrays which is not present in the release version. Your last sprintf() is to day, which writes a 2-character string. This means (as you know) that day[2] will get a null character, but since the array isn't long enough, and it seems the compiler decided to pack both arrays into a single 4-byte area, this clobbers month[0]. Since month[0] is now a null character, the program thinks month holds a null string.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|