Thread: String problems - strings seem to join?

  1. #1
    Registered User
    Join Date
    Jan 2007

    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:

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

    /* 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;
    	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?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    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!

    "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

  3. #3
    Registered User
    Join Date
    Jan 2007
    Ah, excelent! Such a simple problem, sometimes it takes another pair of eyes to spot these sort of things. Thankyou so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  5. problems with string manipulation
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 04:45 PM