[Libav-user] g++ 4.7.2 fails to compile av_err2str

Alex Cohn alexcohn at netvision.net.il
Wed Jan 16 09:45:06 CET 2013


On Mon, Jan 14, 2013 at 9:41 PM, "René J.V. Bertin" <rjvbertin at gmail.com> wrote:
> On Jan 14, 2013, at 19:14, Nicolas George wrote:
>>
>> I checked both in the man page (from gcc+glibc) and a test program: if you
>> use alloca() inside a loop, it returns a new block each time, while a
>> compound literal has always the same address.

The difference is that compound literal is resolved at compile time,
so using it inside a loop does not produce extra instances.

> Seems like this would speak for using a local std::string instance in C++ code where a compound literal cannot be used.

Here is my C++ way of defining av_err2str:

    #ifdef  __cplusplus

    static const std::string av_make_error_string(int errnum)
    {
        char errbuf[AV_ERROR_MAX_STRING_SIZE];
        av_strerror(errnum, errbuf, AV_ERROR_MAX_STRING_SIZE);
        return (std::string)errbuf;
    }

    #undef av_err2str
    #define av_err2str(errnum) av_make_error_string(errnum).c_str()

    #endif // __cplusplus

... and here are some usage examples:

    puts(av_err2str(1));
    std::cout << av_make_error_string(2) << std::endl;
    std::cout << av_err2str(3) << std::endl;

BR,
Alex Cohn


More information about the Libav-user mailing list