Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Good one. Just to be certain, you are wondering why the compiler isn't flagging it as an error right? In that case, I think this problem is unpredictable across compilers but I'm aware of this happening on MSVC all the time.</p> <p><a href="http://support.microsoft.com/kb/98409" rel="nofollow">http://support.microsoft.com/kb/98409</a></p> <p>Let me see if I can explain it like they did. If I were to declare a struct with an empty array like this,</p> <pre><code>struct a { int x; char empty[]; }; </code></pre> <p>the compiler might allocate 4 bytes for x and probably another 4 bytes for the char pointer. empty will contain the address 4 bytes past the start of struct a.</p> <p>Since it is a character array of no length, trying to access it would be an error since there is no trailing 0 to signify the end of the string.</p> <p>I could choose to initialize the struct later to point to the start of an actual string to overcome this error.</p> <pre><code>struct a myStruct = { 1, "hello world"}; // empty now points to the start of "hello world" </code></pre> <p>Since a struct is basically a class, turns out you can do the same thing with a class if you make sure its an aggregate and not a full class.</p> <p>So there ya go. MSVC compilers treat arrays with no fixed sized as a pointer when declared within a struct/class. Remember that class definitions are merely just declarations. The compiler doesn't allocate space for them until you create an instance for it. When you start to think about it, it sorta makes since. How will the compiler know if you plan to allocate storage for it later. It becomes a run-time artifact but the compiler was still smart enough to warn you about the problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload