Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is <em>no way</em> to get a char* from a string that is guaranteed to work on all platforms, for the simple fact that <code>string</code> is not required to use contiguous storage.</p> <p>Your safest, most portable course of action is to copy the string somewhere that <em>does</em> use contigious storage (a <code>vector</code> perhaps), and use that instead.</p> <pre><code>vector&lt;char&gt; chars(my_string.begin(), my_string.end()); char* ptr = &amp;chars[0]; </code></pre> <p>If you want to be hacky and non-portable and decidedly unsafe, you can confirm that your <code>string</code> implementation does in fact use contigious storage, and then maybe use this:</p> <pre><code>&amp;my_str[0] </code></pre> <p>But I would punch any developer that worked for me that did this.</p> <h2>EDIT:</h2> <p>I've been made aware that there are currently no known STL implementations that do not store the string data in a contiguous array, which would make <code>&amp;my_str[0]</code> safe. It is also true (and I was asked to state this) that in the upcoming C++0x standard, it will be required for the storage to be contiguous.</p> <p>It's been suggested that because if these facts that my post is factually incorrect.</p> <p>Decide for yourself, but I say no. This is not in the <em>current</em> C++ standard, and so it is not required. I will still in practice do things the way I have suggested, and in any code review I will flag any code that assumes the underlying storage is contigious.</p> <p>Consider this. Suppose there were a question about vtable pointers. Someone wants to examing a class and get the pointer to a virtual function by looking at the vtable. I would immediately tell them not to do this because there is no mention of <em>how</em> virtual methods are implemented in C++. Every implementation I know uses vtables, and I can't think of a better way to do it. It is likely that polymorphism will <em>forever</em> be implemented using vtables. Does that make it ok to examing the vtable directly?</p> <p>IMO no, because this depends on undocumented implementation details. You have no control over this, and it could change at any time. Even if you expect it will never change, it is still bad engineering to rely on these implementation details.</p> <p>Decide for yourself.</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.
    3. VO
      singulars
      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