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. std:: container cast to C style array
Forum Updated to NodeBB v4.3 + New Features

std:: container cast to C style array

Scheduled Pinned Locked Moved Solved C++ Gurus
20 Posts 4 Posters 3.5k Views 2 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #10

    @JKSH said in std:: container cast to C style array:

    I'm puzzled as to why the function designer allows the char values to be modified

    Maybe for compatibility with main() entry point?

    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
    2
    • JKSHJ JKSH

      @Kent-Dorfman said in std:: container cast to C style array:

      Ugh! closer inspection shows the prototype is
      char* const argv[],
      which may be different than
      const char* argv[]

      Lemme check!

      They have different rules:

      • const char *: Char data is const. Pointer is non-const.
      • char * const: Char data is non-const. Pointer is const.

      Which means:

      • std::vector<const char*>: Char data cannot be modified through the pointer.
      • char* const argv[]: Char data can be modified through the pointer.

      The only way to pass the vector data into your C function is by using a const_cast like @Christian-Ehrlicher showed.

      (P.S. I just found out that execvp() is part of the standard POSIX API. I'm puzzled as to why the function designer allows the char values to be modified)

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #11

      @JKSH , @Christian-Ehrlicher
      TBH, I'm puzzled as to why the argv argument to main() is not declared

      const char * const argv[];
      
      1 Reply Last reply
      0
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #12

        @Christian-Ehrlicher et al

        thanks @Christian-Ehrlicher ... my misstating the problem in the OP led to my own confuction. It was in fact solved by the proper const_cast. due to my misstaing ofthe const char* vs const* char, which were different. No QProcess as it's pure C/C++ ISO with the only externa dependencies to the project being anything that is implemented in pure C++ template headers. Test harness for spacecraft OS and I needed to spawn many clients with different argv params. complete std::thread run method is:

                static void RunProgram(ThreadWrapper* instance) {
                    int pid = fork();
                    if (pid == 0) {
                        static const size_t STRLENGTH = 6;
                        char instanceStr[STRLENGTH];
                        char cmd[STRLENGTH];
                        assert(snprintf(instanceStr, STRLENGTH, "%d", instance->instanceNum) > 0);
                        assert(snprintf(cmd, STRLENGTH, "%s", instance->callType.c_str()) > 0);
                        std::vector<const char*> cmdLine =
                                { "./CDSTest", cmd, SHMNAME, nullptr, nullptr };
                        if (instance->callType == "-c") {
                            cmdLine[3] = instanceStr;
                        } else {
                            cmdLine.resize(4);
                        }
                        assert(execvp("./CDSTest", const_cast<char**>(cmdLine.data())) >= 0);
                    }
                    int status(0);
                    instance->pid = pid;
                    assert(waitpid(pid, &status, 0) >= 0);
        
                    instance->retval = WEXITSTATUS(status);
                        // stall thread until command completes
                }
        
        

        Solved.

        JonBJ 1 Reply Last reply
        2
        • Kent-DorfmanK Kent-Dorfman

          @Christian-Ehrlicher et al

          thanks @Christian-Ehrlicher ... my misstating the problem in the OP led to my own confuction. It was in fact solved by the proper const_cast. due to my misstaing ofthe const char* vs const* char, which were different. No QProcess as it's pure C/C++ ISO with the only externa dependencies to the project being anything that is implemented in pure C++ template headers. Test harness for spacecraft OS and I needed to spawn many clients with different argv params. complete std::thread run method is:

                  static void RunProgram(ThreadWrapper* instance) {
                      int pid = fork();
                      if (pid == 0) {
                          static const size_t STRLENGTH = 6;
                          char instanceStr[STRLENGTH];
                          char cmd[STRLENGTH];
                          assert(snprintf(instanceStr, STRLENGTH, "%d", instance->instanceNum) > 0);
                          assert(snprintf(cmd, STRLENGTH, "%s", instance->callType.c_str()) > 0);
                          std::vector<const char*> cmdLine =
                                  { "./CDSTest", cmd, SHMNAME, nullptr, nullptr };
                          if (instance->callType == "-c") {
                              cmdLine[3] = instanceStr;
                          } else {
                              cmdLine.resize(4);
                          }
                          assert(execvp("./CDSTest", const_cast<char**>(cmdLine.data())) >= 0);
                      }
                      int status(0);
                      instance->pid = pid;
                      assert(waitpid(pid, &status, 0) >= 0);
          
                      instance->retval = WEXITSTATUS(status);
                          // stall thread until command completes
                  }
          
          

          Solved.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #13

          @Kent-Dorfman
          I am surprised at your usage of assert(). You are using it with a (vital) function call as its argument. Have you tested it, in Release mode, with a variety of compilers? Assuming https://en.cppreference.com/w/cpp/error/assert is "recognised" for C++ ISO (I don't know whether it is), it claims it will indeed be a macro with the definition being like:

          // Defined in header <cassert>
          #ifdef NDEBUG
          #define assert(condition) ((void)0)
          #else
          #define assert(condition) /*implementation defined*/
          #endif
          

          ?

          [I have no idea what spacecraft OS is, but I'm very worried that yours is going to come crashing onto my head when you launch it non-debug mode ;-) ]

          Christian EhrlicherC 1 Reply Last reply
          2
          • JonBJ JonB

            @Kent-Dorfman
            I am surprised at your usage of assert(). You are using it with a (vital) function call as its argument. Have you tested it, in Release mode, with a variety of compilers? Assuming https://en.cppreference.com/w/cpp/error/assert is "recognised" for C++ ISO (I don't know whether it is), it claims it will indeed be a macro with the definition being like:

            // Defined in header <cassert>
            #ifdef NDEBUG
            #define assert(condition) ((void)0)
            #else
            #define assert(condition) /*implementation defined*/
            #endif
            

            ?

            [I have no idea what spacecraft OS is, but I'm very worried that yours is going to come crashing onto my head when you launch it non-debug mode ;-) ]

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #14

            @JonB You're correct - the usage of assert() is wrong in the code from @Kent-Dorfman

            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
            • Kent-DorfmanK Offline
              Kent-DorfmanK Offline
              Kent-Dorfman
              wrote on last edited by Kent-Dorfman
              #15

              don't read into it. it's a test harness, not flight code. LOL

              AUTOSAR standards forbid use of assert in mission critical code anyway so the existence of the assert() in R&D test code is kind of irrelevant...

              JonBJ 1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #16

                @Kent-Dorfman said in std:: container cast to C style array:

                in R&D test code is kind of irrelevant...

                It's not irrelevant, it's wrong in your case!

                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
                • Kent-DorfmanK Kent-Dorfman

                  don't read into it. it's a test harness, not flight code. LOL

                  AUTOSAR standards forbid use of assert in mission critical code anyway so the existence of the assert() in R&D test code is kind of irrelevant...

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #17

                  @Kent-Dorfman said in std:: container cast to C style array:

                  AUTOSAR standards forbid use of assert in mission critical code anyway so the existence of the assert() in R&D test code is kind of irrelevant...

                  In that case, even more reason not to put the code you want executed as an argument to assert(), only use assert() on a bool result variable? Anyway, your business, so long as you understand what assert() expands to in non-Debug compilation. Your usage just really caught my eye.

                  Kent-DorfmanK 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Kent-Dorfman said in std:: container cast to C style array:

                    AUTOSAR standards forbid use of assert in mission critical code anyway so the existence of the assert() in R&D test code is kind of irrelevant...

                    In that case, even more reason not to put the code you want executed as an argument to assert(), only use assert() on a bool result variable? Anyway, your business, so long as you understand what assert() expands to in non-Debug compilation. Your usage just really caught my eye.

                    Kent-DorfmanK Offline
                    Kent-DorfmanK Offline
                    Kent-Dorfman
                    wrote on last edited by
                    #18
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • Kent-DorfmanK Offline
                      Kent-DorfmanK Offline
                      Kent-Dorfman
                      wrote on last edited by
                      #19

                      OK...I see the point of contention...since assert is itself defined as a macro...

                      JonBJ 1 Reply Last reply
                      1
                      • Kent-DorfmanK Kent-Dorfman

                        OK...I see the point of contention...since assert is itself defined as a macro...

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #20

                        @Kent-Dorfman
                        Indeed so! Hence I pasted the definition, showing that in the NDEBUG case it's not just that it does not verify the expression result, it's that it does not even evaluate (call) it. So any expression which has any side-effects should not be directly passed as argument to assert(), your code will not execute the CDSTest at all! And that will mean the child will continue into the pid != 0 (i.e. parent) code below, and continue running as a forked copy in parallel, with consequences. (IIRC, you should follow an execvp() in child with _exit(...), just in case....)

                        Hence my fear of your rocket maybe crashing onto my head ;-)

                        1 Reply Last reply
                        3

                        • Login

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