C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-18-2007, 10:41 AM   #1
Registered User
 
Join Date: Jan 2007
Posts: 6
String problems - strings seem to join?

I'm having some trouble to do with strings, with a function I've written.

First, at the top of the program, outside of main, I've declared the following global strings, to hold the time, and the date:

Code:
char Date[11];
char Time[9];
The variables are given data during this function:

Code:
/* Load time */
/* Loads the time and date settings into the global Time and Date variables */

int LoadTime()
{
	cout << "Loading time and date...\n";

	time_t TimeObject;
	tm * TimePointer;

	time(&TimeObject);
	TimePointer = gmtime(&TimeObject);
	char Symbols[3] = "-:\0";

	string MyTimeString = asctime(TimePointer);
	
	Date[0] = MyTimeString[8];
	Date[1] = MyTimeString[9];
	Date[2] = Symbols[0];
	Date[3] = MyTimeString[4];
	Date[4] = MyTimeString[5];
	Date[5] = MyTimeString[6];
	Date[6] = Symbols[0];
	Date[7] = MyTimeString[20];
	Date[8] = MyTimeString[21];
	Date[9] = MyTimeString[22];
	Date[10] = MyTimeString[23];
	Date[11] = Symbols[2];

	Time[0] = MyTimeString[11];
	Time[1] = MyTimeString[12];
	Time[2] = MyTimeString[13];
	Time[3] = MyTimeString[14];
	Time[4] = MyTimeString[15];
	Time[5] = MyTimeString[16];
	Time[6] = MyTimeString[17];
	Time[7] = MyTimeString[18];
	Time[8] = MyTimeString[19];
	Time[9] = Symbols[2];
	
	cout << Date << "\n";
	
	return 0;
}
The problem is, when Date is printed onto the console, the contents of time also appear after date too! I thought that the string should have been terminated by the \0 escape code.

Does any kind people have some insight into this strange behaivour?
Metalixia is offline   Reply With Quote
Old 01-18-2007, 10:57 AM   #2
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,492
You didn't actually provide space for the '\0' terminator. Date contains space for 11 characters (indices 0-10), but you assign to the twelth (index 11).

As it happens on your platform, Date and Time are directly adjacent in memory, so Time[0] is the same memory location as (the invalid) Date[11]. In other words, the assignment to Time[0] overwrites the terminator you gave Date.
__________________
All the buzzt!
CornedBee

"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
CornedBee is offline   Reply With Quote
Old 01-18-2007, 11:00 AM   #3
Registered User
 
Join Date: Jan 2007
Posts: 6
Ah, excelent! Such a simple problem, sometimes it takes another pair of eyes to spot these sort of things. Thankyou so much.
Metalixia is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Inheritance Hierarchy] User Input on program with constructors. How ? chandreu C++ Programming 8 04-25-2008 02:45 PM
Inheritance Hierarchy for a Package class twickre C++ Programming 7 12-08-2007 04:13 PM
Custom String class gives problem with another prog. I BLcK I C++ Programming 1 12-18-2006 03:40 AM
RicBot John_ C++ Programming 8 06-13-2006 06:52 PM
problems with string manipulation Unregistered C++ Programming 0 04-24-2002 04:45 PM


All times are GMT -6. The time now is 06:12 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22