Jump to content

Simple for() exhausting memory


Mahngiel

Recommended Posts

// in this case, $teams_count = 16
for($j = $tournament->teams_count; $j != 1; $j/2)
{
echo $j;
}

 

What I expect:

16 | 8 | 4 | 2 | 1

 

What I get: memory exhaustion.

PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 128716801 bytes)

 

Is it so late, I'm missing something?

Link to comment
Share on other sites

If you are interested, I solved my own problem by ditching a for() and adding a while()

while($tournament->team_count >= 1)
{
	for($j = 1; $tournament->team_count >= 1;$j++)
	{
		echo '<div id="round_' . $j . '">'; 

		for($i = 1; $i <= $tournament->team_count; $i++)
		{
			echo '<div class="tourney_cell">Player ' . $i . '</div>';
		}

		echo '</div>';

		$tournament->team_count = $tournament->team_count / 2;
	}
}

Link to comment
Share on other sites

That while loop is redundant. It's not the while loop that fixed it, it's the change of $!= to >=

 

Actually, not wholly correct.  The while loop was redundant, but the issue lies in expression 3 and the fact that i never changed the stored value of $j.

 

// original loop
for($j = $tournament->teams_count; $j != 1; $j/2)

// fixed loop
for($j = $tournament->teams_count; $j != 1; $j= $j/2)

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.