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. Violation of MISRA08_18-4-1

Violation of MISRA08_18-4-1

Scheduled Pinned Locked Moved Solved C++ Gurus
6 Posts 3 Posters 1.4k Views 1 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.
  • P Offline
    P Offline
    Phadnis
    wrote on last edited by
    #1

    Hi,
    In the header file I have declared a address variable as
    Animal *m_lion;
    and in implementation file I have written this
    m_lion = new Lion;
    where Animal is the base class with some virtual functions and Lion is its derived class.
    I am deallocating the memory in the destructor of the class in which i have declared and allocated the memory for the address variable

    If I run the static code analyser with MISRA2008,then I am getting error as Violation of MISRA08_18-4-1

    Looking forward for help,
    with regards

    jsulmJ 1 Reply Last reply
    0
    • P Phadnis

      Hi,
      In the header file I have declared a address variable as
      Animal *m_lion;
      and in implementation file I have written this
      m_lion = new Lion;
      where Animal is the base class with some virtual functions and Lion is its derived class.
      I am deallocating the memory in the destructor of the class in which i have declared and allocated the memory for the address variable

      If I run the static code analyser with MISRA2008,then I am getting error as Violation of MISRA08_18-4-1

      Looking forward for help,
      with regards

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @Phadnis said in Violation of MISRA08_18-4-1:

      Violation of MISRA08_18-4-1

      Is there more than that? Some descriptive text?
      As far as I know MISRA disallows any kind of dynamic memory allocation (malloc/new).

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #3

        @Phadnis said in Violation of MISRA08_18-4-1:

        MISRA08_18-4-1

        As far as I know MISRA disallows any kind of dynamic memory allocation (malloc/new).

        <This Link> verifies your suspicion.

        If you meet the AI on the road, kill it.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Phadnis
          wrote on last edited by
          #4

          Rationale
          The use of dynamic memory can lead to out-of-storage run-time failures, which are undesirable. The built-in new and delete operators, other than the placement versions, use dynamic heap memory. The functions calloc, malloc, realloc and free also use dynamic heap memory. There is a range of unspecified, undefined and implementation-defined behaviour associated with dynamic memory allocation, as well as a number of other potential pitfalls. Dynamic heap memory allocation may lead to memory leaks, data inconsistency, memory exhaustion, non-deterministic behaviour, etc.
          Note that some implementations may use dynamic heap memory allocation to implement other functions (for example, functions in the library cstring). If this is the case, then these functions shall also be avoided.
          Example
          void f1 ( )
          {
          int32_t * i = new int32_t; // Non-compliant
          delete i;
          }

          • Below is the code

          User::User()
          {
          m_lion = new Lion;
          }

          User::~User()
          {
          delete m_lion;
          }

          1 Reply Last reply
          0
          • Kent-DorfmanK Offline
            Kent-DorfmanK Offline
            Kent-Dorfman
            wrote on last edited by
            #5

            @Phadnis said in Violation of MISRA08_18-4-1:

            Rationale

            If you have drank the cool-aid and don't intend to allow free-store allocation of objects then you cannot use Qt. It's really that simple. No mention of whether the end app is for an embedded platform, but I'd suspect it's more or a training or academic exercise for you. You are buying into a MISRA religious dogma without having a greater understanding of the context in which the rules are important. They don't apply across the board, but only for specialized cases.

            If you meet the AI on the road, kill it.

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

              @Phadnis said in Violation of MISRA08_18-4-1:

              Rationale

              If you have drank the cool-aid and don't intend to allow free-store allocation of objects then you cannot use Qt. It's really that simple. No mention of whether the end app is for an embedded platform, but I'd suspect it's more or a training or academic exercise for you. You are buying into a MISRA religious dogma without having a greater understanding of the context in which the rules are important. They don't apply across the board, but only for specialized cases.

              P Offline
              P Offline
              Phadnis
              wrote on last edited by
              #6

              @Kent-Dorfman Thanks I agree.

              1 Reply Last reply
              0

              • Login

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