Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a C++ object have a fixed size? [closed]

I used to C's fixed size of structure. Don't know if a C++ object is fixed or not. I mean is it always possible to predict what size is for an object? during compiling time? and/or run time? or impossible unless using sizeof operator? I knew it's hardware platform dependent. Beside that, what else?

like image 891
Tim Avatar asked Dec 29 '25 16:12

Tim


1 Answers

A C++ object has a fixed size, of course. If it wasn't fixed, and you made an array of objects, how would the compiler know how much space to allocate?

The same rules that apply to structs apply here, the total from each type of data's size plus any padding for alignment and if you use any virtual functions that's an extra 4 or 8 bytes for the V table pointer depending on the architecture of your machine.

like image 137
Lloyd Crawley Avatar answered Dec 31 '25 08:12

Lloyd Crawley