Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Any Idea why? ....

Any Idea why? ....

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structuresdebuggingquestion
13 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JohnnyG
    wrote on last edited by
    #1

    I've created an array of unsigned chars as a parity table. During program initialization, I originally memset the array to all zeros. In the same init sequence, I set elements of the array to one to signify that the parity bit should be set for that particular value. // in header file #define RADAR_PARITYTBLSZ 4096 unsigned char RadarParityTable[RADAR_PARITYTBLSZ]; // in .C file // Initialize parity table for 12 bit radar msgs and 1 bit of parity memset(&RadarParityTable, 0, sizeof(RadarParityTable)); for(i = 0; i < RADAR_PARITYTBLSZ; i++) if(ComputeParity(i) == 0) // this returns 0 if value of i is even RadarParityTable[i] = 1; The problem is that the last element of the array, in this case [4095], (the array is 4096 bytes in size, is mysteriously set to 0x20 instead of 1. I'm using odd parity so the parity for a value of 0xfff or 4095 -- 12 binary bits set means add one to it to make it odd. I set a breakpoint right after the code above in the init sequence and Quickwatch says ALL of the values are zero. Later when I compare parity to the parity table in another module and use quickwatch, the value for that element is 0x20. The only way I was able to fix this was to increase the size of the array by 1 to 4097, initialize the first 4096 elements the same as before, and then all of the values in the array were correct even the last one. I even tried adding this line of code below in the init sequence but it didn't work. RadarParityTable[RADAR_PARITYTBLSZ-1] = 1; Any ideas why it was set to hex 0x20.

    J R R J 4 Replies Last reply
    0
    • J JohnnyG

      I've created an array of unsigned chars as a parity table. During program initialization, I originally memset the array to all zeros. In the same init sequence, I set elements of the array to one to signify that the parity bit should be set for that particular value. // in header file #define RADAR_PARITYTBLSZ 4096 unsigned char RadarParityTable[RADAR_PARITYTBLSZ]; // in .C file // Initialize parity table for 12 bit radar msgs and 1 bit of parity memset(&RadarParityTable, 0, sizeof(RadarParityTable)); for(i = 0; i < RADAR_PARITYTBLSZ; i++) if(ComputeParity(i) == 0) // this returns 0 if value of i is even RadarParityTable[i] = 1; The problem is that the last element of the array, in this case [4095], (the array is 4096 bytes in size, is mysteriously set to 0x20 instead of 1. I'm using odd parity so the parity for a value of 0xfff or 4095 -- 12 binary bits set means add one to it to make it odd. I set a breakpoint right after the code above in the init sequence and Quickwatch says ALL of the values are zero. Later when I compare parity to the parity table in another module and use quickwatch, the value for that element is 0x20. The only way I was able to fix this was to increase the size of the array by 1 to 4097, initialize the first 4096 elements the same as before, and then all of the values in the array were correct even the last one. I even tried adding this line of code below in the init sequence but it didn't work. RadarParityTable[RADAR_PARITYTBLSZ-1] = 1; Any ideas why it was set to hex 0x20.

      J Offline
      J Offline
      jbarton
      wrote on last edited by
      #2

      Nothing in the code that you show should have this result. Why not use a data breakpoint in the debugger to see when the element is being overwritten? Data breakpoints are set with Edit / BreakPoints and then selecting the Data tab. Enter as the expression: RadarParityTable[4095] Best regards, John

      J 1 Reply Last reply
      0
      • J JohnnyG

        I've created an array of unsigned chars as a parity table. During program initialization, I originally memset the array to all zeros. In the same init sequence, I set elements of the array to one to signify that the parity bit should be set for that particular value. // in header file #define RADAR_PARITYTBLSZ 4096 unsigned char RadarParityTable[RADAR_PARITYTBLSZ]; // in .C file // Initialize parity table for 12 bit radar msgs and 1 bit of parity memset(&RadarParityTable, 0, sizeof(RadarParityTable)); for(i = 0; i < RADAR_PARITYTBLSZ; i++) if(ComputeParity(i) == 0) // this returns 0 if value of i is even RadarParityTable[i] = 1; The problem is that the last element of the array, in this case [4095], (the array is 4096 bytes in size, is mysteriously set to 0x20 instead of 1. I'm using odd parity so the parity for a value of 0xfff or 4095 -- 12 binary bits set means add one to it to make it odd. I set a breakpoint right after the code above in the init sequence and Quickwatch says ALL of the values are zero. Later when I compare parity to the parity table in another module and use quickwatch, the value for that element is 0x20. The only way I was able to fix this was to increase the size of the array by 1 to 4097, initialize the first 4096 elements the same as before, and then all of the values in the array were correct even the last one. I even tried adding this line of code below in the init sequence but it didn't work. RadarParityTable[RADAR_PARITYTBLSZ-1] = 1; Any ideas why it was set to hex 0x20.

        R Offline
        R Offline
        Roger Allen
        wrote on last edited by
        #3

        You probably have another array on the atck which is writing beyond its bounds. Take a look at the other vars declared next to your array. Roger Allen Sonork 100.10016 If I had a quote, it would be a very good one.

        J 1 Reply Last reply
        0
        • J jbarton

          Nothing in the code that you show should have this result. Why not use a data breakpoint in the debugger to see when the element is being overwritten? Data breakpoints are set with Edit / BreakPoints and then selecting the Data tab. Enter as the expression: RadarParityTable[4095] Best regards, John

          J Offline
          J Offline
          JohnnyG
          wrote on last edited by
          #4

          I'll try that tomorrow. I'm at home now.:-O

          1 Reply Last reply
          0
          • J JohnnyG

            I've created an array of unsigned chars as a parity table. During program initialization, I originally memset the array to all zeros. In the same init sequence, I set elements of the array to one to signify that the parity bit should be set for that particular value. // in header file #define RADAR_PARITYTBLSZ 4096 unsigned char RadarParityTable[RADAR_PARITYTBLSZ]; // in .C file // Initialize parity table for 12 bit radar msgs and 1 bit of parity memset(&RadarParityTable, 0, sizeof(RadarParityTable)); for(i = 0; i < RADAR_PARITYTBLSZ; i++) if(ComputeParity(i) == 0) // this returns 0 if value of i is even RadarParityTable[i] = 1; The problem is that the last element of the array, in this case [4095], (the array is 4096 bytes in size, is mysteriously set to 0x20 instead of 1. I'm using odd parity so the parity for a value of 0xfff or 4095 -- 12 binary bits set means add one to it to make it odd. I set a breakpoint right after the code above in the init sequence and Quickwatch says ALL of the values are zero. Later when I compare parity to the parity table in another module and use quickwatch, the value for that element is 0x20. The only way I was able to fix this was to increase the size of the array by 1 to 4097, initialize the first 4096 elements the same as before, and then all of the values in the array were correct even the last one. I even tried adding this line of code below in the init sequence but it didn't work. RadarParityTable[RADAR_PARITYTBLSZ-1] = 1; Any ideas why it was set to hex 0x20.

            R Offline
            R Offline
            Ramu Pulipati
            wrote on last edited by
            #5

            JohnnyG wrote: memset(&RadarParityTable, 0, sizeof(RadarParityTable)); to memset(RadarParityTable, 0, sizeof(RadarParityTable)); Did you corrected this ??? Hth....;) Ramu

            J 2 Replies Last reply
            0
            • R Roger Allen

              You probably have another array on the atck which is writing beyond its bounds. Take a look at the other vars declared next to your array. Roger Allen Sonork 100.10016 If I had a quote, it would be a very good one.

              J Offline
              J Offline
              JohnnyG
              wrote on last edited by
              #6

              You were right and John Barton's suggestion helped me identify the problem though I still have to find out why. Apparently, that element in the array gets overwritten when I'm using strtok() on some other data. I'm not sure if it's because I'm using strtok in a thread or if I'm doing some bad pointer arithmetic. I use strtok on a buffer and then later do... *(p - 1) = ' '; because I need to place spaces back into the buffer so that I can look for two spaces. It could be that statement. Anyways, thanks for the help, guys.

              B 1 Reply Last reply
              0
              • J JohnnyG

                You were right and John Barton's suggestion helped me identify the problem though I still have to find out why. Apparently, that element in the array gets overwritten when I'm using strtok() on some other data. I'm not sure if it's because I'm using strtok in a thread or if I'm doing some bad pointer arithmetic. I use strtok on a buffer and then later do... *(p - 1) = ' '; because I need to place spaces back into the buffer so that I can look for two spaces. It could be that statement. Anyways, thanks for the help, guys.

                B Offline
                B Offline
                Bill Wilson
                wrote on last edited by
                #7

                *(p - 1) = ' '; will put a space character in the byte imedieatly preceeding the location pointed to by p. If p is a pointer to the location after your buffer, the last character of your buffer will become a 0x20 (space character).

                J 1 Reply Last reply
                0
                • R Ramu Pulipati

                  JohnnyG wrote: memset(&RadarParityTable, 0, sizeof(RadarParityTable)); to memset(RadarParityTable, 0, sizeof(RadarParityTable)); Did you corrected this ??? Hth....;) Ramu

                  J Offline
                  J Offline
                  JohnnyG
                  wrote on last edited by
                  #8

                  Sorry, I didn't answer. I did not get to it but I believe that when I don't put the address of operator (&) it says something about parameter 1 not being right. That's why I used &RadarParityTable. I'll check again if I have time tomorrow. I know that parameter 1 should be a void pointer. Thanks

                  R 1 Reply Last reply
                  0
                  • B Bill Wilson

                    *(p - 1) = ' '; will put a space character in the byte imedieatly preceeding the location pointed to by p. If p is a pointer to the location after your buffer, the last character of your buffer will become a 0x20 (space character).

                    J Offline
                    J Offline
                    JohnnyG
                    wrote on last edited by
                    #9

                    Yes. That space however is going to the end of the wrong array, RadarParityTable. I have to parse another buffer, a file header, and treat the data differently when it encounters two spaces. Because strtok puts a 0x00 or '\0' at the end of each substring where the token exists, the data will be in EBCDIC when written to disk, I had to replace the spaces that strtok put into it. So, I did as you say put a space preceding the spot where strtok found the next substring of data. I must be corrupting that memory somehow, though boundschecker active mode is not catching it. Its funny though that changing the size of the RadarParityTable from 4096 to 4097 fixed the problem.

                    1 Reply Last reply
                    0
                    • R Ramu Pulipati

                      JohnnyG wrote: memset(&RadarParityTable, 0, sizeof(RadarParityTable)); to memset(RadarParityTable, 0, sizeof(RadarParityTable)); Did you corrected this ??? Hth....;) Ramu

                      J Offline
                      J Offline
                      JohnnyG
                      wrote on last edited by
                      #10

                      BTW, The way I'm doing it now doesn't give me any warnings and when I breakpoint after the memset, the entire array is equal to zeroes.

                      1 Reply Last reply
                      0
                      • J JohnnyG

                        Sorry, I didn't answer. I did not get to it but I believe that when I don't put the address of operator (&) it says something about parameter 1 not being right. That's why I used &RadarParityTable. I'll check again if I have time tomorrow. I know that parameter 1 should be a void pointer. Thanks

                        R Offline
                        R Offline
                        Ramu Pulipati
                        wrote on last edited by
                        #11

                        What error message does it exactly give ??? Using '&' is not correct. Thanks, Ramu

                        J 1 Reply Last reply
                        0
                        • R Ramu Pulipati

                          What error message does it exactly give ??? Using '&' is not correct. Thanks, Ramu

                          J Offline
                          J Offline
                          JohnnyG
                          wrote on last edited by
                          #12

                          You were right Ramu. I had gotten this msg earlier: 'memset' : actual parameter is not a pointer : parameter 1 but that was for a structure. So, when I coded the statement for an array I used the same technique. Now, I see the errors of my way.

                          1 Reply Last reply
                          0
                          • J JohnnyG

                            I've created an array of unsigned chars as a parity table. During program initialization, I originally memset the array to all zeros. In the same init sequence, I set elements of the array to one to signify that the parity bit should be set for that particular value. // in header file #define RADAR_PARITYTBLSZ 4096 unsigned char RadarParityTable[RADAR_PARITYTBLSZ]; // in .C file // Initialize parity table for 12 bit radar msgs and 1 bit of parity memset(&RadarParityTable, 0, sizeof(RadarParityTable)); for(i = 0; i < RADAR_PARITYTBLSZ; i++) if(ComputeParity(i) == 0) // this returns 0 if value of i is even RadarParityTable[i] = 1; The problem is that the last element of the array, in this case [4095], (the array is 4096 bytes in size, is mysteriously set to 0x20 instead of 1. I'm using odd parity so the parity for a value of 0xfff or 4095 -- 12 binary bits set means add one to it to make it odd. I set a breakpoint right after the code above in the init sequence and Quickwatch says ALL of the values are zero. Later when I compare parity to the parity table in another module and use quickwatch, the value for that element is 0x20. The only way I was able to fix this was to increase the size of the array by 1 to 4097, initialize the first 4096 elements the same as before, and then all of the values in the array were correct even the last one. I even tried adding this line of code below in the init sequence but it didn't work. RadarParityTable[RADAR_PARITYTBLSZ-1] = 1; Any ideas why it was set to hex 0x20.

                            J Offline
                            J Offline
                            JohnnyG
                            wrote on last edited by
                            #13

                            I fixed it. Thanks, everybody. It turns out that on the very first call to strtok and all subsequent calls, I was placing the '\0' that strtok appends to each substring with a space. However, on the first call to strtok, there is no space (my token) before it, and thus I was placing a space into a memory address where my other array's end element existed.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups