How do variables in C++ store their type?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I'm pretty new to programming in general so sorry for this question as its probably very simple the question is if I define a variable of a certain type (which as far as I know just allocates data for variables content) how does it keep track of type of variable it is.










share|improve this question







New contributor




Finn McClusky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    1
    down vote

    favorite












    I'm pretty new to programming in general so sorry for this question as its probably very simple the question is if I define a variable of a certain type (which as far as I know just allocates data for variables content) how does it keep track of type of variable it is.










    share|improve this question







    New contributor




    Finn McClusky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm pretty new to programming in general so sorry for this question as its probably very simple the question is if I define a variable of a certain type (which as far as I know just allocates data for variables content) how does it keep track of type of variable it is.










      share|improve this question







      New contributor




      Finn McClusky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I'm pretty new to programming in general so sorry for this question as its probably very simple the question is if I define a variable of a certain type (which as far as I know just allocates data for variables content) how does it keep track of type of variable it is.







      c++ variables data-types






      share|improve this question







      New contributor




      Finn McClusky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Finn McClusky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Finn McClusky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 1 hour ago









      Finn McClusky

      111




      111




      New contributor




      Finn McClusky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Finn McClusky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Finn McClusky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          6
          down vote













          Variables (or more generally: “objects” in the sense of C) do not store their type at runtime. As far as machine code is concerned, there is only untyped memory. Instead, the operations on this data interpret the data as a specific type (e.g. as a float or as a pointer). The types are only used by the compiler.



          For example, we might have a struct or class struct Foo int x; float y; ; and a variable Foo f . How can a field access auto result = f.y; be compiled? The compiler knows that f is an object of type Foo and knows the layout of Foo-objects. Depending on platform-specific details, this might be compiled as “Take the pointer to the start of f, add 4 bytes, then load 4 bytes and interpret this data as a float.” In many machine code instruction sets (incl. x86-64) there are different processor instructions for loading floats or ints.



          One example where the C++ type system cannot keep track of the type for us is an union like union Bar int as_int; float as_float; . An union contains up to one object of various types. If we store an object in an union, this is the union's active type. We must only try to get that type back out of the union, anything else would be undefined behavior. Either we “know” while programming what the active type is, or we can create a tagged union where we store a type tag (usually an enum) separately. This is a common technique in C, but because we have to keep the union and the type tag in sync this is fairly error prone. In C++, we can use object-oriented techniques instead.



          There is one case where C++ does store the type of an object: if the class of the object has any virtual methods (a “polymorphic type”, aka. interface). The target of a virtual method call is unknown at compile time and is resolved at run time based on the dynamic type of the object (“dynamic dispatch”). Most compilers implement this by storing a virtual function table (“vtable”) at the start of the object. The vtable can be used to get the type of the object at runtime.



          C++ allows us to inspect the type of an object with the typeid() operator which gives us a std::type_info object. Either the compiler knows the type of the object at compile time, or the compiler has stored the necessary type information inside the object and can retrieve it at runtime.






          share|improve this answer




















          • Very comprehensive.
            – Deduplicator
            41 mins ago










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "131"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );






          Finn McClusky is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f380347%2fhow-do-variables-in-c-store-their-type%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          6
          down vote













          Variables (or more generally: “objects” in the sense of C) do not store their type at runtime. As far as machine code is concerned, there is only untyped memory. Instead, the operations on this data interpret the data as a specific type (e.g. as a float or as a pointer). The types are only used by the compiler.



          For example, we might have a struct or class struct Foo int x; float y; ; and a variable Foo f . How can a field access auto result = f.y; be compiled? The compiler knows that f is an object of type Foo and knows the layout of Foo-objects. Depending on platform-specific details, this might be compiled as “Take the pointer to the start of f, add 4 bytes, then load 4 bytes and interpret this data as a float.” In many machine code instruction sets (incl. x86-64) there are different processor instructions for loading floats or ints.



          One example where the C++ type system cannot keep track of the type for us is an union like union Bar int as_int; float as_float; . An union contains up to one object of various types. If we store an object in an union, this is the union's active type. We must only try to get that type back out of the union, anything else would be undefined behavior. Either we “know” while programming what the active type is, or we can create a tagged union where we store a type tag (usually an enum) separately. This is a common technique in C, but because we have to keep the union and the type tag in sync this is fairly error prone. In C++, we can use object-oriented techniques instead.



          There is one case where C++ does store the type of an object: if the class of the object has any virtual methods (a “polymorphic type”, aka. interface). The target of a virtual method call is unknown at compile time and is resolved at run time based on the dynamic type of the object (“dynamic dispatch”). Most compilers implement this by storing a virtual function table (“vtable”) at the start of the object. The vtable can be used to get the type of the object at runtime.



          C++ allows us to inspect the type of an object with the typeid() operator which gives us a std::type_info object. Either the compiler knows the type of the object at compile time, or the compiler has stored the necessary type information inside the object and can retrieve it at runtime.






          share|improve this answer




















          • Very comprehensive.
            – Deduplicator
            41 mins ago














          up vote
          6
          down vote













          Variables (or more generally: “objects” in the sense of C) do not store their type at runtime. As far as machine code is concerned, there is only untyped memory. Instead, the operations on this data interpret the data as a specific type (e.g. as a float or as a pointer). The types are only used by the compiler.



          For example, we might have a struct or class struct Foo int x; float y; ; and a variable Foo f . How can a field access auto result = f.y; be compiled? The compiler knows that f is an object of type Foo and knows the layout of Foo-objects. Depending on platform-specific details, this might be compiled as “Take the pointer to the start of f, add 4 bytes, then load 4 bytes and interpret this data as a float.” In many machine code instruction sets (incl. x86-64) there are different processor instructions for loading floats or ints.



          One example where the C++ type system cannot keep track of the type for us is an union like union Bar int as_int; float as_float; . An union contains up to one object of various types. If we store an object in an union, this is the union's active type. We must only try to get that type back out of the union, anything else would be undefined behavior. Either we “know” while programming what the active type is, or we can create a tagged union where we store a type tag (usually an enum) separately. This is a common technique in C, but because we have to keep the union and the type tag in sync this is fairly error prone. In C++, we can use object-oriented techniques instead.



          There is one case where C++ does store the type of an object: if the class of the object has any virtual methods (a “polymorphic type”, aka. interface). The target of a virtual method call is unknown at compile time and is resolved at run time based on the dynamic type of the object (“dynamic dispatch”). Most compilers implement this by storing a virtual function table (“vtable”) at the start of the object. The vtable can be used to get the type of the object at runtime.



          C++ allows us to inspect the type of an object with the typeid() operator which gives us a std::type_info object. Either the compiler knows the type of the object at compile time, or the compiler has stored the necessary type information inside the object and can retrieve it at runtime.






          share|improve this answer




















          • Very comprehensive.
            – Deduplicator
            41 mins ago












          up vote
          6
          down vote










          up vote
          6
          down vote









          Variables (or more generally: “objects” in the sense of C) do not store their type at runtime. As far as machine code is concerned, there is only untyped memory. Instead, the operations on this data interpret the data as a specific type (e.g. as a float or as a pointer). The types are only used by the compiler.



          For example, we might have a struct or class struct Foo int x; float y; ; and a variable Foo f . How can a field access auto result = f.y; be compiled? The compiler knows that f is an object of type Foo and knows the layout of Foo-objects. Depending on platform-specific details, this might be compiled as “Take the pointer to the start of f, add 4 bytes, then load 4 bytes and interpret this data as a float.” In many machine code instruction sets (incl. x86-64) there are different processor instructions for loading floats or ints.



          One example where the C++ type system cannot keep track of the type for us is an union like union Bar int as_int; float as_float; . An union contains up to one object of various types. If we store an object in an union, this is the union's active type. We must only try to get that type back out of the union, anything else would be undefined behavior. Either we “know” while programming what the active type is, or we can create a tagged union where we store a type tag (usually an enum) separately. This is a common technique in C, but because we have to keep the union and the type tag in sync this is fairly error prone. In C++, we can use object-oriented techniques instead.



          There is one case where C++ does store the type of an object: if the class of the object has any virtual methods (a “polymorphic type”, aka. interface). The target of a virtual method call is unknown at compile time and is resolved at run time based on the dynamic type of the object (“dynamic dispatch”). Most compilers implement this by storing a virtual function table (“vtable”) at the start of the object. The vtable can be used to get the type of the object at runtime.



          C++ allows us to inspect the type of an object with the typeid() operator which gives us a std::type_info object. Either the compiler knows the type of the object at compile time, or the compiler has stored the necessary type information inside the object and can retrieve it at runtime.






          share|improve this answer












          Variables (or more generally: “objects” in the sense of C) do not store their type at runtime. As far as machine code is concerned, there is only untyped memory. Instead, the operations on this data interpret the data as a specific type (e.g. as a float or as a pointer). The types are only used by the compiler.



          For example, we might have a struct or class struct Foo int x; float y; ; and a variable Foo f . How can a field access auto result = f.y; be compiled? The compiler knows that f is an object of type Foo and knows the layout of Foo-objects. Depending on platform-specific details, this might be compiled as “Take the pointer to the start of f, add 4 bytes, then load 4 bytes and interpret this data as a float.” In many machine code instruction sets (incl. x86-64) there are different processor instructions for loading floats or ints.



          One example where the C++ type system cannot keep track of the type for us is an union like union Bar int as_int; float as_float; . An union contains up to one object of various types. If we store an object in an union, this is the union's active type. We must only try to get that type back out of the union, anything else would be undefined behavior. Either we “know” while programming what the active type is, or we can create a tagged union where we store a type tag (usually an enum) separately. This is a common technique in C, but because we have to keep the union and the type tag in sync this is fairly error prone. In C++, we can use object-oriented techniques instead.



          There is one case where C++ does store the type of an object: if the class of the object has any virtual methods (a “polymorphic type”, aka. interface). The target of a virtual method call is unknown at compile time and is resolved at run time based on the dynamic type of the object (“dynamic dispatch”). Most compilers implement this by storing a virtual function table (“vtable”) at the start of the object. The vtable can be used to get the type of the object at runtime.



          C++ allows us to inspect the type of an object with the typeid() operator which gives us a std::type_info object. Either the compiler knows the type of the object at compile time, or the compiler has stored the necessary type information inside the object and can retrieve it at runtime.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          amon

          78.9k19149235




          78.9k19149235











          • Very comprehensive.
            – Deduplicator
            41 mins ago
















          • Very comprehensive.
            – Deduplicator
            41 mins ago















          Very comprehensive.
          – Deduplicator
          41 mins ago




          Very comprehensive.
          – Deduplicator
          41 mins ago










          Finn McClusky is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          Finn McClusky is a new contributor. Be nice, and check out our Code of Conduct.












          Finn McClusky is a new contributor. Be nice, and check out our Code of Conduct.











          Finn McClusky is a new contributor. Be nice, and check out our Code of Conduct.













           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f380347%2fhow-do-variables-in-c-store-their-type%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          What does second last employer means? [closed]

          List of Gilmore Girls characters

          One-line joke