Thread: strange problem, not sure what is wrong

  1. #1

    strange problem, not sure what is wrong

    This is code that is giving me trouble:
    int main(void) {
    const	char Off[] = "Off";
    	Sensor *X = new Sensor("On", "near the front door");
    	MainFrame *W = new MainFrame("Off");
    	cout << "Sensor X is located " << X->GetLocation() << endl;
    	cout << "Sensor X is currently " << X->GetSensorState() << endl;
    The output from this code should be:
    Sensor X is located near the front door (this works)
    Sensor X is currently On

    Instead I get:
    Sensor X is located near the front door
    Sensor X is currently Onnear the front door

    This is the function GetSensorState:
    inline	char *GetSensorState()  { return State; }
    All it does is return State which is a protected character array. State is defined as
    char State[2] (only meant to hold 'On' or 'Off' so i figured 2 is enough)

    The function GetLocation is:
     inline	char *GetLocation()  { return itsLocation; }
    All GetLocation does is return itsLocation which is a protected character array that describes the location of the sensor
    itsLocation is defined as:
    char itsLocation[50];

    I am honestly not sure what is wrong here, this is quite strange.
    Last edited by Shadow12345; 07-21-2002 at 11:21 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > (only meant to hold 'On' or 'Off' so i figured 2 is enough)

    Well "Off" requires 4 counting the \0

  3. #3
    what do you mean counting the \0? and how could that cause my problem?

  4. #4
    Registered User
    Join Date
    Jan 2002
    Character arrays are terminated by '\0'. "off" is three characters, plus the terminating null character, so you need [4] in your char array.
    Truth is a malleable commodity - Dick Cheney

  5. #5
    Registered User
    Join Date
    Apr 2002
    char State[2] holds two char's, not two C-strings, i.e. "on"/"off". There's no provision for the terminating '\0' character in either case.

    The compiler reads data from an array until the '\0' char is found and terminates the read. Even a full array must end with '\0'.

    Note that the compiler will both "overwrite" and "overread" your array boundaries. Not a good thing since you can corrupt memory contiguous to your array if the former occurs and read absolute garbage in the case of the latter.

    Initializing 'State' to [4] allocates space for the '\0' char provided nothing longer than "off" is written to the array.

    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  6. #6
    re-read my problem please
    it only doesn't work in one part of my program, but works in another part.
    I don't know if the character array is causing my problem (I don't think it is)

    plus i am going to try rewriting the same program using strings
    Last edited by Shadow12345; 07-22-2002 at 08:34 AM.

  7. #7
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    I agree I think the problem is your getssensorstate string, It can only hold 1 letter and a null terminating character that is how it knows the stribng has come to an end, if you just up this to 4 then it will allow you to hold 'o' 'n' '\0' and 'o' 'f' 'f' '\0', if the function is trying to return a string that is too short then I think it could cause trouble.
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

  8. #8
    yes changing it to 4 works

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange problem with GETLINE
    By wco5002 in forum C++ Programming
    Replies: 13
    Last Post: 07-07-2008, 09:57 AM
  2. Strange problem
    By G4B3 in forum C Programming
    Replies: 6
    Last Post: 05-14-2008, 02:07 PM
  3. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  4. Extremely strange "delete" problem
    By dr_jaymahdi in forum C++ Programming
    Replies: 4
    Last Post: 10-21-2007, 09:06 PM
  5. strange dll relatred problem
    By cloudy in forum Windows Programming
    Replies: 6
    Last Post: 07-03-2005, 05:30 PM