# Thread: LZ77 Match Searching (HELP!)

1. ## LZ77 Match Searching (HELP!)

I've learned LZ77 in theory, know I wanna practice, my first attempt was to code a C++ function that finds the best match when comparing the sliding window with the look ahead buffer, this is what i came up with: (btw it must return a array of int, 0->contains distance to run backwards from the lookahead buffer 1->contains how long the match is, it cant be less then 2)

Code:
```#include <iostream>
#include <string>

using namespace std;

int * find_match(char * sliding_window, char * look_ahead)
{
int distance = 0;
int lenght = 0;

// for loop runs every character pointed in var sliding_window
// it searches for a character match longer than 2
for(int x = 0; *(sliding_window+x) != '\0'; x++)
{
{
//if reached here, then it means a match longer than 2 bytes has been found

// distance = ???? dont know how to calculate distance

lenght++; //previously 2, but 2 has already been checked if it matches, and it does
lenght++;

}

if(lenght > 0) // if it's bigger than 0, then it means it's already found a match
break;
}

if(distance != 0 && lenght != 0)
{
int return_array[] = {distance,lenght};
return return_array;
}
else
{
return 0;
}
}

int main()
{
char * str1 = "Just testing my function.";
char * str2 = "I'm just testing.";
int lenght_distance[] = find_match(str1, str2);
cout << "distance: " << lenght_distance[0];
return 0;
}```
Somebody help me at making this function work, it's awful so far. And I can't call it on main without bumping into error report.

2. No such thing as "returning an array". Best you can do is to return an int*, as you have done; but then, you must make sure your memory lasts. Any variable that you declare just as a variable will die the instant the function ends, meaning that your pointer is now pointing to nothing in particular. If for whatever reason you cannot implement the actual solution of passing the array into the function, then you need to acquire memory that will last longer than the function by using "new".