How do variables in C++ store their type?
Clash 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.
c++ variables data-types
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.
add a comment |Â
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.
c++ variables data-types
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.
add a comment |Â
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.
c++ variables data-types
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
c++ variables data-types
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.
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.
add a comment |Â
add a comment |Â
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.
Very comprehensive.
– Deduplicator
41 mins ago
add a comment |Â
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.
Very comprehensive.
– Deduplicator
41 mins ago
add a comment |Â
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.
Very comprehensive.
– Deduplicator
41 mins ago
add a comment |Â
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.
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.
answered 1 hour ago


amon
78.9k19149235
78.9k19149235
Very comprehensive.
– Deduplicator
41 mins ago
add a comment |Â
Very comprehensive.
– Deduplicator
41 mins ago
Very comprehensive.
– Deduplicator
41 mins ago
Very comprehensive.
– Deduplicator
41 mins ago
add a comment |Â
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.
Finn McClusky is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password