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 ?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved C++ Gurus
7 Posts 5 Posters 1.1k Views 2 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.
  • V Offline
    V Offline
    Vinoth Rajendran4
    wrote on 7 Mar 2024, 04:44 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!

    C 1 Reply Last reply 7 Mar 2024, 05:11
    0
    • V Vinoth Rajendran4
      7 Mar 2024, 04:44

      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!

      C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 7 Mar 2024, 05:11 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 7 Mar 2024, 05:42
      1
      • C Christian Ehrlicher
        7 Mar 2024, 05:11

        @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 7 Mar 2024, 05:42 last edited by Vinoth Rajendran4 3 Jul 2024, 05:43
        #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.

        S C 2 Replies Last reply 7 Mar 2024, 06:25
        0
        • V Vinoth Rajendran4
          7 Mar 2024, 05:42

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

          S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 7 Mar 2024, 06:25 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
            7 Mar 2024, 05:42

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

            C Online
            C Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 7 Mar 2024, 06:30 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
            • C Offline
              C Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on 7 Mar 2024, 08:06 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.

              A 1 Reply Last reply 7 Mar 2024, 11:52
              5
              • C Chris Kawa
                7 Mar 2024, 08:06

                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.

                A Offline
                A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on 7 Mar 2024, 11:52 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

                5/7

                7 Mar 2024, 06:30

                • Login

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