malloc.c:2372: sysmalloc: Assertion - c++

/* Dynamic Programming implementation of LCS problem */
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<set>
using namespace std;
/* Returns length of LCS for X[0..m-1], Y[0..n-1] */
int** lcs( char *X, char *Y, int m,int n)
{
int **L;
L = new int*[m];
/* Following steps build L[m+1][n+1] in bottom up fashion. Note
that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] */
for (int i=0; i<=m; i++)
{
L[i] = new int[n];
for (int j=0; j<=n; j++)
{
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X[i-1] == Y[j-1])
L[i][j] = L[i-1][j-1] + 1;
else
L[i][j] = max(L[i-1][j], L[i][j-1]);
}
}
return L;
}
void printlcs(char *X, char *Y,int m,int n,int *L[],string str)
{
if(n==0 || m==0)
{ cout<<str<<endl;
return ;
}
if(X[m-1]==Y[n-1])
{ str= str + X[m-1];
//cout<<X[m-1];
m--;
n--;
printlcs(X,Y,m,n,L,str);
}else if(L[m-1][n]==L[m][n-1]){
string str1=str;
printlcs(X,Y,m-1,n,L,str);
printlcs(X,Y,m,n-1,L,str1);
}
else if(L[m-1][n]<L[m][n-1])
{
n--;
printlcs(X,Y,m,n,L,str);
}
else
{
m--;
printlcs(X,Y,m,n,L,str);
}
}
/* Driver program to test above function */
int main()
{
char X[] = "afbecd";
char Y[] = "fabced";
int m = strlen(X);
int n = strlen(Y);
int **L;
L=lcs(X, Y,m,n);
string str="";
printlcs(X,Y,m,n,(int **)L,str);
return 0;
}
This is the program for print all possible longest common sub-sequences. If we give input char X[] = "afbecd";char Y[] = "fabced"; then it was showing following error, while for input char X[] = "afbec";char Y[] = "fabce" it is working fine.
solution: malloc.c:2372: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed.
Can please anyone figure out why this strange behaviour is occuring. Thanks

