Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Run-Time Check Failure #2 - Stack around the variable 'obj1' was corrupted.
Forum Updated to NodeBB v4.3 + New Features

Run-Time Check Failure #2 - Stack around the variable 'obj1' was corrupted.

Scheduled Pinned Locked Moved C++ Gurus
13 Posts 6 Posters 10.1k 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.
  • T Offline
    T Offline
    thisisbhaskar
    wrote on last edited by
    #3

    The first problem I see the way you delete data.
    it should be
    @ delete[] data @

    Correct this, run and see if you get the error again.

    1 Reply Last reply
    0
    • I Offline
      I Offline
      Indrajeet
      wrote on last edited by
      #4

      Hi Denis

      @
      opens the file to read
      QString str = myfile.readAll();//Reads The full file
      QByteArray ba = str.toLatin1();
      int iByteArraySize = ba.size();
      char *chrData = new char[iByteArraySize + 1];
      strcpy(chrData,ba.data());
      myfile.close();
      return chrData;@

      1 Reply Last reply
      0
      • S Offline
        S Offline
        situ117
        wrote on last edited by
        #5

        @Class A
        {
        public:
        char *data=NULL;
        A()
        {
        }
        @
        BTW Line number 4 is an invalid C++ statement. Initialize data in A's constructor.

        1 Reply Last reply
        0
        • I Offline
          I Offline
          Indrajeet
          wrote on last edited by
          #6

          Hi vijay

          Thanks for reply.But still same error.

          1 Reply Last reply
          0
          • I Offline
            I Offline
            Indrajeet
            wrote on last edited by
            #7

            Hi situ

            I have done it in constructor only.
            Here by mistake i wrote like that.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              thisisbhaskar
              wrote on last edited by
              #8

              I don't see anything wrong with the code.. unless you are hiding some code from us :)

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tobias.hunger
                wrote on last edited by
                #9

                Why don't you use QByteArray in favor of char*? That is way safer to work with and should have very similar memory and run-time characteristics: It is basically just a char * that is guaranteed to end in '\0'.

                @
                QString str = myfile.readAll();//Reads The full file
                QByteArray ba = str.toLatin1();
                int iByteArraySize = ba.size();
                char *chrData = new char[iByteArraySize + 1];
                strcpy(chrData,ba.data());
                @

                First line reads a the file in the form of a QByteArray into a UTF-16 encoded string, assuming the input to be ASCII encoded IIRC (but I might be wrong here in which case the default text codec will be used). This is lossy as all special characters, even those in the Latin1 range, are getting dropped or damaged by the recoding. Then you turn the UTF-16 encoded string back into a Latin1-encoded QByteArray (save since by now you have ASCII chars only and those are a subset of Latin1). Finally you copy that over into a char*. That is not really efficient: You got 4 copies (file to bytearray, bytearray to string, string to bytearray and bytearray to char*) and 2 format conversions (from 1byte per character to 2byte chars and back) going on here.

                Plus the strcpy is not the safest operation around, even though it is fine here since QByteArray makes sure the string is terminated by '\0'.

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  Indrajeet
                  wrote on last edited by
                  #10

                  Hi Hunger

                  Thanks for the reply.

                  Can you just show me how to avoid loosing special characters or any other characters.
                  But the final buffer should be char*.

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    loladiro
                    wrote on last edited by
                    #11

                    Could you elaborate on why you need a char*? Also, what do you want to store in your char* (i.e. what encoding do you want)?

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      Indrajeet
                      wrote on last edited by
                      #12

                      Hi

                      I want to read text files.I want data in char* because i am calling this function in win32 app. which do not know QByteArray.

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        tobias.hunger
                        wrote on last edited by
                        #13

                        That all depends on how your file is encoded... you need to decode it properly using the right text codec.

                        1 Reply Last reply
                        0

                        • Login

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