Suppose I have the following Foo class. The size of Foo's member variables are what I would expect. However, the size of Foo it'self is 16. Where are the extra four bytes coming from?  #include &lt;iostream&gt; #include &lt;string&gt;  using namespace std;  class Foo { public:         int *data;         int length; };  int main() {         Foo f;         cout &lt;&lt; "Size of foo's data: " &lt;&lt; sizeof(f.data) &lt;&lt; endl;         cout &lt;&lt; "Size of foo's length: " &lt;&lt; sizeof(f.length) &lt;&lt; endl;         cout &lt;&lt; "Size of foo: " &lt;&lt; sizeof(f) &lt;&lt; endl;          return 0; }   Output:          Size of foo's data: 8          Size of foo's length: 4          Size of foo: 16
1450
Why does the sizeof operator return a size larger for a structure than the total sizes of the structure's members?
40546
In C/C++, what an unsigned char is used for? How is it different from a regular char?
19865
Can someone explain what this means?  int (*data[2])[2];
12769
Recently, I noticed some people mentioning that std::list::size() has a linear complexity. According to  , this is in fact implementation dependent as the standard doesn't say what the complexity has to be. The comment  says:     Actually, it depends on which STL you   are using. Microsoft Visual Studio V6   implements size() as {return (_Size);   } whereas gcc (at least in versions   3.3.2 and 4.1.0) do it as { return std::distance(begin(), end()); } The   first has constant speed, the second   has o(N) speed    So my guess is that for the VC++ crowd size() has constant complexity as Dinkumware probably won't have changed that fact since VC6. Am I right there? What does it look like currently in gcc? If it is really O(n), why did the developers choose to do so?
35378
For example:  int a = 12; cout &lt;&lt; typeof(a) &lt;&lt; endl;   Expected output:  int
12125
This question was .  Now I'd like to learn the differences between a struct and a class in C++. Please discuss the technical differences as well as reasons for choosing one or the other in OO design.  I'll start with an obvious difference:   If you don't specify public: or private:, members of a struct are public by default; members of a class are private by default.   I'm sure there are other differences to be found in the obscure corners of the C++ specification.
56384
When declaring a template, I am used to having this kind of code:  template &lt;class T&gt;   But , they used:  template &lt;unsigned int N&gt;   I checked that it compiles. But what does it mean? Is it a non-type parameter? And if so, how can we have a template without any type parameter?
43917
I am looking for details of the VTable structure, order and contents, and the location of the vtable pointers within objects.   Ideally, this will cover single inheritance, multiple inheritance, and virtual inheritance.  References to external documentation would also be appreciated  Documentation of GCC 4.0x class layout is  and the Itanium, and more broadly GNU, ABI layout documents are .
37713
I am trying to create a MATLAB class with a member variable that's being updated as a result of a method invocation, but when I try to change the property within the class it (apperently, from what I understood from MATLAB's memory management) creates a copy of the object and then modifies it, leaving the original object's property untouched.  classdef testprop       properties           numRequests=0;       end       methods           function Request(this, val)               disp(val);               this.numRequests=this.numRequests+1;           end       end   end     .  &gt;&gt; a=testprop; &gt;&gt; a.Request(9); &gt;&gt; a.Request(5);   &gt;&gt; a.numRequests    ans = 0
14927
Disclaimer Yes, I am fully aware that what I am asking about is totally stupid and that anyone who would wish to try such a thing in production code should be fired and/or shot.  I'm mainly looking to see if can be done. Now that that's out of the way, is there any way to access private class members in C++ from outside the class?  For example, is there any way to do this with pointer offsets? (Naive and otherwise non-production-ready techniques welcome) Update As noted in the comments, I asked this question because I wanted to write a blog post on over-encapsulation (and how it affects TDD).  I wanted to see if there was a way to say &quot;using private variables isn't a 100% reliable way to enforce encapsulation, even in C++.&quot;  At the end, I decided to focus more on how to solve the problem rather than why it's a problem, so I didn't feature some of the stuff brought up here as prominently as I had planned, but I still left a link. At any rate, if anyone's interested in how it came out, here it is:   (I suggest reading it before you decide that I'm crazy).
72809
When you create an instance of a class with the new operator, memory gets allocated on the heap. When you create an instance of a struct with the new operator where does the memory get allocated, on the heap or on the stack ?
122420
I get a transmission over the network that's an array of chars/bytes. It contains a header and some data. I'd like to map the header onto a struct. Here's an example:  #pragma pack(1)  struct Header {     unsigned short bodyLength;     int msgID;     unsigned short someOtherValue;     unsigned short protocolVersion; };  int main() {     boost::array&lt;char, 128&gt; msgBuffer;     Header header;      for(int x = 0; x &lt; sizeof(Header); x++)         msgBuffer[x] = 0x01; // assign some values      memcpy(&amp;header, msgBuffer.data(), sizeof(Header));      system("PAUSE");          return 0; }   Will this always work assuming the structure never contains any variable length fields? Is there a platform independent / idiomatic way of doing this?  Note:  I have seen quite a few libraries on the internet that let you serialize/deserialize, but I get the impression that they can only deserialize something if it has ben previously serialized with the same library. Well, I have no control over the format of the transmission. I'm definitely going to get a byte/char array where all the values just follow upon each other.
42535
If I have a class as follows     class Example_Class     {        private:          int x;           int y;         public:           Example_Class()           {               x = 8;              y = 9;          }        ~Example_Class()         { }     };   And a struct as follows  struct {    int x;    int y; } example_struct;   Is the structure in memory of the  example_struct simmilar to that in Example_Class  for example if I do the following  struct example_struct foo_struct; Example_Class foo_class = Example_Class();  memcpy(&amp;foo_struct, &amp;foo_class, sizeof(foo_struct));   will foo_struct.x = 8 and foo_struct.y = 9 (ie: the same values as the x,y values in the foo_class) ?  The reason I ask is I have a C++ library (don't want to change it) that is sharing an object with C code and I want to use a struct to represent the object coming from the C++ library. I'm only interested in the attributes of the object.  I know the ideal situation would be to have Example_class wrap arround a common structure between the C and C++ code but it is not going to be easy to change the C++ library in use.
100259
Is std::string size() a O(1) operation?  The implementation of STL I'm using is the one built into VC++
67310
While googling, I see that using  can be slow.  has a  method that is available as well.  Is there an efficient way in java to get the file size?
38591
class someclass {};  class base {     int a;     int *pint;     someclass objsomeclass;     someclass* psomeclass; public:     base()     {         objsomeclass = someclass();         psomeclass = new someclass();         pint = new int();          throw "constructor failed";         a = 43;     } }  int main() {     base temp(); }   In the above code, the constructor throws. Which objects will be leaked, and how can the memory leaks be avoided?  int main() {     base *temp = new base(); }   How about in the above code? How can the memory leaks be avoided after the constructor throws?
12557
What are the benefits of having a member variable declared as read only?  Is it just protecting against someone changing its value during the lifecycle of the class or does using this keyword result in any speed or efficiency improvements?