Thread: Pointer value changes value at an unexpected place

  1. #1
    Registered User
    Join Date
    Aug 2012

    Unhappy Pointer changes value at an unexpected place

    In an attempt to debug this problem, I have added printf statements to see what is happening when the code is running.

    The printf statement containing “#103D” gives a good value for “FirstPtr” but then its value changes.

    I am using Borland C++ IDE to write 16 bit code for a picoFlash single board computer with a 186 compatible processor and 512K of Ram. I am adding code to an existing program that takes 183 pages to print. I have checked the “check stack overflow” option. The compiler and linker don’t give any error messages.

    I don’t know what other information I should inclue because I have no idea what is happening.

    How can I get to the bottom of this problem?

    void DownloadAFile(char *strDirectory,char *strFileName,char *strRxBlock, int intOffset)
                    char *FirstPtr;    //Pointer used to parse the incomming Request
                    char strNrChars[4];          //Number of characters to follow
                    int intNrChars;      //Number of characters following next
                    int intLen;                                                            //Length of string
                    char strLength[4];                                            // 4 char string length
                    char *SPtr;                                             //Pointer to the Line Input String
                    static char strLineInput[1024];    //String used for input
                    char *RspPtr;       //Pointer to strResponseString
                    static char strResponseString[120];
                    char *ShortRspPtr;
                    char strShortResponseString[120];           //Without Rec #
                    char strCommandString[100];
                    char strFullFileName[80];
                    static char strOldFullFileName[80];
                    char strRecordNumber[4];                           //Requested Record Number
                    int intRecordNumber;      //Requested Record Number
                    int intSameFileFlag;
                    static FILE *TxtDLFilePtr;
                    char strTmpStr[80];
                    static int intLastRecordRead;
                    int intLastRecordReadPlus1;
                    int intContinueRecs;                                                       //1 => Continue reading records
                    int intBlockNr;
                    char strRecNr[4];
    //Skip code here
                    //Pick intRecordNumber
                    FirstPtr = &strRxBlock[0];                                                             //to 1
                    printf("#103 ok not term strRxBlock = %s\n", FirstPtr);        //ok
                    printf("#103B s/b=1 Ptr @ strRxBlock[0] = %p\n", FirstPtr);        //ok
                    FirstPtr+= intOffset;             //Beginning of # char in Rec #  to 29
                    printf("#103C s/b=29 Ptr @ Nr char in RecordNr = %p\n", FirstPtr);        //ok
    //            TempPtr = FirstPtr;
                    strncpy (strNrChars,FirstPtr,4);                  //Nr chars in Record #   ok
    //The value of FirstPtr printed with the following line is ok = 5E7E:0AE4
                    printf("#103D after strncpy FirstPtr = %p\n", FirstPtr);        //ok
                    strNrChars[4] = '\0';
    //The value of FirstPtr printed with the following line has changed = 5E7E:0A00
                    printf("#103E after strncpy FirstPtr = %p\n", FirstPtr);        //ok
                    printf("#104 ok strNrChars in Record Nr = %s\n",strNrChars);      //Nr chars in Record # 4 ok
                    printf("#103F after strncpy FirstPtr = %p\n", FirstPtr);        //ok
                    intNrChars = atoi(strNrChars);
                    printf("#103G after strncpy FirstPtr = %p\n", FirstPtr);        //ok
                    printf("#105 ok intNrChars = %d\n",intNrChars);                                               // = 4 ok
                    printf("#103H after strncpy FirstPtr = %p\n", FirstPtr);        //ok
    //More code follows
    Last edited by Jerry900; 08-02-2012 at 01:50 AM. Reason: Grammer

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Simple buffer overrun.

    char strNrChars[4]; //Number of characters to follow
    strNrChars[4] = '\0';

    The valid subscripts are 0, 1, 2 and 3
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2005
    strNrChars[4] = '\0';
    Overwrites the array bounds.
    Valid indexes are 0 .. 3

    EDIT: I should be typing faster

  4. #4
    Registered User
    Join Date
    Aug 2012
    How embarrassing. Thank you for the quick response.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unexpected output
    By juice in forum Windows Programming
    Replies: 6
    Last Post: 03-10-2012, 11:13 AM
  2. Replies: 9
    Last Post: 06-13-2009, 02:31 AM
  3. place int pointer in data block
    By MK27 in forum C Programming
    Replies: 2
    Last Post: 01-18-2009, 12:30 AM
  4. unexpected class?
    By ichijoji in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2003, 06:47 PM
  5. pointer didnt locate the right place~
    By black in forum C++ Programming
    Replies: 9
    Last Post: 08-02-2002, 11:55 AM