0

On like 20 I keep getting a memory leak. I'm sure I'm just not seeing what the problem is but was hoping someone with fresh eyes could tell me where I'm going wrong at.

--inside of the work, suppose to take a sentence and after every word with a comma, reverse the word without moving it's place in the sentence.

function bassAckwards($input)
{
  //$inputLen = strlen($input);
  $stringEnd = 0;
  $findme = ',';
  $flag1 = true;
  $tempOffset = 0;
  $string = "";

  //Start loop, as long as flag is true
  while($flag1 == true){
    //find position of next ","
    $pos = strpos($input, $findme, $tempOffset);

    //If $pos is null end loop and return string.
    if($pos == false){
      $flag1 = false;
    }
    else{
      //$pos is not null so create inner loop and add to string
      for($reverse = $pos--; $reverse > $stringEnd; $reverse--){  
        $string = $string . substr($input, $reverse); // < ------  TempStr will add in substr, which will be added to $string  
      }
    }
    $stringEnd = $pos;
    $tempOffset = $pos + 1;
  }
  return $string;
}

print bassAckwards("Php,Cookies,Arrays,Mysql,LAMP");
3
  • On line 17 can you output $pos? Commented Jan 21, 2015 at 2:55
  • 1
    How have you determined that you have a "memory leak"? Commented Jan 21, 2015 at 3:36
  • When I ran the code it popped a memory leak error before crashing basically. Someone pointed out the error which was in the for loop $pos-- Commented Jan 21, 2015 at 3:55

2 Answers 2

1

Your code is very bad in term of performance, try this

function bassAckwards($input)
{
    $findme = ',';
    $input = explode($findme, $input);
    foreach ($input as $i => $word) {
        $input[$i] = strrev($word);
    }
    return implode($findme, $input);
}

print bassAckwards("Php,Cookies,Arrays,Mysql,LAMP");
Sign up to request clarification or add additional context in comments.

3 Comments

I'm still learning php and didn't realize explode did that. Came across it earlier in my studies as I was trying to find a delimiter. Didn't realize that it turned it into an array. But no idea what => $word is actually doing. I don't really see it as a operator and when I find it in the arrays it's looks like a key. Is it basically taking the words and combining them into an actual array to be used?
@Hieu Vo I like this code. Had I seen it before I wrote mine I would not have bothered. I did not know about strrev(). Good to know.
Also I edited though I doubt it would improve it since it's just 1 line and variable. Since I know I'm dealing with Commas I just deleted the $findme and replaced them all with ","
0

Your problem was probably due to this:

for($reverse = $pos--; $reverse > $stringEnd; $reverse--){ 

More precisely this:

$reverse = $pos--;

Even more precisely this:

$pos--;

Causing a recursive loop, causing $string to get larger than the amount of memory.

I would have done something like this, but I'm not sure what you were trying to do. An expample output would have helped.

Code is untested

This if you want to reverse the words.

If you want to reverse every character...<<\br>

Replace

$words = explode(' ',substr($input,$e,$s-$e));

With

$characters = str_split(substr(($input,$e,$s-$e));

Reverse Routine:

$e = 0;
while(true){
  $s = strpos($input, ',', $e);
  if ($s){
    $words = explode(substr($input,$e,$s-$e));
    $words = array_reverse($words); 
    foreach($words as $val){
      $reverseStr .= $val;
    }
  }
  else{
    break;
  }
  $e = $s+1;
}

2 Comments

Thanks, I didn't think that the <code> $pos-- would start a memory leak. I was just assuming it would take the value and subtract it by 1. I fixed it, I still got a ways but yeah hopefully I can get there using my code instead of the 5 lines it would of actually taken to do this.
Typical syntax in a for loop is the first term is a fixed and not dynamic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.