How to have two array pointers to one array

Hi,
My problem in the following code is that when i reach to passing array to thread at the end , i am not passing a pointer , and as a result , not getting things sorted up correctly .
I know my problem is at arrLeft and arrRight being created and copied values , and not pointed to the original array , as a result i think the sub arrays arrRight and arrLeft are sorted but not the original array because its not pointed .
Hope this is the problem , but i cant figure it out .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <fstream>
#include <windows.h>
#include <process.h>
#include <time.h>
#include <string>
#include <sstream>
#include <string>
#include <array>
using namespace std;
int globalArraySize=0;

int * readFile(){
	int* array1 ;
	int arraysize;
	int i=0;
	ifstream myfile ("input.txt");
    if (myfile.is_open())
  {
	myfile >> arraysize;
	array1= new int[arraysize];
		while ( myfile.good() )
		{
			myfile >> array1[i++];
		}
		myfile.close();
  }
  else cout << "Unable to open file"; 
  globalArraySize=arraysize;
  return array1;
}
unsigned __stdcall QuickSort(void* a) {
	int *array = (int*)a;
	int arraysize=_msize(array) / sizeof(int);
	cout << " First element is   " <<  array[0] <<"last element is " << array[arraysize-1]<< endl;
	int leftRec=0;
	int rightRec=arraysize-1;
	int pivot =array[int(arraysize/2)];
	int leftSize=int(arraysize/2);
	int rightSize=arraysize-leftSize;
	if (arraysize<=1){
		return 0;
	}
	cout << " Size is  " <<  arraysize << endl;
	while (leftRec < rightRec){
		while ( array[leftRec] <pivot){
			++leftRec;
		}
		while ( array[rightRec] >pivot){
			--rightRec;
		}
			int temp=array[leftRec];
			array[leftRec]=array[rightRec];
			array[rightRec]=temp;
	}


        // **************I think problem starts here *****************

	int *arrLeft =new int[leftSize];
	int *arrRight=new int[rightSize];
	int f=0;
	for ( int j=0;j<arraysize;j++){
		if(j<leftSize)
		{
		   arrLeft[j]=array[j];
		}
		else {
			arrRight[f++]=array[j];
		}
	}
	
	_beginthreadex(NULL, 0,  QuickSort,  arrLeft, 0, 0);
	_beginthreadex(NULL, 0,  QuickSort,  arrRight, 0, 0);
	//cout << " array elements  " <<  array[1] <<"  " << array[2] <<"  " <<array[3] <<"  " <<endl;
	return 0;
}

int main () {
  int* list;
  list=readFile();
  _beginthreadex(NULL, 0,  QuickSort, list, 0, 0);
  Sleep(4000);
  cout << "array elements are " ;
  for ( int i=0;i<=7;i++){
	  cout <<" , " <<list[i];
  }
  cout << endl;
  system ("pause");
  return 0;
}
Last edited on
There's never a need to pass a pointer by reference. Since a pointer just holds an address, when passing a pointer to a function that address gets copied to the function, meaning the pointer in the function will point to the same object the pointer you passed to the function pointed two.

The way you've indented/stylized your code is pretty messy (and doesn't have much uniformity to it), so you should try to clean that up.

