Note that there are some explanatory texts on larger screens.

plurals
  1. POIs a C-Style array to std::array transition completely safe for arrays?
    primarykey
    data
    text
    <p>First time questioner :) Is it possible to transform global c-style arrays to std::arrays without breaking the code? I'm working on a project which consists of decompiling the source code of an old game. We have already managed to refactor a large part of the disassembly/decompilation output. Since it's automatic there are still sections like</p> <pre><code> int a; int b[50]; *(&amp;a + 100) = xxx; </code></pre> <p>or</p> <pre><code> int b[50]; int a; *(&amp;a - 100) = xxx; </code></pre> <p>and other types of crazy pointer arithmetics remaining, which have yet to be refactored manually. But we would like to use bounds checking for sections that have been (presumably) correctly changed to arrays.</p> <p>(<strong>Ignore the text in italics, I'm keeping it just for consistency in the comments</strong>)<em>I've found one problem so far with chaning every array: <code>sizeof(class containing array)</code> would change. This could break code in some cycles, for example someclass somearray[100]; //for example (sizeof(somearray[0]) == 50) is true int pointer = (int)somearray; pointer += 100 ((someclass</em>)pointer)->doSomething(); .<em>because <code>pointer +=100</code> wouldn't be pointing to the second element, but somewhere inside the first, or even zeroth, I'm not sure (don't forget it's automatically decompiled code, hence the ugliness).</em></p> <p>I'm thinking of changing every global array to std::array and every instance of accessing the array without the <code>[]</code> operator to <code>array._Elems</code>.</p> <p>Are there any problems that might arise if I were to change global arrays to std::arrays in code such as this?</p> <p><strong>Edit</strong> You were right about the size not changing. I had an error in the testing functions. So I'll expand the question:</p> <p>Is it safe to change every c-style array to std::array?</p> <p><strong>Edit</strong> Our current code is actually only runnable in debug mode, since it doesn't move variables around. Release mode crashes basically at the start of the program.</p> <p><strong>Edit</strong> Since there seems to be some confusion what this question is about, let me clarify: Is there some guarantee that there's no other member in the array, other than T elems [N] ? Can I count on having </p> <pre><code>array&lt;array&lt;int,10&gt;, 10&gt; varname; int* ptr = &amp;varname[0][0]; ptr += 10 </code></pre> <p>and be sure that ptr is pointing at <code>varname[1][0]</code> regardless of implementation details? Although it's guaranteed that an array is contiguous, I'm not sure about this. The standard contains an implementation, but I'm not sure whether that's an example implementation or the actual definition which every implementation should adhere with iterator and const_iterator being the only things that are implementation specific, since only those have the words <em>implementation-defined</em> (I don't have the latest specifiation at hand, so there might be some other differences).</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.
 

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