In lcs function you have out of array bounds during iteration over L array in for loop. L is array of length m:
int **L;
L = new int*[m];
in this loop:
for (int i=0; i<=m; i++)
{
L[i] = new int[n];
you access L[m] element when i == m. It's Undefined Behavior, as arrays indexed from 0 and this is access to the m + 1 element.
Same problem is in the next loop during access to n + 1 element in L[i] array of length n:
for (int j=0; j<=n; j++)
{
// Code skipped
L[i][j] = 0;

Related

I have a buffer overflow problem while writing mergesort() in C++

The prompt question is, the size of the help array that can be written is (R-L+1)*'4' bytes, but '8' bytes may be written, what does this mean?Is the array out of bounds, but I think it is logically correct, the specific code is as follows:
void merge(int a[], int L, int M, int R) {
int* help = new int[R - L + 1];
int i = 0;
int p = L;
int q = M + 1;
while (p <= M && q <= R) {
help[i++] = a[p] <= a[q] ? a[p++] : a[q++];
}
while (p <= M) {
help[i++] = a[i++];
}
while (q <= R) {
help[i++] = a[i++];
}
for (i = 0; i < R - L + 1; i++) {
a[L + i] = help[i];
}
}

Print a matrix of alternating X's and O's given column and row constraints?

I'm trying to write an algorithm that prints a matrix of X's and O's, given the following params:
int numRows
int numCols
int charsPerCol
int charsPerRow
e.g. calling
printXOMatrix(int charsPerCol, int charsPerRow, int numCols, int numRows);
with the parameters
printXOMatrix(3,2,15,8);
will result in the following being printed to stdout:
XXXOOOXXXOOOXXX
XXXOOOXXXOOOXXX
OOOXXXOOOXXXOOO
OOOXXXOOOXXXOOO
XXXOOOXXXOOOXXX
XXXOOOXXXOOOXXX
OOOXXXOOOXXXOOO
OOOXXXOOOXXXOOO
Here's my code so far, it seems to print correctly if the number of columns / the number of chars per column is different, but fails for example in the case of:
printXOMatrix(2,2,8,8);
the following is printed to stdout:
XXOOXXOO
OOXXOOXX
OOXXOOXX
XXOOXXOO
XXOOXXOO
OOXXOOXX
OOXXOOXX
XXOOXXOO
How do I handle this edge case/clean up my code? Here's what I have so far:
#include <stdio.h>
void getXAndOGrid(int charsPerCol, int charsPerRow, int numCols, int numRows) {
char c = 'X';
for (int i=1; i<=(numCols*numRows); i++) {
// if current index is divisible by the columns (new row)
if (i % numCols == 0) {
// print character, then newline
printf("%c\n", c);
// if current index is divisible by number of columns times num of chars in column
if (i % (numCols * charsPerRow) == 0) {
if (c == 'O') {
c = 'X';
} else {
c = 'O';
}
}
// else if current index is divisible by num in row before it alternates
// and is not divisible by number of columns
} else if (i % charsPerCol == 0) {
printf("%c", c);
if (c == 'O') {
c = 'X';
} else {
c = 'O';
}
} else {
printf("%c", c);
}
}
}
int main() {
getXAndOGrid(3,2,15,8);
return 0;
}
Well, after figuring out the code had an inconsistancy as selbie said, it seems the problem is when you reach the end of the line you don't reset c back to what it was at the start of the line!
But the question should be: why are you writing so much code? The following does exactly the same (well, apart from handling the edge case correctly!):-
void PrintMatrix (int charsPerCol, int charsPerRow, int numCols, int numRows)
{
for (int y = 0 ; y < numRows ; ++y)
{
for (int x = 0 ; x < numCols ; ++x)
{
printf ((((x / charsPerCol) & 1) ^ ((y / charsPerRow) & 1)) != 0 ? "o" : "x");
}
printf ("\n");
}
}
The problem is that you missed one case. You handled the case that the table switch up, with the condition: if (i % (numCols * charsPerRow) == 0), but you haven't handled the case when it doesn't . So I add another condition:
if (i % (numCols * charsPerRow) == 0)
{
if ((numCols / charsPerCol)%2 == 1)
{
if (c == 'O')
{
c = 'X';
}
else
{
c = 'O';
}
}
}
else
{
if ((numCols / charsPerCol)%2 == 0)
{
if (c == 'O')
{
c = 'X';
}
else
{
c = 'O';
}
}
}
So, if the (numCols / charsPerCol) is odd, for instance XXOOXXOOXX, no switch from X <-> O needed, otherwise yes (for instance XXOOXXOO, now c need to be switched from O -> X).
The opposite holds for when the initial condition i % (numCols * charsPerRow) == 0 is reached.
Full code:
#include <iostream>
using namespace std;
void getXAndOGrid(int charsPerCol, int charsPerRow, int numCols, int numRows) {
char c = 'X';
for (int i=1; i<=(numCols*numRows); i++) {
// if current index is divisible by the columns (new row)
if (i % numCols == 0) {
// print character, then newline
printf("%c\n", c);
// if current index is divisible by number of columns times num of chars in column
if (i % (numCols * charsPerRow) == 0) {
if ((numCols / charsPerCol)%2 == 1)
{
if (c == 'O') {
c = 'X';
} else {
c = 'O';
}
}
}
else
{
//cerr << c << endl;
if ((numCols / charsPerCol)%2 == 0)
{
if (c == 'O') {
c = 'X';
} else {
c = 'O';
}
}
}
// else if current index is divisible by num in row before it alternates
// and is not divisible by number of columns
}
else if (i % charsPerCol == 0) {
printf("%c", c);
if (c == 'O') {
c = 'X';
} else {
c = 'O';
}
}
else {
printf("%c", c);
}
}
}
int main() {
getXAndOGrid(2,2,8,8);
return 0;
}
Output (your case):
XXOOXXOO
XXOOXXOO
OOXXOOXX
OOXXOOXX
XXOOXXOO
XXOOXXOO
OOXXOOXX
OOXXOOXX
Output (with getXAndOGrid(3,3,15,15);):
XXXOOOXXXOOOXXX
XXXOOOXXXOOOXXX
XXXOOOXXXOOOXXX
OOOXXXOOOXXXOOO
OOOXXXOOOXXXOOO
OOOXXXOOOXXXOOO
XXXOOOXXXOOOXXX
XXXOOOXXXOOOXXX
XXXOOOXXXOOOXXX
OOOXXXOOOXXXOOO
OOOXXXOOOXXXOOO
OOOXXXOOOXXXOOO
XXXOOOXXXOOOXXX
XXXOOOXXXOOOXXX
XXXOOOXXXOOOXXX
P.S : IMO, your code has quite a bit of duplicated code, wouldn't it be cleaner if a function is implemented?
void switchC(char &c)
{
if (c == 'O') { c = 'X';} else {c = 'O';}
}
void getXAndOGrid(int charsPerCol, int charsPerRow, int numCols, int numRows) {
char c = 'X';
for (int i=1; i<=(numCols*numRows); i++) {
if (i % numCols == 0) {
printf("%c\n", c);
if (i % (numCols * charsPerRow) == 0) {
if ((numCols / charsPerCol)%2 == 1) {switchC(c);}
}
else {
if ((numCols / charsPerCol)%2 == 0) {switchC(c);}
}
}
else if (i % charsPerCol == 0) { printf("%c", c); switchC(c);}
else { printf("%c", c);}
}
}
P.P.S : #Skizz and #selbie has compacted it even more.
There's a lot of complexity in your code because you are printing character by character, using for-loop index values initialized to 1 instead of 0, and trying to print the entire thing in a single loop.
Here's a cleaner approach. There are two unique lines to print. The one starting with X and the one starting with O. Have one for-loop to build each line type. Then another loop to print each line.
What do you think of this? The code below uses C++, but if you need pure C, replace the new/delete calls with malloc and free as seen in the comments:
void printXOMatrix(int numRows, int numCols, int charsPerCol, int charsPerRow)
{
char* primary = new char[numCols + 1]; // malloc(sizeof(char)*(numcols+1))
char* opposite = new char[numCols + 1]; // malloc(sizeof(char)*(numcols+1))
for (int col = 0; col < numCols; col++)
{
char ch = (col % (charsPerCol * 2) < charsPerCol) ? 'X' : 'O';
primary[col] = ch;
opposite[col] = (ch == 'X') ? 'O' : 'X';
}
primary[numCols] = '\0';
opposite[numCols] = '\0';
for (int row = 0; row < numRows; row++)
{
char* line = (row % (charsPerRow * 2) < charsPerRow) ? primary : opposite;
std::cout << line << std::endl; // printf("%s\n", line);
}
delete[] primary; // free(primary)
delete[] opposite; // free(opposite)
}

malloc.c:2451: sYSMALLOc: Assertion ... failed

I cannot for the life of me figure out what's going on. Here's the error I get:
alloc static vecs
a.out: malloc.c:2451: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed. Aborted (core dumped)
The error occurs in the function Halton in class qmc, which I've included the relevant bits to below. As you can see, the first print statement "alloc static vecs" executes, but the statement std::vector<double> H(s); appears not to, since the print statement immediately following it does not execute.
Now, I should mention that when I replace the statement static std::vector<int> bases = FirstPrimes(s); in Halton with static std::vector<int> bases = {2,3,5,7,11,13}; (the RHS is the return array of FirstPrimes(), just hardcoded) then there is no error.
There are more functions in Halton (it returns a std::vector) but I've omitted them for brevity. I'll add them if anyone wants to try to run it themselves, just ask!
I'm using g++ 4.6 and Ubuntu 12.04, and the compilation command is g++ -std=c++0x scratch.cpp QMC.cpp.
main (scratch.cpp):
#include <iostream>
#include <vector>
#include "QMC.h"
int main() {
QMC qmc;
std::vector<double> halton = qmc.Halton(6,1);
}
QMC.h:
#ifndef QMC_H
#define QMC_H
#include <iostream>
#include <cmath>
#include <vector>
class QMC {
public:
QMC();
bool isPrime(int n);
std::vector<int> ChangeBase(int n, int radix);
std::vector<int> NextChangeBase(std::vector<int>& a_in, int radix);
double RadicalInverse(std::vector<int>& a, int b);
std::vector<int> FirstPrimes(int n);
std::vector<double> Halton(int s, int n = 0);
};
#endif
QMC.cpp:
#include "QMC.h"
QMC::QMC(){}
std::vector<double> QMC::Halton(int s, int n) {
static std::vector<std::vector<int> > newBases(s);
static std::vector<int> bases = FirstPrimes(s);
/* replacing the statement immediately above with
static std::vector<int> bases = {2,3,5,7,11,13}; fixes it */
std::cout << "alloc static vecs \n";
std::vector<double> H(s);
std::cout << "alloc H \n";
// ...there's more to this function, but the error occurs just above this.
}
std::vector<int> QMC::FirstPrimes(int n) {
std::vector<int> primes(n);
primes[0] = 2;
int testNum = 3;
for (int countOfPrimes = 1; countOfPrimes <= n; ++countOfPrimes) {
while (isPrime(testNum) == false)
testNum = testNum + 2;
primes[countOfPrimes] = testNum;
testNum = testNum + 2;
}
return primes;
}
bool QMC::isPrime(int n) {
if (n == 1) return false; // 1 is not prime
else if (n < 4) return true; // 2 & 3 are prime
else if (n % 2 == 0) return false; // even numbers are not prime
else if (n < 9) return true; // 5 & 7 are prime
else if (n % 3 == 0) return false; // multiples of 3 (> 3) are not prime
else
{
int r = floor(sqrt((double)n));
int f = 5;
while (f <= r)
{
if (n % f == 0) return false;
if (n % (f + 2) == 0) return false;
f += 6;
}
return true;
}
}
FirstPrimes has a buffer overflow. The relevant lines:
std::vector<int> primes(n);
primes[0] = 2;
for (int countOfPrimes = 1; countOfPrimes <= n; ++countOfPrimes)
primes[countOfPrimes] = testNum;
For a vector of size n, the valud indices are 0 through n-1. On the last loop iteration you do an out-of-bounds access.
I'd suggest changing both of the [ ] to .at( ), as well as fixing the logic error. This would also prevent trouble if you happened to call this function with n == 0.

C++ Not Counting white beands

I need some help. I'm writing a code in C++ that will ultimately take a random string passed in, and it will do a break at every point in the string, and it will count the number of colors to the right and left of the break (r, b, and w). Here's the catch, the w can be either r or b when it breaks or when the strong passes it ultimately making it a hybrid. My problem is when the break is implemented and there is a w immediately to the left or right I can't get the program to go find the fist b or r. Can anyone help me?
#include <stdio.h>
#include "P2Library.h"
void doubleNecklace(char neck[], char doubleNeck[], int size);
int findMaxBeads(char neck2[], int size);
#define SIZE 7
void main(void)
{
char necklace[SIZE];
char necklace2[2 * SIZE];
int brk;
int maxBeads;
int leftI, rightI, leftCount = 0, rightCount=0, totalCount, maxCount = 0;
char leftColor, rightColor;
initNecklace(necklace, SIZE);
doubleNecklace(necklace, necklace2, SIZE);
maxBeads = findMaxBeads(necklace2, SIZE * 2);
checkAnswer(necklace, SIZE, maxBeads);
printf("The max number of beads is %d\n", maxBeads);
}
int findMaxBeads(char neck2[], int size)
{
int brk;
int maxBeads;
int leftI, rightI, leftCount = 0, rightCount=0, totalCount, maxCount = 0;
char leftColor, rightColor;
for(brk = 0; brk < 2 * SIZE - 1; brk++)
{
leftCount = rightCount = 0;
rightI = brk;
rightColor = neck2[rightI];
if(rightI == 'w')
{
while(rightI == 'w')
{
rightI++;
}
rightColor = neck2[rightI];
}
rightI = brk;
while(neck2[rightI] == rightColor || neck2[rightI] == 'w')
{
rightCount++;
rightI++;
}
if(brk > 0)
{
leftI = brk - 1;
leftColor = neck2[leftI];
if(leftI == 'w')
{
while(leftI == 'w')
{
leftI--;
}
leftColor = neck2[leftI];
}
leftI = brk - 1;
while(leftI >= 0 && neck2[leftI] == leftColor || neck2[leftI] == 'w')
{
leftCount++;
leftI--;
}
}
totalCount = leftCount + rightCount;
if(totalCount > maxCount)
{
maxCount = totalCount;
}
}
return maxCount;
}
void doubleNecklace(char neck[], char doubleNeck[], int size)
{
int i;
for(i = 0; i < size; i++)
{
doubleNeck[i] = neck[i];
doubleNeck[i+size] = neck[i];
}
}
I didn't study the code in detail, but something is not symmetric: in the for loop, the "left" code has an if but the "right" code doesn't. Maybe you should remove that -1 in the for condition and add it as an if for the "right" code:
for(brk = 0; brk < 2 * SIZE; brk++)
{
leftCount = rightCount = 0;
if (brk < 2 * SIZE - 1)
{
rightI = brk;
rightColor = neck2[rightI];
//...
}
if(brk > 0)
{
leftI = brk - 1;
leftColor = neck2[leftI];
//...
}
//...
Just guessing, though... :-/
Maybe you should even change those < for <=.

Issue with implementing "Closest pair of points" in C++

I'm trying to implement Closest pair of points in C++ according to Cormen book and wikipedia article, I think that algorithm is correct, but it does work only for a very small data. Code is below:
#include <cstdio>
#include <algorithm>
#include <cmath>
#define REP(i,n) for(int i=0;i<n;i++)
using namespace std;
struct point
{
long long x, y;
};
struct dist
{
long long x_1,y_1,x_2,y_2, distance;
} dis;
inline bool OrdX(const point &a, const point &b)
{
if(a.x==b.x)
{
return a.y<b.y;
}
return a.x<b.x;
}
inline int OrdY(const point &a, const point &b)
{
if(a.y==b.y)
{
return a.x<b.x;
}
return a.y<b.y;
}
// is - function that check is a an element of X_L array
inline bool is(const point &a, point *X_L, int p, int k)
{
if(p<=k)
{
int center = (p+k)/2;
if(X_L[center].x == a.x)
{
return true;
}
if(X_L[center].x > a.x)
{
return is(a, X_L, p, center-1);
}
else
{
return is(a, X_L, center+1, k);
}
}
return false;
}
// odl - function takes two points and return distance between them ^2
inline long long odl(const point &a, const point &b)
{
return ((a.x-b.x)*(a.x-b.x))+((a.y-b.y)*(a.y-b.y));
}
int tmp;
// fun - function that returns the pair of closest points using divide & conquer
struct dist fun(int n, point *X, point *Y)
{
// if there are less that 4 points - it checks it using bruteforce
if(n<4)
{
if(odl(X[0], X[1]) < dis.distance)
{
dis.distance = odl(X[0],X[1]);
dis.x_1 = X[0].x;
dis.y_1 = X[0].y;
dis.x_2 = X[1].x;
dis.y_2 = X[1].y;
}
if(n==3)
{
if(odl(X[0], X[2]) < dis.distance)
{
dis.distance = odl(X[0],X[2]);
dis.x_1 = X[0].x;
dis.y_1 = X[0].y;
dis.x_2 = X[2].x;
dis.y_2 = X[2].y;
}
if(odl(X[1], X[2]) < dis.distance)
{
dis.distance = odl(X[1],X[2]);
dis.x_1 = X[1].x;
dis.y_1 = X[1].y;
dis.x_2 = X[2].x;
dis.y_2 = X[2].y;
}
}
}
// otherwise it divides points into two arrays and runs fun
// recursively foreach part
else
{
int p=n/2;
int PPP = (X[p].x + X[p-1].x)/2;
point *X_L = new point[p];
point *X_R = new point[n-p];
point *Y_L = new point[p];
point *Y_R = new point[n-p];
REP(i,p)
X_L[i] = X[i];
for(int r=p; r<n; r++)
{
X_R[r-p] = X[r];
}
int length_Y_L = 0;
int length_Y_R = 0;
REP(i,n)
{
if(is(Y[i], X_L, 0, p))
{
Y_L[length_Y_L++] = Y[i];
}
else
{
Y_R[length_Y_R++] = Y[i];
}
}
dist D_L = fun(p, X_L, Y_L);
dist D_R = fun(n-p, X_R, Y_R);
dist D;
if(D_L.distance < D_R.distance)
{
D = D_L;
}
else
{
D = D_R;
}
tmp = 0;
point *Y2 = new point[n];
double from = sqrt((double)D.distance);
for(int r=0; r<n; r++)
{
if(Y[r].x > (long long)PPP-from && Y[r].x < (long long)PPP + from)
{
Y2[tmp++] = Y[r];
}
}
//--tmp;
//int xxx = min(7, tmp-r);
int r = 0;
for(int j=1; j<min(7, tmp-r); j++)
{
if(odl(Y2[r], Y2[r+j]) < D.distance)
{
D.distance = odl(Y2[r], Y2[r+j]);
D.x_1 = Y2[r].x;
D.y_1 = Y2[r].y;
D.x_2 = Y2[r+j].x;
D.y_2 = Y2[r+j].y;
}
r++;
}
dis = D;
}
return dis;
}
int main()
{
int n;
n = 7;
point *X = new point[n];
point *Y = new point[n];
for(int i=0; i< 7; i++)
{
X[i].x = 0;
X[i].y = 10*i;
}
/*
REP(i,n)
{
scanf("%lld %lld", &X[i].x, &X[i].y);
}
*/
sort(X, X+n, OrdX);
REP(i,n)
Y[i] = X[i];
sort(Y, Y+n, OrdY);
dis.distance = odl(X[0], X[1]);
dis.x_1 = X[0].x;
dis.y_1 = X[0].y;
dis.x_2 = X[1].x;
dis.y_2 = X[1].y;
dist wynik = fun(n, X, Y);
printf(" %lld %lld\n %lld %lld\n", wynik.x_1, wynik.y_1, wynik.x_2, wynik.y_2);
return 0;
}
and I get this error:
malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char
*) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct
malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size)
>= (unsigned long)((((__builtin_offsetof (struct malloc_chunk,
fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t)))
- 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end &
pagemask) == 0)' failed.
I've tried loooking for explanation of this error but can't find anything clear for me :/.
Can You please help me to solve this ? Thanks
The message means you've done something bad with dynamically allocated memory. Perhaps you freed an object twice, or wrote into memory beyond the beginning or end of an array-like dynamically allocated object.
On Linux, the tool valgrind may help pin-point the first place in your program's execution where it made a boo-boo.
By the way, your macro:
#define REP(i,n) for(int i=0;i<n;i++)
is poorly defined. The substitution of n should be parenthesized, because n could be an expression which has the wrong precedence with respect to the < operator. For instance: REP(i, k < m ? z : w). You want:
#define REP(var,n) for(int var=0;var<(n);var++)
The var reminds the programmer that this argument is a variable name, and not an arbitrary expression.
Your function is is redundant; that's just std::binary_search. That would help a lot with #sbi's problem of readability.
There's also quite a bit of redundancy in blocks like
dis.distance = odl(X[0],X[1]);
dis.x_1 = X[0].x;
dis.y_1 = X[0].y;
dis.x_2 = X[1].x;
dis.y_2 = X[1].y;
You can write a simple function dist calcDist(point,point) for this. You should probably move all the point definitions and associated functions to a separate header "point.h", again to keep things readable.
As for the memory issue: first, the arrays X_L and X_R are not really necessary. They contain the same data as X, so you can make them pointers to &(X[0]) and &(X[p) respectively. Y_L and Y_R are shuffled versions, so you do need to the arrays to copy data to. However, if you allocate them with new[], you are responsible for cleanup with delete[]. It looks like you can just use a std::vector<point> Y_L instead. No need to do bookkeeping, vector does that for you. Just call Y_L.push_back(Y[i]).