Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>when they are represented in memory are objects C++ objects the same as C structs.</p> </blockquote> <p>Strictly speaking, no. In general, yes. C++ classes and structs are identical in memory layout to C structs except:</p> <ul> <li>Bit fields have different packing rules</li> <li>Sizes are fixed at compile time</li> <li>If there are any virtual functions, the compiler will add a vtable entry to the memory layout.</li> <li>If the object inherits a base class, the new class' layout will be appended to the base class layout, including vtable, if any.</li> </ul> <blockquote> <p>I don't think C++ allows overloading the + operator for the built in char * type. so i'd like to create my own lightweight string class which has no extra overhead std::string has. I think std::string is represented contiguously</p> </blockquote> <p>You can create a <code>operator+</code> overload for the <code>char*</code> type. Normal behavior is pointer arithmetic. <code>std::string</code> overloads <code>operator+</code> to append <code>char*</code> data to the string. The string is stored in memory as a C string, plus additional information. The <code>c_str()</code> member function returns a pointer to the internal <code>char</code> array.</p> <p>In your C example, you're relying on undefined behavior. Don't <code>realloc</code> like that. It can result in Bad Things - namely bizarre segfaults.</p> <p>Your C++ example is also doing Bad Things in doing <code>realloc(this)</code>. Instead, you should carry a <code>char*</code> and get a <code>new char[]</code> buffer to store the chars in instead of a <code>realloc()</code>. Behavior for such a <code>realloc</code> is undefined.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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