3
class Test
{
    int x;
};
int main()
{
   cout << sizeof(Test) ;
   return 0;
}

Output : 4
I just want to ask that even i am not created any object of class Test why it prints 4 ?

shuboy2014
  • 1,350
  • 2
  • 18
  • 44
  • 6
    Does your cars fuel tank have 50 liters of fuel in it because ``volume(fuel_tank)`` returns 50? – BitTickler Apr 16 '16 at 14:43
  • I got it bittickler thanks . – shuboy2014 Apr 16 '16 at 14:48
  • Technically, the OS had to load the executable into the memory, so a class might take memory! The keyword here is 'might', because all depends on how the OS handle executable and how the compiler compile the class and what is the architecture of the CPU. – Guillaume Racicot Apr 16 '16 at 15:22
  • @GuillaumeRacicot: I don't understand. How does a class, without methods, that is not instantiated, take up memory space during program execution? Please cite a source or provide an example. – Thomas Matthews Apr 16 '16 at 19:33
  • Well, the OS has to read the binary file that contain the class. The more actual code (in binary) there is, the more memory the operating system has to read from the file to execute the code. The code is usually loaded into memory. You can read an implementation in the linux source code here: https://github.com/torvalds/linux/blob/master/fs/binfmt_elf.c – Guillaume Racicot Apr 16 '16 at 20:41
  • If there is no memory left, the OS can put your binary into the swap too. The only advice I have to give to you: don't worry about this. This is not a large memory usage and the max usage is predictable and small. OSes are pretty good at managing this. So don't bother about executable size, you should focus on performance of your binary instead ;) Still, if you are interested about this, read that: http://stackoverflow.com/questions/8352535/how-does-kernel-get-an-executable-binary-file-running-under-linux – Guillaume Racicot Apr 16 '16 at 20:45
  • Sizeof returns the size in bytes of the object representation of type. It doesn't return the size of the class. It nicely gives back the size of an int (try out `cout << sizeof(int)`). – Edgar Klerks Apr 16 '16 at 21:21

2 Answers2

8

sizeof(X) is the number of bytes an X takes when created. A call to new tends to use a few more bytes for memory use overhead, but an automatic storage (on-stack or local or global or static etc) array of X[N] will take N*sizeof(X) memory in practice (a little extra maybe for function local statics due to thread safety requirements).

It has nothing to do with the amount of memory the type itself takes.

Classes themselves use memory if they have methods that are not optimized away, if they have a vtable (caused by use of the virtual keywords), or similar. Then memory storing code or virtual function tables may exist outside of the memory costs of instances of the class.

Within the C++ language itself, there is no way to determine how much memory the class itself takes, nor no reliable way to determine what the new overhead is. You can usually puzzle that out by looking at the runtime behaviour, or the code for the compiler or runtime libraries, for a given platform.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • In general a `class` or `struct` is a concept and does not occupy variable (data) space, but it does take up memory in the compiler's memory. If a `class` or `struct` has methods, those methods will take up *code* space, if the methods are executed (linkers tend to drop functions that are not used). A variable or instance of a `class` or `struct` takes up memory. – Thomas Matthews Apr 16 '16 at 19:30
  • @thom are you agreeing or disagreeing? Few systems distinguish between code and data memory (some systems write code to ROM or the like). I guess some could encode vtables there as well. But most modern computers, memory is memory with at most some protection/execution bits set in the page table. My point is that there are a few ways in which a class can "take up memory"; in practice, this can even be a priblem, because I can write code that generates an obscene amount of types from a very short program. – Yakk - Adam Nevraumont Apr 16 '16 at 19:40
  • Don't forget to cite static member variables which do occupy memory space ;) – YSC Aug 16 '17 at 12:01
0

A class or or a struct is basically a kind of datatype (not exactly the datatype), so a data type will occupy memory only when a variable of its type is created. So a class will occupy space when it is instantiated. If a class has a static member variable it will occupy space even if there is no instantiation.

user16217248
  • 3,119
  • 19
  • 19
  • 37