title: exception thrown with random numbers in c++
body: <p>I'm trying to make a game which requires pseudo random numbers to set the enemies to random spots. So I tried including the <code>stdlib</code> header file and used <code>srand</code> before getting random numbers for the enemies using <code>rand</code>, my code until the first <code>srand</code> looks like this:</p>
  <pre>#include &quot;pch.h&quot;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;Windows.h&gt;
  #include &lt;time.h&gt;
  
  int screenWidth = 70, screenHeight = 80;
  
  class Enemy {
  public:
      int column, lives;
      Enemy() {};
      Enemy(int nColumn, int nLives) {
          column = nColumn;
          lives = nLives;
      }
  };
  int main()
  {
      wchar_t *screen = new wchar_t[screenWidth * screenHeight];
      HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
      SetConsoleActiveScreenBuffer(hConsole);
      DWORD bytesWritten = 0;
      int x = 0, y = 0, width = 10, height = 10;
      std::wstring canvas = L&quot;&quot;;
      wchar_t *title = new wchar_t[8];
      wsprintf(title, L&quot;retro shooter&quot;);
      SetConsoleTitle(title);
      int enemiesLength = screenWidth / width;
      Enemy* enemies = new Enemy[enemiesLength];
      srand(time(NULL)); // It throws the error at this line
      for (int i = 0; i &lt; enemiesLength; i++) {
          if (rand() % 2) enemies[i] = Enemy(i, 1);
      }
      // the code doesn't end here that's why I didn't put out the closing curly bracket
  </pre>
  <p>The code above gives me the error/exception:</p>
  <blockquote>
  <p>Exception thrown at 0x76EDE496 (ntdll.dll) in retroShooter.exe: 0xC0000005: Access violation reading location 0x183A0FA2.</p>
  </blockquote>
  <p>I have tried using vectors but the exception is the same.
  I have also tried <code>include</code>-ing <code>cstdlib</code> like: <code>#include &lt;cstdlib&gt;</code> but the exception is the exact same.
  This exception is thrown after it has been compiled and it isn't marked as an error in visual studio</p>