pointer mayhem

This is a discussion on pointer mayhem within the C++ Programming forums, part of the General Programming Boards category; Hi. I'm trying to understand how pointers work. I have an algorithms implemented in two ways: one using pointers, the ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    8

    Question pointer mayhem

    Hi.

    I'm trying to understand how pointers work. I have an algorithms implemented in two ways: one using pointers, the other not so much. The thing is, the second implementation works, the first doesn't and I'm not sure why.

    Here's my main (IN_FILE, N_DATA and E_GOOD are globally defined variables: name of an input-file, 1000, and the exit code):
    Code:
    int main(const int argc, const char *argv){
    	ifstream fin(IN_FILE);
    	vector< Supplier >* supps = new vector< Supplier > (N_DATA);
    	
    	for(int i = 0; i < N_DATA; ++i)
    		fin >> supps->at(i).companyID >> supps->at(i).price;
    	
    	printSuppliers( &selectionSort(*supps), 2);
    	supps = sort(supps);
    	cout << "<--------------------------->" << endl;
    	printSuppliers(supps, 2);
    	
    	return E_GOOD;
    }
    Here's the pointer based sorting:
    Code:
    vector< Supplier > * sort(vector< Supplier > *v){
    	int i, j, min;
    
    	for (i = 0; i < v->size() - 1; i++){
    		min = i;
    		for (j = i+1; j < v->size(); j++){
    			if (v->at(i).price < v->at(min).price){          
    				min = j;
    			}
    		}
    		swap(v->at(i), v->at(min));
    	}
    	
    	return v;
    }
    and the non-pointer based:
    Code:
    vector<Supplier> selectionSort(vector<Supplier> a){
    	int i, j, min;
    
    	for (i = 0; i < a.size() - 1; i++){
    		min = i;
    		for (j = i+1; j < a.size(); j++){
    			if (a[j].price < a[min].price){          
    				min = j;
    			}
    		}
    		swap(a[i], a[min]);
    	}
    	
    	return a;
    }
    here's the printing function:
    Code:
    void printSuppliers(const vector< Supplier > *v, int n){
    	for(int i = 0; i < n; i++){
    		cout 
    			<< "Supplier: " << i << endl
    			<< "\tID: " << v->at(i).companyID << endl
    			<< "\tPrice: " << v->at(i).price << endl << endl
    			;
    	}
    }
    My first output (from the dereferenced sorting) is in correct order, but the second output is unsorted:
    Code:
    Supplier: 0
            ID: C956
            Price: 828
    
    Supplier: 1
            ID: C38
            Price: 893
    
    <--------------------------->
    Supplier: 0
            ID: C0
            Price: 1869
    
    Supplier: 1
            ID: C1
            Price: 1728
    Anyone care to help me understand why this is happening?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >			if (v->at(i).price < v->at(min).price){
    Shouldn't that be j?
    Code:
    			if (v->at(j).price < v->at(min).price){

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    Quote Originally Posted by swoopy View Post
    Code:
    >			if (v->at(i).price < v->at(min).price){
    Shouldn't that be j?
    Code:
    			if (v->at(j).price < v->at(min).price){

    ...yes...yes it should. Thanks for pointing that out, and boy do I feel stupid for missing that. At least I know that my understanding of pointers isn't drastically off.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    There is no reason really to allocate the vector itself dynamically (using new), and the usual way to pass it to functions is via references.

    For example
    Code:
    void selectionSort(vector<Supplier>& a)
    This lets you modify the vector in the function.

    I can see that you want selectionSort to return the vector to be used as an argument for another function. It is safe to return a reference to the parameter that you passed in by reference. Thus:
    Code:
    vector<Supplier>& selectionSort(vector<Supplier>& a)
    Your non-pointer version is not very efficient: you pass a copy of the vector and you return a copy.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer Mayhem
    By thomas41546 in forum C Programming
    Replies: 3
    Last Post: 05-11-2008, 11:25 AM
  2. Replies: 1
    Last Post: 03-24-2008, 11:16 AM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 08:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21