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. How to safely get data from QBytearray * into unsigned char * const (openssl related)
Forum Updated to NodeBB v4.3 + New Features

How to safely get data from QBytearray * into unsigned char * const (openssl related)

Scheduled Pinned Locked Moved Solved C++ Gurus
5 Posts 3 Posters 5.2k 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.
  • levi.plL Offline
    levi.plL Offline
    levi.pl
    wrote on last edited by levi.pl
    #1

    My goal is to safely pass QBytearray contents to openssl function for which I wrote wrapper. It requires unsigned char array as parameter. So far I'm here:

    foo(QByteArray *  _data);
     {
     unsigned char *data = new unsigned char[_data->size()];
     data=reinterpret_cast<unsigned char*>(data->data());
     }
    

    I would like to protect _data from accidental change and use unsigned char * const in declaration of data.

    • I could pass QByteArray as value. Would system make deep copy immediately or after I tried to modify it ? How to best protect it from accidental modification inside in this case ?
    • I can't picture in my head putting together *const = new and assigning value in one line. Can you please help me out on this one ?

    Thank you.

    jsulmJ 1 Reply Last reply
    0
    • levi.plL levi.pl

      My goal is to safely pass QBytearray contents to openssl function for which I wrote wrapper. It requires unsigned char array as parameter. So far I'm here:

      foo(QByteArray *  _data);
       {
       unsigned char *data = new unsigned char[_data->size()];
       data=reinterpret_cast<unsigned char*>(data->data());
       }
      

      I would like to protect _data from accidental change and use unsigned char * const in declaration of data.

      • I could pass QByteArray as value. Would system make deep copy immediately or after I tried to modify it ? How to best protect it from accidental modification inside in this case ?
      • I can't picture in my head putting together *const = new and assigning value in one line. Can you please help me out on this one ?

      Thank you.

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

      @levi.pl Why not just:

      foo(QByteArray *  _data);
       {
           const unsigned char *data = reinterpret_cast<const unsigned char*>(_data->constData());
       }
      

      No need to allocate any new memory.

      In your version you do not copy the data from QByteArray to newly allocated array, as this assignment just assigns a new pointer value to data:

      data=reinterpret_cast<unsigned char*>(data->data());
      

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

      1 Reply Last reply
      4
      • Paul ColbyP Offline
        Paul ColbyP Offline
        Paul Colby
        wrote on last edited by
        #3

        Hi @levi-pl,

        I'm not sure I understand your question fully, but it sounds like you want to use QByteArray::constData().

        const char * const data = _data.constData();
        

        And if you specifically want unsigned chars, then you can cast like so:

        const uchar * const data = reinterpret_cast<const uchar *>(_data.constData());
        

        Is that what you were asking?

        1 Reply Last reply
        2
        • Paul ColbyP Offline
          Paul ColbyP Offline
          Paul Colby
          wrote on last edited by
          #4

          @jsulm beat me to it :)

          levi.plL 1 Reply Last reply
          0
          • Paul ColbyP Paul Colby

            @jsulm beat me to it :)

            levi.plL Offline
            levi.plL Offline
            levi.pl
            wrote on last edited by
            #5

            @Paul-Colby @jsulm :

            I'm still switching my brain to think in C/C++. One step closer. Thank you guys. Confirmed kill.

            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