title: c++ recursion with object as function parameter
body: <p>I'm trying to find all paths through a given network of activities and thought that I could do so using recursion. It is much like traversing through a network drive with different folders and sub-folders.</p>
  
  <p>I have defined an activity class (for storing each activity's properties) and a network class (for storing the network's activities). I'm passing the network object as a parameter to the recursive function call. The recursive function does not work as expected. It gives a segmentation fault and seems not to be able to access the network object properly.</p>
  
  <p>Can someone point me in the right direction on how to solve this (and how I could also store the found paths in a 2 dimensional array).</p>
  
  <p>Here is the code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  class Activity {
     public:
        int id;
        double duration;
        int successors[10];
  };
  
  class Network {
     public:
        Activity activities[];
  };
  
  int traverse(int, Network);
  
  int main()
  {
      // define the set of successors for each activity
      // a value of -1 indicates that no more successors are present
      const int successorset[][5] = {
          { 1,2,3,-1 },
          { 5,-1 },
          { 4,-1 },
          { 4,-1 },
          { 5,-1 },
          { -1},
          };
  
      // two dimensional array where for each path the activity ids are stored
      int pathset [10][10];
  
      // declare activities
      Activity Activity0, Activity1, Activity2, Activity3, Activity4, Activity5;
  
      // declare network
      Network NetworkMain;
  
      // define activity 0
      Activity0.id = 0;
      Activity0.duration = 0;
      Activity0.successors[0] = successorset[0][0];
      Activity0.successors[1] = successorset[0][1];
      Activity0.successors[2] = successorset[0][2];
      Activity0.successors[3] = successorset[0][3];
      NetworkMain.activities[0] = Activity0;
  
      // define activity 1
      Activity1.id = 1;
      Activity1.duration = 3;
      Activity1.successors[0] = successorset[1][0];
      Activity1.successors[1] = successorset[1][1];
      NetworkMain.activities[1] = Activity1;
  
      // define activity 2
      Activity2.id = 2;
      Activity2.duration = 1;
      Activity2.successors[0] = successorset[2][0];
      Activity2.successors[1] = successorset[2][1];
      NetworkMain.activities[2] = Activity2;
  
      // define activity 3
      Activity3.id = 3;
      Activity3.duration = 2;
      Activity3.successors[0] = successorset[3][0];
      Activity3.successors[1] = successorset[3][1];
      NetworkMain.activities[3] = Activity3;
  
      // define activity 4
      Activity4.id = 4;
      Activity4.duration = 4;
      Activity4.successors[0] = successorset[4][0];
      Activity4.successors[1] = successorset[4][1];
      NetworkMain.activities[4] = Activity4;
  
      // define activity 5
      Activity5.id = 5;
      Activity5.duration = 0;
      Activity5.successors[0] = successorset[5][0];
      NetworkMain.activities[5] = Activity5;
  
      // print info on all activities to check whether they are defined correctly
      for( int a = 0; a &lt; 6; a++ ) {
          cout &lt;&lt; "id of activity = " &lt;&lt; NetworkMain.activities[a].id &lt;&lt; endl;
          cout &lt;&lt; "duration of activity = " &lt;&lt; NetworkMain.activities[a].duration &lt;&lt; endl;
          int s = 0;
          while (NetworkMain.activities[a].successors[s]!=-1) {
              cout &lt;&lt; "successor of activity " &lt;&lt; a &lt;&lt; " = " &lt;&lt; NetworkMain.activities[a].successors[s] &lt;&lt; endl;
              s++;
          }
      }
  
      // call recursive function to traverse through all paths of the network
      traverse(0, NetworkMain);
  
      return 0;
  }
  
  int traverse(int id, Network net)
  {
      if (net.activities[id].successors[0]==-1) // reached finish activity
      {
          cout &lt;&lt; "reached finish " &lt;&lt; endl;
          return 1;
      }
      else
      {
          cout &lt;&lt; "id of activity under investigation " &lt;&lt; net.activities[id].id &lt;&lt; endl;
          int t = 0;
          while (net.activities[id].successors[t]!=-1) {
              cout &lt;&lt; "going to investigate successor " &lt;&lt; t &lt;&lt; endl;
              traverse(net.activities[id].successors[t], net);
              t++;
          }
      }
  }
  </code></pre>
  
  <p>Here is the output:</p>
  
  <pre><code>id of activity = 0
  duration of activity = 0
  successor of activity 0 = 1
  successor of activity 0 = 2
  successor of activity 0 = 3
  id of activity = 1
  duration of activity = 3
  successor of activity 1 = 5
  id of activity = 2
  duration of activity = 1
  successor of activity 2 = 4
  id of activity = 3
  duration of activity = 2
  successor of activity 3 = 4
  id of activity = 4
  duration of activity = 4
  successor of activity 4 = 5
  id of activity = 5
  duration of activity = 0
  id of activity under investigation -578861888
  going to investigate successor 0
  id of activity under investigation 0
  going to investigate successor 0
  id of activity under investigation -581127744
  going to investigate successor 0
  Segmentation fault (core dumped)
  
  Process returned 139 (0x8B)   execution time : 0.087 s
  Press ENTER to continue.
  </code></pre>
  
  <p>Edit: should have added the newbie alert, below is the debugger output.</p>
  
  <pre><code>Active debugger config: GDB/CDB debugger:Default
  Building to ensure sources are up-to-date
  Selecting target: 
  Debug
  Adding source dir: /home/home/Desktop/recursion/
  Adding source dir: /home/home/Desktop/recursion/
  Adding file: /home/home/Desktop/recursion/bin/Debug/recursion
  Changing directory to: /home/home/Desktop/recursion/.
  Set variable: LD_LIBRARY_PATH=.:
  
  [debug]Command-line: /usr/bin/gdb -nx -fullname -quiet  -args /home/home/Desktop/recursion/bin/Debug/recursion
  [debug]Working dir : /home/home/Desktop/recursion
  
  Starting debugger: /usr/bin/gdb -nx -fullname -quiet  -args /home/home/Desktop/recursion/bin/Debug/recursion
  done
  
  [debug]Reading symbols from /home/home/Desktop/recursion/bin/Debug/recursion...done.
  [debug](gdb) 
  [debug]&gt; set prompt &gt;&gt;&gt;&gt;&gt;&gt;cb_gdb:
  
  Registered new type: wxString
  Registered new type: STL String
  Registered new type: STL Vector
  Setting breakpoints
  
  [debug]&gt;&gt;&gt;&gt;&gt;&gt;cb_gdb:
  [debug]&gt; show version
  [debug]GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
  [debug]Copyright (C) 2016 Free Software Foundation, Inc.
  [debug]License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;
  [debug]This is free software: you are free to change and redistribute it.
  [debug]There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
  [debug]and "show warranty" for details.
  [debug]This GDB was configured as "x86_64-linux-gnu".
  [debug]Type "show configuration" for configuration details.
  [debug]For bug reporting instructions, please see:
  [debug]&lt;http://www.gnu.org/software/gdb/bugs/&gt;.
  [debug]Find the GDB manual and other documentation resources online at:
  [debug]&lt;http://www.gnu.org/software/gdb/documentation/&gt;.
  [debug]For help, type "help".
  [debug]Type "apropos word" to search for commands related to "word".
  [debug]&gt;&gt;&gt;&gt;&gt;&gt;cb_gdb:
  [debug]&gt; set confirm off
  
  Debugger name and version: GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
  
  [debug]&gt;&gt;&gt;&gt;&gt;&gt;cb_gdb:
  [debug]&gt; set width 0
  [debug]&gt;&gt;&gt;&gt;&gt;&gt;cb_gdb:
  [debug]&gt; set height 0
  [debug]&gt;&gt;&gt;&gt;&gt;&gt;cb_gdb:
  [debug]&gt; set breakpoint pending on
  [debug]&gt;&gt;&gt;&gt;&gt;&gt;cb_gdb:
  [debug]&gt; set print asm-demangle on
  [debug]&gt;&gt;&gt;&gt;&gt;&gt;cb_gdb:
  [debug]&gt; set unwindonsignal on
  [debug]&gt;&gt;&gt;&gt;&gt;&gt;cb_gdb:
  [debug]&gt; set print elements 0
  [debug]&gt;&gt;&gt;&gt;&gt;&gt;cb_gdb:
  [debug]&gt; set disassembly-flavor intel
  [debug]&gt;&gt;&gt;&gt;&gt;&gt;cb_gdb:
  [debug]&gt; catch throw
  [debug]Catchpoint 1 (throw)
  [debug]&gt;&gt;&gt;&gt;&gt;&gt;cb_gdb:
  [debug]&gt; source /usr/share/codeblocks/scripts/stl-views-1.0.3.gdb
  [debug]&gt;&gt;&gt;&gt;&gt;&gt;cb_gdb:
  [debug]&gt; directory /home/home/Desktop/recursion/
  [debug]Source directories searched: /home/home/Desktop/recursion:$cdir:$cwd
  [debug]&gt;&gt;&gt;&gt;&gt;&gt;cb_gdb:
  [debug]Using terminal's PID as console PID 13148, TTY /dev/pts/1
  [debug]&gt; tty /dev/pts/1
  [debug]Queued:[tty /dev/pts/1]
  [debug]&gt;&gt;&gt;&gt;&gt;&gt;cb_gdb:
  [debug]&gt; run
  [debug]Starting program: /home/home/Desktop/recursion/bin/Debug/recursion 
  [debug]Program received signal SIGSEGV, Segmentation fault.
  [debug]0x0000000000400ee9 in traverse (id=-140179936, net=...) at /home/home/Desktop/recursion/main.cpp:103
  [debug]/home/home/Desktop/recursion/main.cpp:103:2808:beg:0x400ee9
  [debug]&gt;&gt;&gt;&gt;&gt;&gt;cb_gdb:
  
  Program received signal SIGSEGV, Segmentation fault.
  At /home/home/Desktop/recursion/main.cpp:103
  
  [debug]&gt; bt 30
  [debug]#0  0x0000000000400ee9 in traverse (id=-140179936, net=...) at /home/home/Desktop/recursion/main.cpp:103
  [debug]#1  0x0000000000400ff1 in traverse (id=0, net=...) at /home/home/Desktop/recursion/main.cpp:114
  [debug]#2  0x0000000000400ff1 in traverse (id=6, net=...) at /home/home/Desktop/recursion/main.cpp:114
  [debug]#3  0x0000000000400ff1 in traverse (id=0, net=...) at /home/home/Desktop/recursion/main.cpp:114
  [debug]#4  0x0000000000400e91 in main () at /home/home/Desktop/recursion/main.cpp:96
  [debug]&gt;&gt;&gt;&gt;&gt;&gt;cb_gdb:
  </code></pre>