Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Including C header that uses temporary arrays



  • 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?


  • Lifetime Qt Champion

    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.



  • @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.



  • @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. :^)


  • Moderators

    @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 ...



  • @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".


  • Moderators

    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.


Log in to reply