Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The global variable <code>tuple</code> is the only actual <code>Tuple</code> in this program. Memory for global variables is managed by the compiler.</p> <p>In <code>main</code>, <code>Tuple *result</code> is just a pointer which, when you declared it, contains a random number (whatever happened to previously be in the space it now occupies) and thus <code>result</code> points to garbage (not a valid <code>Tuple</code> object).</p> <p>Then you assign the result of <code>findMaxSubArray</code> to <code>tuple</code>. Since <code>findMaxSubArray</code> returns <code>&amp;tuple</code> (in one way or another) which is a global variable, <code>result</code> points to the global variable <code>tuple</code>. So when you do <code>result-&gt;sum</code>, it's the same as doing <code>tuple.sum</code>.</p> <p>In <code>findMaxSubArray</code>, the line <code>Tuple *left, *right, *cross;</code> declares three pointers to <code>Tuple</code>s which contain a garbage value. In one branch of the <code>if</code> you don't use them and just return <code>&amp;tuple</code>, the address of the global variable <code>tuple</code>. In the other branch, you set <code>left</code>, <code>right</code>, and <code>cross</code> to either <code>findMaxCrossingSubArray</code> or <code>findMaxSubArray</code>, which both return <code>&amp;tuple</code> one way or the other.</p> <p>I do suggest reading a book on C++ and forgetting everything you know about C while using C++ (but remember it all again when you program C again). They are not the same language. This code is riddled with things you learned from your C training (such as <code>#define</code> and <code>typedef struct ... Tuple</code>) for which C++ offers better facilities.</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. VO
      singulars
      1. This table or related slice is empty.
    2. 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