Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Storing null value in a char array

Storing null value in a char array

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 2.3k Views
  • 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.
  • A Offline
    A Offline
    anmol2701
    wrote on 2 May 2014, 12:01 last edited by
    #1

    Hi,

    i need to store a value starting with null character in character array but i am getting buffer overflow exception. any method to store null character without terminating the array.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on 2 May 2014, 12:04 last edited by
      #2

      There is no reason why this should not be possible. But of course you can not use strcpy() and friends, which interpret the data as C string, which means the first NULL char indicates the end of the string. Use memcpy() with the appropriate length instead. If you still have issues, please show some code...

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      0
      • A Offline
        A Offline
        anmol2701
        wrote on 2 May 2014, 12:15 last edited by
        #3

        well i am doing somethiong like this:-

        char arr[50];
        arr[0]=0x42;
        arr[1]=0x5b;
        arr[2]=0x00;
        arr[3]=0x5c;
        .....and so on till arr[49]

        but since i am putting arr[1]=0x00 it is giving buffer overflow exception since compiler is taking it as null value.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MuldeR
          wrote on 2 May 2014, 12:32 last edited by
          #4

          [quote author="anmol2701" date="1399032936"]but since i am putting arr[1]=0x00 it is giving buffer overflow exception since compiler is taking it as null value.
          [/quote]

          No, that's perfectly valid! So your problem must be elsewhere. For the compiler the char array is just an array of char's and the value 0x00 has no special meaning. It does have a special meaning only when interpreting the array as a C string, like strcpy(), strlen(), strcmp() and friends do...

          Where exactly in the code do you get the buffer overflow exception?

          __

          For instance, this code works fine:
          @int _tmain(int argc, _TCHAR* argv[])
          {
          char test[50];

          for(int i = 0; i < 50; i++)
          {
          test[i] = i % 10;
          }

          for(int i = 0; i < 50; i++)
          {
          printf("Value[d] is %d\n", i, (int)test[i]);
          }

          return 0;
          }@
          @Value[00] is 0 <--
          Value[01] is 1
          Value[02] is 2
          Value[03] is 3
          Value[04] is 4
          Value[05] is 5
          Value[06] is 6
          Value[07] is 7
          Value[08] is 8
          Value[09] is 9
          Value[10] is 0 <--
          Value[11] is 1
          Value[12] is 2
          Value[13] is 3
          Value[14] is 4
          Value[15] is 5
          Value[16] is 6
          Value[17] is 7
          Value[18] is 8
          Value[19] is 9
          Value[20] is 0 <--
          Value[21] is 1
          Value[22] is 2
          Value[23] is 3
          Value[24] is 4
          Value[25] is 5
          Value[26] is 6
          Value[27] is 7
          Value[28] is 8
          Value[29] is 9
          Value[30] is 0 <--
          Value[31] is 1
          Value[32] is 2
          Value[33] is 3
          Value[34] is 4
          Value[35] is 5
          Value[36] is 6
          Value[37] is 7
          Value[38] is 8
          Value[39] is 9
          Value[40] is 0 <--
          Value[41] is 1
          Value[42] is 2
          Value[43] is 3
          Value[44] is 4
          Value[45] is 5
          Value[46] is 6
          Value[47] is 7
          Value[48] is 8
          Value[49] is 9@

          My OpenSource software at: http://muldersoft.com/

          Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

          Go visit the coop: http://youtu.be/Jay...

          1 Reply Last reply
          0
          • A Offline
            A Offline
            anmol2701
            wrote on 3 May 2014, 04:44 last edited by
            #5

            arr2=0×00;

            here i get exception. char array takes 0x00 as a null value and terminates the array. all the values entered after null cause buffer overflow.

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              zeljko
              wrote on 3 May 2014, 11:26 last edited by
              #6

              Have you tried using QByteArray for that purpose ?

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MuldeR
                wrote on 3 May 2014, 12:00 last edited by
                #7

                [quote author="anmol2701" date="1399092247"]arr2=0×00;

                here i get exception.[/quote]

                Then you obviously have some other issue in your code. But at this point it's impossible to say without seeing your complete code...

                [quote author="anmol2701" date="1399092247"]
                char array takes 0x00 as a null value and terminates the array. all the values entered after null cause buffer overflow.[/quote]

                No, it certainly doesn't! As said before, the value 0x00 has no special meaning at all, except when interpreting the content of the array as a C string. And even then, 0x00 only identifies the end of the string, not the end of the array. You can perfectly write to array positions after the NULL-terminator - as long as you are writing to positions that still are inside the array bounds.

                You can try "the above sample code":http://qt-project.org/forums/viewreply/174569/, if you don't believe ;-)

                __

                Or just try this:
                @int main()
                {
                char test[42];
                test[0] = 'T';
                test[1] = 'E';
                test[2] = 'S';
                test[3] = 'T';
                test[4] = '\n';
                test[5] = 0x00; //<-- First terminator
                test[6] = 'E';
                test[7] = 'V';
                test[8] = 'I';
                test[9] = 'L';
                test[10] = '!';
                test[11] = '\n';
                test[12] = 0x00; //<-- Second terminator

                printf("Result #1: %s", test);
                printf("Result #2: %s", &test[6]);

                return 0;
                }@

                Expected Output:
                @Result #1: TEST
                Result #2: EVIL!@

                My OpenSource software at: http://muldersoft.com/

                Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                Go visit the coop: http://youtu.be/Jay...

                1 Reply Last reply
                0
                • Z Offline
                  Z Offline
                  zeljko
                  wrote on 3 May 2014, 12:08 last edited by
                  #8

                  My wild guess is that his array isn't sized well, and it have nothing to do with 0x00. It's impossible to help him without provided code example which crashes.

                  1 Reply Last reply
                  0

                  8/8

                  3 May 2014, 12:08

                  • Login

                  • Login or register to search.
                  8 out of 8
                  • First post
                    8/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved