Does a class occupy memory..where is member function stored. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Does a class occupy memory..where is member function stored.

why is output 1 for class a{}; int main() { a o; cout<<sizeof(o); }

18th Aug 2017, 1:19 PM
Jose Thomas
Jose Thomas - avatar
7 Answers
+ 4
Yes but no. It allocates space in the Stack (where your program is stored, your functions and so on) but not on the Heap (where pointers are stored)
18th Aug 2017, 1:27 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 3
Normal member functions are stored in the .text section of your program. They do not take up extra memory per each instance, because a member function takes in a pointer to the class instance, rather than creating a whole new function for each instance. Declaring the class but not creating an instance does make the .text section slightly bigger (unless the compiler sees that you don't do anything with it and throws it out), so yes it does use up some memory when declaring a class. However, this memory will most likely already be allocated and have extra space. So overall you most likely won't be taking up anymore total memory with the declaration of the class.
18th Aug 2017, 4:20 PM
aklex
aklex - avatar
+ 2
The output is 1 as the class contains nothing but still exist
18th Aug 2017, 1:24 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
so does declaring class allocate space in memory?
18th Aug 2017, 1:26 PM
Jose Thomas
Jose Thomas - avatar
0
but y is the output 1 why not any other or is it Machine dependent?
19th Aug 2017, 5:42 AM
Jose Thomas
Jose Thomas - avatar
0
1 is the number of byte o take in memory
19th Aug 2017, 6:42 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
0
https://www.hackerrank.com/codeskill-1 guys plz participate in codeskill 1. an arena for programmers
1st Feb 2018, 5:17 AM
Jose Thomas
Jose Thomas - avatar