Pointers issue here , hope someone can help

Hi ,
I have a pointer issue here , 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
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"; 

  return array1;
}
unsigned __stdcall QuickSort(void* a) {
	int arraysize;
	int g=0;
	int *array = (int*)a;
	arraysize=sizeof(&array) / sizeof(int);
	cout << " Element number 3 is " << array[3]  << endl;
	return 0;
}

int main () {
  int* list;
  list=readFile();
  //int arraytest[4]={1,2,3,4};
  cout << "Element number 3 is  " << list[3]<< endl;
  _beginthreadex(NULL, 0,  QuickSort, &list, 0, 0);
  system ("pause");
  return 0;
}


the cout for element number 3 , is not matching after calling the QuickSort threat. i know i am passing a pointer to a pointer , but everything i tried didnt work .

Thanks
You are passing a pointer to a pointer but you read it as a pointer to int. Do either
1
2
3
4
5
_beginthreadex(..., &list, ...);
//and
int **array = (int**)a;
//and
cout << ... << (*array)[3] << ...;
or
1
2
3
4
5
_beginthreadex(..., list, ...);
//and
int *array = (int*)a;
//and
cout << ... << array[3] << ...;


The problem is that right now your array variable points to the location of list variable, which is nowhere near the memory you actually want to read.
ooh man u tried int **array = (int**)a;
and this is what i was missing (*array)[3]

Thank you soo much man
Hi,

but , is there any memory leak as i can see , you are allocating the memory to the array but we are not deleting it any were ..
please correct me if i am wrong ..
cheers . .
sizeof(&array) won't give you the size of the array, you need to pass that in somehow.
@bluecoder, sure, but in a program this small it's hardly relevant as the OS will free all that memory when the program ends anyway.
hi ,
according to me all that u have said is true
/*_beginthreadex(..., list, ...);
*//and
*int *array = (int*)a;
*//and
*cout << ... << array[3] <<
*/
@hamsterman for this particular code there is a defuult because the arry has been set to a maximum holding of the value 3.thus as for me i would have used ur code but change the array with [*] to give room for greater number coz the value 3 can be sorted from an array with more values
thanks hamsterman
Hi ,
Thanks for the amazing discussion .I thought this is done .
Well now that issue is over now , I am pointing to the correct location now .
But here is the issue now .
I am having an issue , as kbw said sizeoff() is not working .i looked around and i apperantly you cant use sizeoff for a pointer array , make sense for sure , but not helping .
So i user a global variable .

My problem is that when i call QuickSort , the array size is not passed correctly .I need to call quicksort and pass it the left and right side of the array .
As you can see in my code int arraysize=globalArraySize; will always set that to 8 , and basically i cant terminate my recursive call .
I have to only pass array , was thinking of adding the size at the end , but i was hopping for a better way to do that.
Again thanks to all who participated here , amazing info as usual.

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
#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=globalArraySize;
	int leftRec=0;
	int rightRec=arraysize-1;
	int pivot =array[int(arraysize/2)];
	int leftSize=arraysize-int(arraysize/2);
	int rightSize=arraysize-leftSize;
	//cout << " left is " << leftSize << "right is " << rightSize << endl;
	if (arraysize<=1){
		return 0;
	}
	while (leftRec < rightRec){
		while ( array[leftRec] <pivot){
			++leftRec;
		}
		while ( array[rightRec] >pivot){
			--rightRec;
		}
			int temp=array[leftRec];
			array[leftRec]=array[rightRec];
			array[rightRec]=temp;
	}
	_beginthreadex(NULL, 0,  QuickSort,  array, 0, 0);
	return 0;
}

int main () {
  int* list;
  list=readFile();
  _beginthreadex(NULL, 0,  QuickSort, list, 0, 0);
  system ("pause");
  return 0;
}
Last edited on
I am not sure it will work if the array size is 5 .. .
as int leftSize = arraysize - int( arraysize / 2 ) // will be 3 .
int rightSize = arraysize - leftSize ; // will be 2

now the condtion
while( leftSize < rightSize ) will never be satisfied .. and will not enter into the loop.
A good thing to do would be
1
2
3
4
struct QSortData{
   int* array;
   int size;
};

Create such object before calling every thread and pass it so that you can move more than one variable.
blue , thanks for that , i will look into it .

hamsterman , thanks for your advice , but my main should pass the array grabbed from the file to the thread . your solution would of made things soo much easier ( probably i should double check with my professor to see if i am allowed to pass struct instead of array , but i doubt it .
Topic archived. No new replies allowed.