What is the main function of sizeof (I am new to C++). For instance
int k=7;
char t='Z';
What do sizeof (k) or sizeof (int) and sizeof (char) mean?
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.
sizeof(x)returns the amount of memory (in bytes) that the variable or typexoccupies. It has nothing to do with the value of the variable.For example, if you have an array of some arbitrary type
Tthen the distance between elements of that array is exactlysizeof(T).When used on a variable, it is equivalent to using it on the type of that variable:
As a rule-of-thumb, it is best to use the variable name where possible, just in case the type changes:
When used on user-defined types,
sizeofstill returns the amount of memory used by instances of that type, but it’s worth pointing out that this does not necessary equal the sum of its members.While
sizeof(int) + sizeof(char)is typically5, on many machines,sizeof(Foo)may be8because the compiler needs to pad out the structure so that it lies on 4 byte boundaries. This is not always the case, and it’s quite possible that on your machinesizeof(Foo)will be 5, but you can’t depend on it.