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. What is the default value for member variable if not defined ?

What is the default value for member variable if not defined ?

Scheduled Pinned Locked Moved Unsolved C++ Gurus
7 Posts 5 Posters 1.0k Views
  • 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.
  • V Offline
    V Offline
    Vinoth Rajendran4
    wrote on last edited by
    #1

    Hi All,

    I have a member variable which is of bool type and it's default value is not defined in the class. During my application execution, this particular class instance is created dynamically multiple times.

    What i could observe is for all Android version this bool member variable is always getting set to false .
    But for iOS version lower than 16.02 this bool variable is randomly setting the value to either true or false.
    And for iOS version higher than 16.02, this bool variable is always setting to false

    Can some one help me understand why the OS/compiler is setting random values for particular set of OS version alone?

    Is there any documentation which could help me understand this concept ?

    Thanks in advance!

    Christian EhrlicherC 1 Reply Last reply
    0
    • V Vinoth Rajendran4

      Hi All,

      I have a member variable which is of bool type and it's default value is not defined in the class. During my application execution, this particular class instance is created dynamically multiple times.

      What i could observe is for all Android version this bool member variable is always getting set to false .
      But for iOS version lower than 16.02 this bool variable is randomly setting the value to either true or false.
      And for iOS version higher than 16.02, this bool variable is always setting to false

      Can some one help me understand why the OS/compiler is setting random values for particular set of OS version alone?

      Is there any documentation which could help me understand this concept ?

      Thanks in advance!

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Vinoth-Rajendran4 said in What is the default value for member variable if not defined ?:

      Is there any documentation which could help me understand this concept ?

      There is no concept here - an undefined value is... undefined. It can contain anything.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      V 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @Vinoth-Rajendran4 said in What is the default value for member variable if not defined ?:

        Is there any documentation which could help me understand this concept ?

        There is no concept here - an undefined value is... undefined. It can contain anything.

        V Offline
        V Offline
        Vinoth Rajendran4
        wrote on last edited by Vinoth Rajendran4
        #3

        @Christian-Ehrlicher : Thanks for the reply. Yes, since the variable is not initialized, the behavior is undefined as per c++ guidelines.

        But i am curious why the value of that bool member variable is always false in all version of Android and in certain versions of iOS.

        sierdzioS Christian EhrlicherC 2 Replies Last reply
        0
        • V Vinoth Rajendran4

          @Christian-Ehrlicher : Thanks for the reply. Yes, since the variable is not initialized, the behavior is undefined as per c++ guidelines.

          But i am curious why the value of that bool member variable is always false in all version of Android and in certain versions of iOS.

          sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          @Vinoth-Rajendran4 it is curious indeed! An interesting find.

          But, as @Christian-Ehrlicher said - you should ignore this finding and always initialize your variables as soon as possible. Never depend on such platform-related quirks.

          (Z(:^

          1 Reply Last reply
          2
          • V Vinoth Rajendran4

            @Christian-Ehrlicher : Thanks for the reply. Yes, since the variable is not initialized, the behavior is undefined as per c++ guidelines.

            But i am curious why the value of that bool member variable is always false in all version of Android and in certain versions of iOS.

            Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Vinoth-Rajendran4 said in What is the default value for member variable if not defined ?:

            But i am curious why the value of that bool member variable is always false in all version of Android and in certain versions of

            Again: it's undefined. Don't know what else is to say. Noone initialized it for you.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            2
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              It depends on the underlying memory management scheme of the OS. Your app is getting memory in big chunks (pages) and in some circumstances those pages might be zeroed before they're handed over to you. Android apparently does this for stack memory by default and you can also opt-in for heap zeroing.
              In any case, as others said, this is a mechanism outside of the C++ language, and you should definitely not rely on it being there. Keep your app language correct and initialize your variables before use.

              aha_1980A 1 Reply Last reply
              5
              • Chris KawaC Chris Kawa

                It depends on the underlying memory management scheme of the OS. Your app is getting memory in big chunks (pages) and in some circumstances those pages might be zeroed before they're handed over to you. Android apparently does this for stack memory by default and you can also opt-in for heap zeroing.
                In any case, as others said, this is a mechanism outside of the C++ language, and you should definitely not rely on it being there. Keep your app language correct and initialize your variables before use.

                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by
                #7

                And to add to @Chris-Kawa:

                If you use C++11 or higher, you should init the members directly at their declaration: bool m_foo = false;. At least that's what I do.

                Regards

                Qt has to stay free or it will die.

                1 Reply Last reply
                1

                • Login

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