C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 24th, 2006, 06:15 AM
dlare9 dlare9 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: somewhere
Posts: 33 dlare9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old May 24th, 2006, 06:20 AM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,345 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 38 m 38 sec
Reputation Power: 54
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(&lt);
    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.

Reply With Quote
  #3  
Old May 24th, 2006, 06:23 AM
ptr2void ptr2void is offline
I haz teh codez!
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Dec 2003
Posts: 2,477 ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 7 h 35 m 10 sec
Reputation Power: 2194
Your array sizes in the getCurrentTimeValue function are wrong. Your array size MUST include space for the null terminator.

Reply With Quote
  #4  
Old May 24th, 2006, 08:05 AM
dlare9 dlare9 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: somewhere
Posts: 33 dlare9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #5  
Old May 24th, 2006, 11:30 AM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
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.
Comments on this post
salem agrees!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Results in Debug and Release mode are different

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap