Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Trying to Disable string operator=

Trying to Disable string operator=

Scheduled Pinned Locked Moved C / C++ / MFC
toolscsharpvisual-studiodata-structures
9 Posts 4 Posters 0 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.
  • F Offline
    F Offline
    ForNow
    wrote on last edited by
    #1

    Hi I have a number of hex data I am trying to get to printable data I mean ascii I read them as a byte[] array where byte is typedef for usigned char I just got this idea it might be easier to manipulate it as string i mean like std::string so I developed this routine

    // converts character array
    // to string and returns it
    string convertToString(unsigned char* a, int size)
    {
    int i;
    string s = "";
    for (i = 0; i < size; i++) {
    s += a[i];
    }
    return s;
    }

    Here is the piece of code that calls it

    returnsource->stmt = convertToString(sourcerecordpointer->statementnumber, 4)

    here is the definition

    BYTE statementnumber[4];

    the data in statement ithe statement number in hex so statement 1 would be x'00000001' so my goal the would be to retrun retrun->stmt as character 0001 or x30303031 Now me thinks that since return->stmt is defined as a string

    struct source
    {
    string stmt;
    string loc;
    string addr1;
    string addr2;

    that the string operator= is invoked becasue of this I end up here in Microsoft code in a member utility and get a read exception this is the path

    C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.34.31933\include\utility

    is there any way I can tell MSVC compiler that all I am trying to do with = operator is accept a return value from my subroutine

    template
    _CONSTEXPR20 _Ty exchange(_Ty& _Val, _Other&& _New_val) noexcept(
    conjunction_v, is_nothrow_assignable<_Ty&, _Other>>) {
    // assign _New_val to _Val, return previous _Val
    _Ty _Old_val = static_cast<_Ty&&>(_Val);
    _Val = static_cast<_Other&&>(_New_val);
    return _Old_val;
    }

    K V C 3 Replies Last reply
    0
    • F ForNow

      Hi I have a number of hex data I am trying to get to printable data I mean ascii I read them as a byte[] array where byte is typedef for usigned char I just got this idea it might be easier to manipulate it as string i mean like std::string so I developed this routine

      // converts character array
      // to string and returns it
      string convertToString(unsigned char* a, int size)
      {
      int i;
      string s = "";
      for (i = 0; i < size; i++) {
      s += a[i];
      }
      return s;
      }

      Here is the piece of code that calls it

      returnsource->stmt = convertToString(sourcerecordpointer->statementnumber, 4)

      here is the definition

      BYTE statementnumber[4];

      the data in statement ithe statement number in hex so statement 1 would be x'00000001' so my goal the would be to retrun retrun->stmt as character 0001 or x30303031 Now me thinks that since return->stmt is defined as a string

      struct source
      {
      string stmt;
      string loc;
      string addr1;
      string addr2;

      that the string operator= is invoked becasue of this I end up here in Microsoft code in a member utility and get a read exception this is the path

      C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.34.31933\include\utility

      is there any way I can tell MSVC compiler that all I am trying to do with = operator is accept a return value from my subroutine

      template
      _CONSTEXPR20 _Ty exchange(_Ty& _Val, _Other&& _New_val) noexcept(
      conjunction_v, is_nothrow_assignable<_Ty&, _Other>>) {
      // assign _New_val to _Val, return previous _Val
      _Ty _Old_val = static_cast<_Ty&&>(_Val);
      _Val = static_cast<_Other&&>(_New_val);
      return _Old_val;
      }

      K Offline
      K Offline
      k5054
      wrote on last edited by
      #2

      Since one of the constructors for std::string is

      std::string(const char *, size_t count)

      you can do your assignment using

      unsigned char datum[6]{ 'A', 'B', 'C', 'D', 'E', 'F' }; // a non-nul terminated sequence of bytes
      std::string str(reinterpret_cast(datum), 4); // assuming we just want the first 4 bytes from datum

      Since you're getting a read exception, that suggests to me that given

      returnsource->stmt = convertToString(sourcerecordpointer->statementnumber, 4);

      then probably your sourcrecordpointer is invalid.

      Keep Calm and Carry On

      F 1 Reply Last reply
      0
      • K k5054

        Since one of the constructors for std::string is

        std::string(const char *, size_t count)

        you can do your assignment using

        unsigned char datum[6]{ 'A', 'B', 'C', 'D', 'E', 'F' }; // a non-nul terminated sequence of bytes
        std::string str(reinterpret_cast(datum), 4); // assuming we just want the first 4 bytes from datum

        Since you're getting a read exception, that suggests to me that given

        returnsource->stmt = convertToString(sourcerecordpointer->statementnumber, 4);

        then probably your sourcrecordpointer is invalid.

        Keep Calm and Carry On

        F Offline
        F Offline
        ForNow
        wrote on last edited by
        #3

        Just an fyi When I removed the return value Meaning I just had convetToString(sourcerecordpointer->statementnumber,4); I didn’t get the exception me thinks it has something to do with the lvalue

        1 Reply Last reply
        0
        • F ForNow

          Hi I have a number of hex data I am trying to get to printable data I mean ascii I read them as a byte[] array where byte is typedef for usigned char I just got this idea it might be easier to manipulate it as string i mean like std::string so I developed this routine

          // converts character array
          // to string and returns it
          string convertToString(unsigned char* a, int size)
          {
          int i;
          string s = "";
          for (i = 0; i < size; i++) {
          s += a[i];
          }
          return s;
          }

          Here is the piece of code that calls it

          returnsource->stmt = convertToString(sourcerecordpointer->statementnumber, 4)

          here is the definition

          BYTE statementnumber[4];

          the data in statement ithe statement number in hex so statement 1 would be x'00000001' so my goal the would be to retrun retrun->stmt as character 0001 or x30303031 Now me thinks that since return->stmt is defined as a string

          struct source
          {
          string stmt;
          string loc;
          string addr1;
          string addr2;

          that the string operator= is invoked becasue of this I end up here in Microsoft code in a member utility and get a read exception this is the path

          C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.34.31933\include\utility

          is there any way I can tell MSVC compiler that all I am trying to do with = operator is accept a return value from my subroutine

          template
          _CONSTEXPR20 _Ty exchange(_Ty& _Val, _Other&& _New_val) noexcept(
          conjunction_v, is_nothrow_assignable<_Ty&, _Other>>) {
          // assign _New_val to _Val, return previous _Val
          _Ty _Old_val = static_cast<_Ty&&>(_Val);
          _Val = static_cast<_Other&&>(_New_val);
          return _Old_val;
          }

          V Offline
          V Offline
          Victor Nijegorodov
          wrote on last edited by
          #4

          ForNow wrote:

          I have a number of hex data I am trying to get to printable data I mean ascii I read them as a byte[] array where byte is typedef for usigned char I just got this idea it might be easier to manipulate it as string i mean like std::string

          I think it is not a good idea to consider a byte array as a string. It is only possible if you are 100% sure there is no any NULL (0x00) byte within this byte array, otherwise you string would represent only the part of the array up to the first NULL byte.

          F 1 Reply Last reply
          0
          • F ForNow

            Hi I have a number of hex data I am trying to get to printable data I mean ascii I read them as a byte[] array where byte is typedef for usigned char I just got this idea it might be easier to manipulate it as string i mean like std::string so I developed this routine

            // converts character array
            // to string and returns it
            string convertToString(unsigned char* a, int size)
            {
            int i;
            string s = "";
            for (i = 0; i < size; i++) {
            s += a[i];
            }
            return s;
            }

            Here is the piece of code that calls it

            returnsource->stmt = convertToString(sourcerecordpointer->statementnumber, 4)

            here is the definition

            BYTE statementnumber[4];

            the data in statement ithe statement number in hex so statement 1 would be x'00000001' so my goal the would be to retrun retrun->stmt as character 0001 or x30303031 Now me thinks that since return->stmt is defined as a string

            struct source
            {
            string stmt;
            string loc;
            string addr1;
            string addr2;

            that the string operator= is invoked becasue of this I end up here in Microsoft code in a member utility and get a read exception this is the path

            C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.34.31933\include\utility

            is there any way I can tell MSVC compiler that all I am trying to do with = operator is accept a return value from my subroutine

            template
            _CONSTEXPR20 _Ty exchange(_Ty& _Val, _Other&& _New_val) noexcept(
            conjunction_v, is_nothrow_assignable<_Ty&, _Other>>) {
            // assign _New_val to _Val, return previous _Val
            _Ty _Old_val = static_cast<_Ty&&>(_Val);
            _Val = static_cast<_Other&&>(_New_val);
            return _Old_val;
            }

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            I compiled, with Visual C++ 2022, and run (successfully) the following code:

            #include using namespace std;

            typedef unsigned char BYTE;

            struct source
            {
            string stmt;
            string loc;
            string addr1;
            string addr2;
            };

            // converts character array
            // to string and returns it
            string convertToString(unsigned char* a, int size)
            {
            int i;
            string s = "";
            for (i = 0; i < size; i++) {
            s += a[i];
            }
            return s;
            }

            int main()
            {

            BYTE statementnumber[4];

            source * returnsource = new source;

            returnsource->stmt = convertToString(statementnumber, 4);

            delete returnsource;
            }

            "In testa che avete, Signor di Ceprano?" -- Rigoletto

            F 1 Reply Last reply
            0
            • C CPallini

              I compiled, with Visual C++ 2022, and run (successfully) the following code:

              #include using namespace std;

              typedef unsigned char BYTE;

              struct source
              {
              string stmt;
              string loc;
              string addr1;
              string addr2;
              };

              // converts character array
              // to string and returns it
              string convertToString(unsigned char* a, int size)
              {
              int i;
              string s = "";
              for (i = 0; i < size; i++) {
              s += a[i];
              }
              return s;
              }

              int main()
              {

              BYTE statementnumber[4];

              source * returnsource = new source;

              returnsource->stmt = convertToString(statementnumber, 4);

              delete returnsource;
              }

              "In testa che avete, Signor di Ceprano?" -- Rigoletto

              F Offline
              F Offline
              ForNow
              wrote on last edited by
              #6

              I look at my code again the only difference I see is that my code is part of a DLL the pointer “return source” is a parameter passed by the invoking program it allocated on the heap via new Thanks

              1 Reply Last reply
              0
              • V Victor Nijegorodov

                ForNow wrote:

                I have a number of hex data I am trying to get to printable data I mean ascii I read them as a byte[] array where byte is typedef for usigned char I just got this idea it might be easier to manipulate it as string i mean like std::string

                I think it is not a good idea to consider a byte array as a string. It is only possible if you are 100% sure there is no any NULL (0x00) byte within this byte array, otherwise you string would represent only the part of the array up to the first NULL byte.

                F Offline
                F Offline
                ForNow
                wrote on last edited by
                #7

                That’s a good idea when I read the byte array for any value between 0x00 and 0x09 I’ll add 48 Those values between 0x0a and 0x0f I’ll add 55 After I finish inserting the bytes do I have to add a null 0x00 to complete the string or string will do that automatically ?

                V 1 Reply Last reply
                0
                • F ForNow

                  That’s a good idea when I read the byte array for any value between 0x00 and 0x09 I’ll add 48 Those values between 0x0a and 0x0f I’ll add 55 After I finish inserting the bytes do I have to add a null 0x00 to complete the string or string will do that automatically ?

                  V Offline
                  V Offline
                  Victor Nijegorodov
                  wrote on last edited by
                  #8

                  ForNow wrote:

                  After I finish inserting the bytes do I have to add a null 0x00 to complete the string or string will do that automatically ?

                  If you mean an std::string then you you don't have to add a terminating null character.

                  F 1 Reply Last reply
                  0
                  • V Victor Nijegorodov

                    ForNow wrote:

                    After I finish inserting the bytes do I have to add a null 0x00 to complete the string or string will do that automatically ?

                    If you mean an std::string then you you don't have to add a terminating null character.

                    F Offline
                    F Offline
                    ForNow
                    wrote on last edited by
                    #9

                    Thanks I think the fact that I was inserting a null byte into a string caused the assertion Thank you

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups