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. ByteArray manipulation issue
Qt 6.11 is out! See what's new in the release blog

ByteArray manipulation issue

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 5 Posters 1.8k 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.
  • J Offline
    J Offline
    Joseph22121
    wrote on last edited by VRonin
    #1

    What is wrong with my code and how to fix it?

    #include <QByteArray>
    
    const char* getString()
    
    {
    
    QByteArray ba("Sample output");
    
    return ba.constData();
    
    }
    
    int main(int argc, char *argv[])
    
    {
    
    const char *str = getString();
    
    printf("%s\n", str);
    
    return 0;
    
    }
    
    J.HilkJ 1 Reply Last reply
    0
    • J Joseph22121

      What is wrong with my code and how to fix it?

      #include <QByteArray>
      
      const char* getString()
      
      {
      
      QByteArray ba("Sample output");
      
      return ba.constData();
      
      }
      
      int main(int argc, char *argv[])
      
      {
      
      const char *str = getString();
      
      printf("%s\n", str);
      
      return 0;
      
      }
      
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Joseph22121

      https://doc.qt.io/qt-5/qbytearray.html#constData

      The pointer remains valid as long as the byte array isn't reallocated or destroyed.

      the byte array is destroyed as soon as the code reaches } therefore the returned pointer becomes invalid


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      4
      • J Offline
        J Offline
        Joseph22121
        wrote on last edited by
        #3

        Thanks for your quick comments. I am beginner to Qt APIs. How to fix this code? Please advise me here.

        J.HilkJ 1 Reply Last reply
        0
        • J Joseph22121

          Thanks for your quick comments. I am beginner to Qt APIs. How to fix this code? Please advise me here.

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @Joseph22121 said in ByteArray manipulation issue:

          Thanks for your quick comments. I am beginner to Qt APIs. How to fix this code? Please advise me here.

          well, thats not really a Qt issue but a C++ one

          you have to make your data persistent.

          • make the byte array a member variable
          • make it static //bad idea
          • memcpy it to heap allocated char array
          • return the byte array instead of a pointer to the internal data
          • ask yourself do you really want/need printf or could you simply use something else, maybe QDebug

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          J 1 Reply Last reply
          3
          • J.HilkJ J.Hilk

            @Joseph22121 said in ByteArray manipulation issue:

            Thanks for your quick comments. I am beginner to Qt APIs. How to fix this code? Please advise me here.

            well, thats not really a Qt issue but a C++ one

            you have to make your data persistent.

            • make the byte array a member variable
            • make it static //bad idea
            • memcpy it to heap allocated char array
            • return the byte array instead of a pointer to the internal data
            • ask yourself do you really want/need printf or could you simply use something else, maybe QDebug
            J Offline
            J Offline
            Joseph22121
            wrote on last edited by
            #5

            @J-Hilk Do you mean to say to dynamically allocate the memory using new operator in c++?

            J.HilkJ 1 Reply Last reply
            1
            • J Joseph22121

              @J-Hilk Do you mean to say to dynamically allocate the memory using new operator in c++?

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @Joseph22121 this is clearly a broken down example of your actual issue. And as long as I don't know your real situation, I can not recommend a solution for your.

              You could simply new the QByteArray, yes, but that will cause for this simple function:

              const char* getString()
              
              {
              
               QByteArray *ba(newQByteArray("Sample output"));
              
              return ba->constData();
              
              }
              

              a memory leak

              why do you go the way over the byte array anyway?

              const char* getString()
              {
                  return "Sample output";
              }
              
              int main()
              
              {
              
                  const char *str = getString();
              
                  printf("%s\n", str);
              
                  delete str;
                  return 0;
              
              }
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              1
              • J Offline
                J Offline
                Joseph22121
                wrote on last edited by
                #7

                @J-Hilk said in ByteArray manipulation issue:

                QByteArray *ba(newQByteArray("Sample output"));

                return ba->constData();

                Thanks for your quick response. Here QByteArray class provides an array of bytes.QByteArray can embed '\0' bytes. QByteArray makes a deep copy of the const char * data, so you can modify it later without experiencing side effects.
                Do you mean to use this way to avoid memory leak issue?

                #include <QByteArray>

                const char* getString()

                {

                QByteArray *ba(newQByteArray("Sample output"));

                return ba->constData();

                }

                int main(int argc, char *argv[])

                {

                const char *str = getString();

                printf("%s\n", str);
                delete str;

                return 0;

                }

                jsulmJ 1 Reply Last reply
                0
                • J Joseph22121

                  @J-Hilk said in ByteArray manipulation issue:

                  QByteArray *ba(newQByteArray("Sample output"));

                  return ba->constData();

                  Thanks for your quick response. Here QByteArray class provides an array of bytes.QByteArray can embed '\0' bytes. QByteArray makes a deep copy of the const char * data, so you can modify it later without experiencing side effects.
                  Do you mean to use this way to avoid memory leak issue?

                  #include <QByteArray>

                  const char* getString()

                  {

                  QByteArray *ba(newQByteArray("Sample output"));

                  return ba->constData();

                  }

                  int main(int argc, char *argv[])

                  {

                  const char *str = getString();

                  printf("%s\n", str);
                  delete str;

                  return 0;

                  }

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Joseph22121 said in ByteArray manipulation issue:

                  QByteArray makes a deep copy of the const char * data

                  No, it does not! Read https://doc.qt.io/qt-5/qbytearray.html#constData
                  "The pointer remains valid as long as the byte array isn't reallocated or destroyed" - and this is exactly what is happening here!

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    Joseph22121
                    wrote on last edited by
                    #9

                    so how to fix the issue here?

                    Below is what I thought to do. Is it correct?

                    const char* getString()
                    {
                    QByteArray ba("Sample output"); // creates byte array of data
                    return strdup(ba.constdata()); // this will do the copy string to heap allocated memory
                    }

                    JonBJ 1 Reply Last reply
                    0
                    • J Joseph22121

                      so how to fix the issue here?

                      Below is what I thought to do. Is it correct?

                      const char* getString()
                      {
                      QByteArray ba("Sample output"); // creates byte array of data
                      return strdup(ba.constdata()); // this will do the copy string to heap allocated memory
                      }

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by JonB
                      #10

                      @Joseph22121
                      I guess you could, but why are you doing any of this with QByteArray? Your goal seems to be to return a char * pointing to "Sample output", you can do that without going anywhere near Qt classes or the complications you are introducing.

                      Also

                      const char *str = getString();
                      delete str;
                      

                      I don't think/know that you can delete a const char *. In any case, you must not delete something allocated via strdup(), which uses malloc().

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        Joseph22121
                        wrote on last edited by
                        #11

                        Do you mean this code is fine? I have avoided using QByteArray here.

                        const char* getString()
                        {
                        return "Sample output";
                        }

                        JonBJ 1 Reply Last reply
                        0
                        • J Joseph22121

                          Do you mean this code is fine? I have avoided using QByteArray here.

                          const char* getString()
                          {
                          return "Sample output";
                          }

                          JonBJ Online
                          JonBJ Online
                          JonB
                          wrote on last edited by JonB
                          #12

                          @Joseph22121
                          Sure! If you just want to return some constant string, for whatever reason. Certainly so far you have nothing which needs a QByteArray, unless your real code wants to do something different from what you show.

                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            Joseph22121
                            wrote on last edited by
                            #13

                            Can I conclude the fix for the code as below?

                            #include <QByteArray>

                            const char* getString()
                            {
                            return "Sample output";
                            }

                            int main()

                            {

                            const char *str = getString();
                            
                            printf("%s\n", str);
                            
                            return 0;
                            

                            }

                            1 Reply Last reply
                            0
                            • J Offline
                              J Offline
                              Joseph22121
                              wrote on last edited by
                              #14

                              I think using QByteArray object ba is not required here. Have modified the code as follows. Please review it.
                              #include <QByteArray>

                              const char* getstring()
                              {
                              static const char *const MY_STRING = "Sample output"; // its a const string in static memory
                              return MY_STRING;
                              }

                              int main()

                              {

                              const char *str = getString();
                              
                              printf("%s\n", str);
                              
                              return 0;
                              

                              }

                              jsulmJ JonBJ 2 Replies Last reply
                              0
                              • J Joseph22121

                                I think using QByteArray object ba is not required here. Have modified the code as follows. Please review it.
                                #include <QByteArray>

                                const char* getstring()
                                {
                                static const char *const MY_STRING = "Sample output"; // its a const string in static memory
                                return MY_STRING;
                                }

                                int main()

                                {

                                const char *str = getString();
                                
                                printf("%s\n", str);
                                
                                return 0;
                                

                                }

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @Joseph22121 Yes, static variable is one of the solutions suggested by @J-Hilk ...

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0
                                • J Joseph22121

                                  I think using QByteArray object ba is not required here. Have modified the code as follows. Please review it.
                                  #include <QByteArray>

                                  const char* getstring()
                                  {
                                  static const char *const MY_STRING = "Sample output"; // its a const string in static memory
                                  return MY_STRING;
                                  }

                                  int main()

                                  {

                                  const char *str = getString();
                                  
                                  printf("%s\n", str);
                                  
                                  return 0;
                                  

                                  }

                                  JonBJ Online
                                  JonBJ Online
                                  JonB
                                  wrote on last edited by JonB
                                  #16

                                  @Joseph22121 said in ByteArray manipulation issue:

                                  const char* getstring()
                                  {
                                  static const char *const MY_STRING = "Sample output"; // its a const string in static memory
                                  return MY_STRING;
                                  }

                                  You are welcome to write it like this if it makes you happy/reads clearer. But just be aware this does nothing different from plain:

                                  const char* getstring()
                                  {
                                      return "Sample output";   // this is also a const string in static memory!
                                  }
                                  
                                  J 1 Reply Last reply
                                  1
                                  • JonBJ JonB

                                    @Joseph22121 said in ByteArray manipulation issue:

                                    const char* getstring()
                                    {
                                    static const char *const MY_STRING = "Sample output"; // its a const string in static memory
                                    return MY_STRING;
                                    }

                                    You are welcome to write it like this if it makes you happy/reads clearer. But just be aware this does nothing different from plain:

                                    const char* getstring()
                                    {
                                        return "Sample output";   // this is also a const string in static memory!
                                    }
                                    
                                    J Offline
                                    J Offline
                                    Joseph22121
                                    wrote on last edited by
                                    #17

                                    @JonB Thanks everyone for your inputs and suggestions. This thread can be closed.

                                    Christian EhrlicherC 1 Reply Last reply
                                    0
                                    • J Joseph22121

                                      @JonB Thanks everyone for your inputs and suggestions. This thread can be closed.

                                      Christian EhrlicherC Online
                                      Christian EhrlicherC Online
                                      Christian Ehrlicher
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      @Joseph22121 said in ByteArray manipulation issue:

                                      This thread can be closed.

                                      You can do this with the Topic Tools in your first post.

                                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                      Visit the Qt Academy at https://academy.qt.io/catalog

                                      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