Menu

In a std::unique_ptr<T[]>, the '.get()' type is T[]* instead of T[]

Tal
2024-05-19
2024-05-22
  • Tal

    Tal - 2024-05-19

    According to https://en.cppreference.com/w/cpp/memory/unique_ptr, the pointer type of std::unique_ptr<T[]> is T* and not T[]*. However, I get the following error in cppcheck 2.14.0 (on linux):

     portability (arithOperationsOnVoidPointer): 'buf.get()' is of type 'void *'. When using void pointers in calculations, the behaviour is undefined. Arithmetic operations on 'void *' is a GNU C extension, which defines the 'sizeof(void)' to be 1
    

    For the following type of code:

     std::unique_ptr<char[]> buf = std::make_unique<char[]>(dataSize);
    return buf.get() + size;
    

    Even though that buf.get() is of type char*.

     
  • CHR

    CHR - 2024-05-22

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/12748

     
    • Tal

      Tal - 2024-05-22

      Thanks!

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.