Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Storing null value in a char array

    General and Desktop
    3
    8
    1746
    Loading More Posts
    • 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
      anmol2701 last edited by

      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 Reply Quote 0
      • M
        MuldeR last edited by

        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 Reply Quote 0
        • A
          anmol2701 last edited by

          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 Reply Quote 0
          • M
            MuldeR last edited by

            [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 Reply Quote 0
            • A
              anmol2701 last edited by

              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 Reply Quote 0
              • Z
                zeljko last edited by

                Have you tried using QByteArray for that purpose ?

                1 Reply Last reply Reply Quote 0
                • M
                  MuldeR last edited by

                  [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 Reply Quote 0
                  • Z
                    zeljko last edited by

                    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 Reply Quote 0
                    • First post
                      Last post