🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

7 total messages Started by Alain M. Gaudrault Tue, 22 Aug 1995 09:09
g++ and qsort
#737
Author: Alain M. Gaudrault
Date: Tue, 22 Aug 1995 09:09
38 lines
2235 bytes
	I'm having a tough time using the C qsort function within a C++
class.  I have the following array of pointers:

   static dataItemType** dataBlockVectorA = NULL;

(which I initialize before the call to qsort, obviously), then further on
down, I call:

   qsort( dataBlockVectorA, dataBlockSize, sizeof( dataItemType*), pcompfunc);

where pcompfunc is defined as follows:

   int transformDataC::pcompfunc( dataItemType** item1, dataItemType** item2);

As can be seen, the ordering function required by qsort is a member function,
and in this case needs to be so as it makes use of other class members.  The
actual call to qsort is also within a member function of the same class.
Problem is, this won't compile.  g++ indicates that:

transformData.C: In method `void transformDataC::algorithmC()':
transformData.C:165: incompatible pointer types for argument 4 of `int qsort(void *, long unsigned int, long unsigned int, int (*)(const void *, const void *))'

Yet, if I create a dummy ordering function with the same type declarations,
but *not* a member function of the class, g++ doesn't complain.  So it seems
that there's a conflict somewhere because I'm trying to use a class member as
an ordering function as opposed to a non-class member.  Is there some sort of
workaround, solution of ANY kind other than writing my own qsort routine?

Details: I'm using gcc 2.5.8 under SunOS 4.1.2

                                                 Big Al the Devil's Pal!!
                                                    __   __            
                                                   /  `-' /          ,,
                                                   |[====|||||||||||[::}
                                                   \__.-._\          ``

                                            http://ccnga.uwaterloo.ca/~amgaudra
Re: g++ and qsort
#741
Author: Pete Grant
Date: Wed, 23 Aug 1995 00:14
35 lines
1595 bytes
In article <DDpG3H.88M@watserv3.uwaterloo.ca> amgaudra@ccnga.uwaterloo.ca (Alain M. Gaudrault) writes:
>
>	I'm having a tough time using the C qsort function within a C++
>class.  I have the following array of pointers:
>
>   static dataItemType** dataBlockVectorA = NULL;
>
>(which I initialize before the call to qsort, obviously), then further on
>down, I call:
>
>   qsort( dataBlockVectorA, dataBlockSize, sizeof( dataItemType*), pcompfunc);
>
>where pcompfunc is defined as follows:
>
>   int transformDataC::pcompfunc( dataItemType** item1, dataItemType** item2);
>
>As can be seen, the ordering function required by qsort is a member function,

Ok, it's a member function.  Now, think about this:  On what object 
is this function to operate?  Remember, member functions are invoked
by either ptr->memberfunc() or obj.memberfunc().  

The function specification for qsort requires two void * arguments.  The
way to comply with the spec is to do something like:

(Warning!  Untested code.)

int pcompfunc (const void * a, const void * b)
 {
   dataItemType * p1 = (dataItemType*)*a;
   dataItemType * p2 = (dataItemType*)*b;
   return p1->comparefunction(p2);
 }

Pete.
Re: g++ and qsort
#739
Author: Charles Fiterman
Date: Wed, 23 Aug 1995 13:39
8 lines
885 bytes
Qsort assumes that objects can me moved bitwise. Many C++ objects have interior
pointers and cannot be moved bitwise. They must be copied and deleted in place.

Therefore what you should do is build an array of pointers to the objects and
sort the array. References to the sorted array should use the pointers. 

The alternative is to write a C++ template qsort that uses a temporary object
for exchange. This would be much slower.
Re: g++ and qsort
#742
Author: Andrew Koenig
Date: Wed, 23 Aug 1995 16:01
10 lines
767 bytes
In article <DDrnAF.E76@midway.uchicago.edu> Charles Fiterman <cef@geodesic.com> writes:

> The alternative is to write a C++ template qsort that uses a temporary object
> for exchange. This would be much slower.

Actually, it's much faster -- because it doesn't require
a function call for each comparison.
-- 
				--Andrew Koenig
				  ark@research.att.com
Re: g++ and qsort
#747
Author: a-cnadc Dann Corbit
Date: Fri, 25 Aug 1995 18:58
13 lines
1117 bytes
In article <DDpG3H.88M@watserv3.uwaterloo.ca>, amgaudra@ccnga.uwaterloo.ca says...
>
>
>        I'm having a tough time using the C qsort function within a C++
>class.                                     http://ccnga.uwaterloo.ca/~amgaudra
The problem is the pointer types are not the same.  
If a function is a member of an object, there is that hidden "this" pointer.
I've seen this same problem before, and have not seen any way to get around it.
What I ended up doing was creating public cdecl functions (yetch).
If someone knows a clever work around, I would like to know about it too.
-- 
The opinions expressed in this message are my own personal views 
and do not reflect the official views of Microsoft Corporation.
Re: g++ and qsort
#749
Author: Charles Fiterman
Date: Mon, 28 Aug 1995 14:14
12 lines
1079 bytes
In article <DDrtu4.13r@research.att.com>,
Andrew Koenig <ark@research.att.com> wrote:
>In article <DDrnAF.E76@midway.uchicago.edu> Charles Fiterman <cef@geodesic.com> writes:
>
>> The alternative is to write a C++ template qsort that uses a temporary object
>> for exchange. This would be much slower.
>
>Actually, it's much faster -- because it doesn't require
>a function call for each comparison.

Actually it depends on how expensive an objects constructor and destructor is.
Most constructors are fairly expensive and involve function calls.
Re: g++ and qsort
#755
Author: Don &
Date: Wed, 30 Aug 1995 00:53
45 lines
1801 bytes
The original message from amgaudra that apparently started this
thread has expired at our site.  So the following may or may not
answer the question.  The code below works with gcc-2.7.0 on a
Sparc.  It allows one to access qsort in a relatively type-safe
way.  "Relatively" because there is still a function pointer
cast used, but at least it is just in one place.  Probably it
would be best to use one of the new types of casts recently added
to the language, but I haven't learned that stuff yet.

Don Bashford
bashford@scripps.edu


// A template wrapper around the C library qsort function
#include <stdlib.h>

template<class T>
void myqsort(T* base, size_t nmemb, int (*Compar)(const T*, const T*) )
{
  typedef int (*PFC)(const void*, const void*);
  PFC compfun = (PFC) Compar;
  size_t size = sizeof(T);
  qsort (base, nmemb, size, compfun);
}

// ----- Stuff above here could be in a header file.  Below is application.

#include <iostream.h>

int mycomp(const int* i, const int* j)
{
  return *i - *j;
}

main()
{
  int a[3];
  a[0] = 6;
  a[1] = 2;
  a[2] = 4;
  myqsort(a, 3, mycomp);
  cout << "a contains ";
  for (int i=0; i<3; ++i) cout << a[i] << " ";
  cout << endl;
}
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads