When does memory get allocated to a data type, class and function? At the time of declaration or at the time of definition?
Also is there a way to know the sizeof function?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A data type is just an abstract notion understood by the compiler. When a variable of that type is needed, memory must be allocated. The:
amount of memory – which is available at compile time using the
sizeofoperator (it’s not a function) – is determined by the compiler: for inbuilt types it’s fixed based on their binary representation, for user-defined types it’s basically a "recursive" sum of the data member’s individual sizes adjusted for alignment/packing)An actual address may be determined at:
staticvariables) [further details in comments]Classes are just user-defined data types, and follow these same rules, though for classes it’s possible for the size to be unknown if a declaration has been seen by the compiler, but the definition is further into the source code: until the compiler sees the definition it can’t calculate the size of the object, and you can make very limited use of the class identifier – creating pointers and references that will point to an instance, but not actual variables of that type.
Again – it’s an operator which basically injects the compile-time constant size value that the compiler’s calculated. As such, there is no
sizeoffunction in any machine-code object the compiler produces.