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. Including C header that uses temporary arrays
Forum Updated to NodeBB v4.3 + New Features

Including C header that uses temporary arrays

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 5 Posters 1.6k 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.
  • K Offline
    K Offline
    kitfox
    wrote on 14 Apr 2019, 02:53 last edited by kitfox
    #1

    I'm trying to include a third party C library and the header files contain some lines that are apparently valid C but invalid C++:

    #define av_err2str(errnum) \
        av_make_error_string((char[AV_ERROR_MAX_STRING_SIZE]){0}, AV_ERROR_MAX_STRING_SIZE, errnum)
    

    the error message is

    D:\dev\GoldfinchQt\thirdParty\ffmpeg\include\libavutil\error.h:122: error: taking address of temporary array
         av_make_error_string((char[AV_ERROR_MAX_STRING_SIZE]){0}, AV_ERROR_MAX_STRING_SIZE, errnum)
                                                                                                   ^
    

    I'm including these files with extern C, but that doesn't seem to help:

    #define __STDC_CONSTANT_MACROS
    
    extern "C" {
    #include <libavutil/avassert.h>
    #include <libavutil/channel_layout.h>
    #include <libavutil/opt.h>
    #include <libavutil/mathematics.h>
    #include <libavutil/timestamp.h>
    #include <libavformat/avformat.h>
    #include <libswscale/swscale.h>
    #include <libswresample/swresample.h>
    }
    

    I tried declaring arrays explicitly, but this causes problems when the header is included by multiple files.

    Is there a way to get this to compile?

    A 1 Reply Last reply 14 Apr 2019, 05:58
    0
    • K kitfox
      14 Apr 2019, 02:53

      I'm trying to include a third party C library and the header files contain some lines that are apparently valid C but invalid C++:

      #define av_err2str(errnum) \
          av_make_error_string((char[AV_ERROR_MAX_STRING_SIZE]){0}, AV_ERROR_MAX_STRING_SIZE, errnum)
      

      the error message is

      D:\dev\GoldfinchQt\thirdParty\ffmpeg\include\libavutil\error.h:122: error: taking address of temporary array
           av_make_error_string((char[AV_ERROR_MAX_STRING_SIZE]){0}, AV_ERROR_MAX_STRING_SIZE, errnum)
                                                                                                     ^
      

      I'm including these files with extern C, but that doesn't seem to help:

      #define __STDC_CONSTANT_MACROS
      
      extern "C" {
      #include <libavutil/avassert.h>
      #include <libavutil/channel_layout.h>
      #include <libavutil/opt.h>
      #include <libavutil/mathematics.h>
      #include <libavutil/timestamp.h>
      #include <libavformat/avformat.h>
      #include <libswscale/swscale.h>
      #include <libswresample/swresample.h>
      }
      

      I tried declaring arrays explicitly, but this causes problems when the header is included by multiple files.

      Is there a way to get this to compile?

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 14 Apr 2019, 05:58 last edited by aha_1980
      #2

      Hi @kitfox,

      I guess the problem is not the macro itself, but it it seems to be used in the header already - and this is likely causing the compile error.

      I tried declaring arrays explicitly, but this causes problems when the header is included by multiple files.

      You could get around just delaring them extern in the header and provide the array itself in a .cpp file.

      BUT: Appearently the original implementation has them on the stack, and you would put them in the global memory. That could cause problems if multiple functions access the same memory simultanous.

      when the header is included by multiple files.

      Do you need to include it in multiple files? Which definitions from it? Maybe you can include this header in a C file in your project and forward all needed functions from there?

      Edit: original post was mixed-up, I've clearified now.

      Qt has to stay free or it will die.

      K 1 Reply Last reply 14 Apr 2019, 13:38
      2
      • A aha_1980
        14 Apr 2019, 05:58

        Hi @kitfox,

        I guess the problem is not the macro itself, but it it seems to be used in the header already - and this is likely causing the compile error.

        I tried declaring arrays explicitly, but this causes problems when the header is included by multiple files.

        You could get around just delaring them extern in the header and provide the array itself in a .cpp file.

        BUT: Appearently the original implementation has them on the stack, and you would put them in the global memory. That could cause problems if multiple functions access the same memory simultanous.

        when the header is included by multiple files.

        Do you need to include it in multiple files? Which definitions from it? Maybe you can include this header in a C file in your project and forward all needed functions from there?

        Edit: original post was mixed-up, I've clearified now.

        K Offline
        K Offline
        kitfox
        wrote on 14 Apr 2019, 13:38 last edited by
        #3

        @aha_1980 I've found a work-around by just avoiding using the #defines in the example code I was trying to compile. Now I'm creating my buffer in the calling code so the header doesn't have to.

        K 1 Reply Last reply 15 Apr 2019, 22:21
        0
        • K Offline
          K Offline
          Kent-Dorfman
          wrote on 14 Apr 2019, 16:37 last edited by
          #4

          @kitfox said in Including C header that uses temporary arrays:

          I've found a work-around by just avoiding using the #defines in the example code I was trying to compile. Now I'm creating my buffer in the calling code so the header doesn't have to.

          This is a prefered work-around anyway. The error your received makes perfect sense, as it leads to poor programming, and making assumptions about the lifetime of temporary objects is a bad idea...thus the reason for the compiler error. :^)

          1 Reply Last reply
          3
          • K kitfox
            14 Apr 2019, 13:38

            @aha_1980 I've found a work-around by just avoiding using the #defines in the example code I was trying to compile. Now I'm creating my buffer in the calling code so the header doesn't have to.

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 15 Apr 2019, 22:21 last edited by
            #5

            @kitfox said in Including C header that uses temporary arrays:

            I've found a work-around by just avoiding using the #defines in the example code I was trying to compile. Now I'm creating my buffer in the calling code so the header doesn't have to.

            Out of curiosity, could you share how you used the macro? I'm asking because I see no arrays, so I'm trying to understand what the comments were referring to ...

            Read and abide by the Qt Code of Conduct

            J 1 Reply Last reply 16 Apr 2019, 07:22
            0
            • K kshegunov
              15 Apr 2019, 22:21

              @kitfox said in Including C header that uses temporary arrays:

              I've found a work-around by just avoiding using the #defines in the example code I was trying to compile. Now I'm creating my buffer in the calling code so the header doesn't have to.

              Out of curiosity, could you share how you used the macro? I'm asking because I see no arrays, so I'm trying to understand what the comments were referring to ...

              J Offline
              J Offline
              JonB
              wrote on 16 Apr 2019, 07:22 last edited by
              #6

              @kshegunov

              I'm asking because I see no arrays

              (char[AV_ERROR_MAX_STRING_SIZE]){0} ?

              @kitfox
              I don't know the answer, but if you Google for taking address of temporary array you get quite a number of hits. worth reading through, e.g. https://stackoverflow.com/questions/32941846/c-error-taking-address-of-temporary-array, https://stackoverflow.com/questions/46521343/what-does-error-taking-address-of-temporary-array-mean. You don't say what your environment is. e.g. https://arduino.stackexchange.com/questions/21297/works-with-gcc-not-with-arduino-error-taking-address-of-temporary-array "Works with gcc, not with Arduino".

              K 1 Reply Last reply 16 Apr 2019, 08:59
              0
              • J JonB
                16 Apr 2019, 07:22

                @kshegunov

                I'm asking because I see no arrays

                (char[AV_ERROR_MAX_STRING_SIZE]){0} ?

                @kitfox
                I don't know the answer, but if you Google for taking address of temporary array you get quite a number of hits. worth reading through, e.g. https://stackoverflow.com/questions/32941846/c-error-taking-address-of-temporary-array, https://stackoverflow.com/questions/46521343/what-does-error-taking-address-of-temporary-array-mean. You don't say what your environment is. e.g. https://arduino.stackexchange.com/questions/21297/works-with-gcc-not-with-arduino-error-taking-address-of-temporary-array "Works with gcc, not with Arduino".

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 16 Apr 2019, 08:59 last edited by
                #7

                Don't you hate it when you're asleep while awake ...? :)
                I imagine this is benign, however, as the compiler is obliged to keep that one alive while the function's executing. You're right though, it's a bad practice.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                0

                1/7

                14 Apr 2019, 02:53

                • Login

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