I just briefly looked at your program, but line 20 seems suspicious. I don't think you want to initialize an array the size of the integral value of the first word in your input file.
hi,
Thank you for your afford
i know some stuff not very good to implement . but this is a university assignment , and has to be done that way .
let me try to ask my question in a better way.
for the following code , how can i create two arrays , one is left and one is right , that their elements pointed to the original array .
For example , if my array [1,2,3,4,5,6]
i want to create leftArray with 3 elements pointed to the 1st 3 elements , and rightArray with 3 elements pointed to the next 3 .
1
2
3
4
5
6
7
8
9
10
11
        int *arrLeft =new int[leftSize];
	int *arrRight=new int[rightSize];
	int f=0;
	for ( int j=0;j<arraysize;j++){
		if(j<leftSize)
		{
		   arrLeft[j]=array[j];
		}
		else {
			arrRight[f++]=array[j];
		}
Last edited on
closed account (D80DSL3A)
You want the two pointers to point to different portions of a single existing array?
1
2
arrLeft = array;// points to beginning of array
arrRight = array + leftSize;// points to leftSide elements further into array 


Your code creates two new arrays then copies data from the first array (array) to the two new ones.

ascii wrote:
There's never a need to pass a pointer by reference.

There is if you need the function to change what the pointer points to.
Last edited on
Hello fun2code ,

this is exactly what happened , i am aware of that , and as a result my array is not getting sorted out when i pass it again to thread . As you can see the new created array is what getting sorted and not the original array .
this is why i am trying to find how to create a fixed size array where each element is pointed to the elements in the first array .
by the way , if i use :
arrLeft = array;// points to beginning of array
then my arrLeft will have a size of the original array , will basically point to all the elements of my original array , and as a result when i pass this back to my thread , it will read size = [original array size ] which not what i want .This is why i am trying to create array with the sizes i managed to get from leftsize and rightsize , and have each element in this array pointed to the elements in original array.
closed account (D80DSL3A)
Perhaps there are issues because your program is multi-threaded. I am not familiar with this.
Regarding the pointers though, a pointer points to only one location in memory and not to an entire array. At a given moment the pointer is pointing only to a specific element in the array.

if i use :
arrLeft = array;// points to beginning of array
then my arrLeft will have a size of the original array

No. The pointer is not the array itself.

You restrict what elements that a pointer can point to by restricting how the pointer is incremented. When you assign arrLeft = array; then arrLeft is pointing to the 1st element of array (element 0). When you increment the pointer to arrLeft + i then it is pointing to element i in array. To keep arrLeft pointing to only array[0] through array[leftSize-1] simply don't increment it any further than arrLeft + leftSize - 1.

Maybe you can see what I mean from this working code for a (single-threaded) quicksort:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
void swap(int* A, int i, int j)
{
	if(i==j)
		return;
	int temp = A[i];
	A[i] = A[j];
	A[j] = temp;
	return;
}

int partition(int* A, int left, int right, int pivotIdx)
{
	int pivotVal = A[pivotIdx];
	swap(A,pivotIdx,right);
	int stIdx = left;
	for(int i=left; i<right; ++i)
		if(A[i] <= pivotVal)
		{
			swap(A,i,stIdx);
			++stIdx;
		}
	swap(A,stIdx,right);	
	return stIdx;
}

void quicksort(int* A, int left, int right)
{
	if(right > left)
	{
		int pivotIdx = (right+left)/2;// any value left <= pivotIdx <= right is OK here
		int newPivotIdx = partition(A,left,right,pivotIdx);
		quicksort(A,left,newPivotIdx-1);
		quicksort(A,newPivotIdx+1,right);
	}
	return;
}

Note that on lines 32 a pointer to the 1st element in the array (A) is passed along with the lowest allowed index (left) and the highest allowed index (newPivotIdx-1). Similarly on line 33.
hello fun2code,
thanks for ur reply .I think i managed to do what i want , but didnt debug it yet.
This is what i have now :
I just have to wait for threads to finish . I will try that , and will let you know.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int **arrLeft =new int*[leftSize];
	int **arrRight=new int*[rightSize];
	//int **arrLeft,**arrRight;
	///arrLeft[leftSize];
	//arrRight [leftSize];
	int f=0;
	for ( int j=0;j<arraysize;j++){
		if(j<leftSize)
		{
		   arrLeft[j]=(int*)array[j];
		}
		else {
			arrRight[f++]=(int*)array[j];
		}
	}
	Sleep(4000);
	_beginthreadex(NULL, 0,  QuickSort,  arrLeft, 0, 0);
	Sleep(4000);
	_beginthreadex(NULL, 0,  QuickSort,  arrRight, 0, 0);
	return 0;
ok so , i managed to fix the stuff we discussed before regarding pointers and all that .
My array is getting sorted , but only the left and right part , the code some how doesnt sort all elements

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <fstream>
#include <windows.h>
#include <process.h>
#include <time.h>
#include <string>
#include <sstream>
#include <string>
#include <array>
using namespace std;
int globalArraySize=0;
struct ThreadInfo
{
	int *list;
	int size;
	bool run;
	int *pLeft;
	int *pRight;
};

int * readFile(){
	int* array1 ;
	int arraysize;
	int i=0;
	ifstream myfile ("input.txt");
    if (myfile.is_open())
  {
	myfile >> arraysize;
	array1= new int[arraysize];
		while ( myfile.good() )
		{
			myfile >> array1[i++];
		}
		myfile.close();
  }
  else cout << "Unable to open file"; 
  globalArraySize=arraysize;
  return array1;
}
unsigned __stdcall QuickSort(void* a) {

	ThreadInfo *info = (ThreadInfo*)a;
	//int arraysize=_msize(array) / sizeof(int);
	int *array=info->list;
	cout << " First element is   " <<  array[0] <<"  last element is " << array[info->size-1]<< endl;
	int pivot =array[int((info->size)/2)];
	int leftSize=int((info->size)/2);
	int rightSize=(info->size)-leftSize-1;
	//cout << "Full Size is  " <<  info->size << endl;
	//cout << "Right size is : " << rightSize << endl;
	if (info->size<=1){
		info->run = false;
		return 0;
	}
	while (info->pLeft < info->pRight){
		while ( *info->pLeft < pivot){
			++info->pLeft;
		}
		while ( *info->pRight  > pivot){
			--info->pRight;
		}
			int temp=*info->pLeft;
			*info->pLeft=*info->pRight;
			*info->pRight=temp;
	}
	//cout << " First element in the array after loop is : " << info->list[0] << endl;
	ThreadInfo *arrLeft =new ThreadInfo;
	arrLeft->list=info->list;
	arrLeft->pLeft=info->list;
	//cout << "First element is the left array is : " <<arraarrLeft->pLeft << endl;
	arrLeft->pRight=&info->list[info->size/2];
	arrLeft->size=leftSize;
	arrLeft->run=true;
	ThreadInfo *arrRight=new ThreadInfo;
	arrRight->list=arrLeft->pRight+1;
	arrRight->pLeft=arrLeft->pRight+1;
	arrRight->pRight=&info->list[info->size-1];
	arrRight->size=rightSize;
	arrRight->run=true;
	_beginthreadex(NULL, 0,  QuickSort,  arrLeft, 0, 0);
	_beginthreadex(NULL, 0,  QuickSort,  arrRight, 0, 0);/*
	bool anyRun = true;
	while(anyRun)
	{
		anyRun = false;
		for(int i = 0; i < 8; i++)
		{
			anyRun =anyRun || arrLeft[i].run || arrRight[i].run;
		}
	}*/
	info->run = false;
	return 0;
}

int main () {
    //int* list;
    ThreadInfo info;
    //Read the input.text file , and pass an array , that contain the values in that text file .
	info.list =readFile() ;
	info.run = true;
	info.pLeft=&info.list[0];
	info.pRight=&info.list[globalArraySize-1];
	info.size=globalArraySize;
  //Start my thread , passing the array passed from readFile function.
  _beginthreadex(NULL, 0,  QuickSort, &info, 0, 0);
 // while(info.run){}
  Sleep(9000);
  cout << "array elements are " ;
  for ( int i=0;i<=7;i++){
	  cout <<" , " <<info.list[i];
  }
  cout << endl;
  system ("pause");
  return 0;
}
Topic archived. No new replies allowed.