|
|
Hi,
I have the following function
int DataPeakCompareFunc(const void *el1, const void *el2)
{
const DataPeak *peak1 = reinterpret_cast<const datapeak="" *="">(el1);
const DataPeak *peak2 = reinterpret_cast<const datapeak="" *="">(el2);
if (peak1->position < peak2->position)
{
return -1;
}
else if (peak2->position < peak1->position)
{
return 1;
}
return 0;
}
I am calling qsort function with the below parameters
qsort((void *)pScan, num_readings, sizeof(DataPeak), DataPeakCompareFunc);
Then i am getting the above error.
What to do?
Thanks in advance
|
|
|
|
|
subramanyeswari wrote: qsort((void *)pScan, num_readings, sizeof(DataPeak), DataPeakCompareFunc);
qsort((void *)pScan, num_readings, sizeof(DataPeak), <code>&</code>DataPeakCompareFunc);
Remember that this function should not be member function. Either it should be static or a global function.
Lookup qsort . Here is the callback function signature.
int (__cdecl *compare )(const void *elem1, const void *elem2 ) BTW why are you posting the whole error as caption for your post. Looks odd.
|
|
|
|
|
|
I swear I already answered this. I'll repeat:
Don't use qsort : it's crap, old, not type safe, etc....
Use STL instead. Here's an example:
#include "stdafx.h"
#include <algorithm>
class CData
{
public:
CData(int val) : m_val(val) {}
friend bool operator<(const CData &l, const CData &r)
{
return l.m_val < r.m_val;
}
private:
int m_val;
};
int main(int arvc, char* argv[])
{
CData SortMe[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
CData *pOnePastEnd = SortMe + sizeof(SortMe)/sizeof(SortMe[0]);
std::sort(SortMe, pOnePastEnd);
return 0;
}
Steve
|
|
|
|
|
Stephen Hewitt wrote: Don't use qsort: not type safe, etc....
What do you mean by type safe? We are ones who's using it and we know what types we are passing to it. So how can you say it's not type safe.
Stephen Hewitt wrote: old,
Old is gold.
Stephen Hewitt wrote: it's crap
No it's not.
|
|
|
|
|
STL is obviously simpler, easier to understand and less messier than qsort can ever be. STL code would execute faster than the routines that we may generally write. Did you notice the STL code to sort a given array? That is 1 line! Brilliant use of operator overloading. If you are writing c++ code, I don't find a good reason to use qsort, instead of STL.
Nobody can give you wiser advice than yourself. - Cicero
|
|
|
|
|
brahmma wrote: If you are writing c++ code, I don't find a good reason to use qsort, instead of STL.
That doesn't mean qsort is crap and not type safe. If you are using it, then you have reasons. It's just a function call, I've used it, works well.
My company does not use STL. So qsort works well.
|
|
|
|
|
Nibu babu thomas wrote: That doesn't mean qsort is crap
Why do you tell that to *me*
Nibu babu thomas wrote: It's just a function call, I've used it, works well.
It works; I never said no. I was trying to compare qsort with an STL based solution. I felt when compared, STL is much better. My point is that if STL could be used instead of qsort, it must be, IMO!!
PS: I'm not voting you one.
Nobody can give you wiser advice than yourself. - Cicero
|
|
|
|
|
brahmma wrote: Why do you tell that to *me*
I've quoted the statement to which I am replying.
|
|
|
|
|
Nibu babu thomas wrote: That doesn't mean qsort is crap and not type safe. If you are using it, then you have reasons. It's just a function call, I've used it, works well.
There is no sane reason to use qsort in preference to std::sort in C++ code; it's (std::sort) better in every respect from type safety through to sorting speed and flexibility.
Nibu babu thomas wrote: My company does not use STL. So qsort works well.
Your company is making an error of judgment in this respect. If I was in charge the person responsible for this would have a lot of explaining to do.
Remember the mantra:
qsort is crap!
Steve
|
|
|
|
|
Nibu babu thomas wrote: Old is gold.
You must use Windows 3.11 :->
Nobody can give you wiser advice than yourself. - Cicero
|
|
|
|
|
Nibu babu thomas wrote: No it's not.
It is; 100% crap.
Steve
|
|
|
|
|
Nibu babu thomas wrote: What do you mean by type safe? We are ones who's using it and we know what types we are passing to it. So how can you say it's not type safe.
Look at the sorting callback; it uses reinterpret_cast s to recover type information which was discarded in the call to qsort (with the conversion to void* )! If that doesn't scream out for a better solution then I don't know what does. If the elements sorted were changed to a different type but the callback wasn't adjusted it will still compile and run but the reinterpret_cast s in the callback will result in strange runtime errors: this is one way in which the fact that qsort is not type safe can cause maintenance problems.
Steve
|
|
|
|
|
Apart from that, every time you use qsort, god kills a kitten.
Nobody can give you wiser advice than yourself. - Cicero
|
|
|
|
|
Stephen Hewitt wrote: Don't use qsort: it's crap, old, not type safe, etc....
Don't forget slow[^] (compared to std::sort)
|
|
|
|
|
Hi,
after having some problems with a self-drawn dialog to derive the CPrintDialog I have successfully sent the document to the printer. The problem is that I can only print 1 page, although the pInfo->GetMaxPage () returns the correct value it should print (from 1 to 9 depending on some things).
What should I check? the pInfo structure is correct (I have check it in debugger and with AfxMessageBox ()) but it doesn't print right yet.
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
|
|
|
|
|
Hi,
2 minutes after posting the message I have solved it. I didn't set nFromPage and nToPage in pInfo->m_pPD->m_pd. (device) so...
thanks anyways
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
|
|
|
|
|
I have few doubts about meomory allocations in program.
1. Which variables are allocated in stack and which are in heap.
2. How can the scope of the global and static variables is through out the application.
3. Why the varibles in stack cannot have program scope while the variables in the heap can have?
Can you please give me a link or two where i can get the information on memory allocation.
Thank you.
KIRAN PINJARLA
|
|
|
|
|
At first you should probably begin with reading about the stack[^] and the heap[^].
Regarding your questions:
1. Unless you allocate memory from the heap with calls like new , malloc(...) or similar and assign the returned value to a pointer variable, your data will be allocated on the stack.
In code the difference would look like this:
int foo()
{
int nValueOnStack;
int pnValueOnHeap = new int;
....
delete pnValueOnHeap;
}
2. I don't understand your question. It doesn't validate to a full sentence.
3. I'm not really sure what you mean. But I think you will get an answer when you read how the stack works in the link above.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
1. Unless you allocate memory from the heap with calls like new, malloc(...) or similar and assign the returned value to a pointer variable, your data will be allocated on the stack.
And what about global variables? If not created in the heap, they are not in the stack too but in the global data segment.
--
=====
Arman
|
|
|
|
|
Arman Z. Sahakyan wrote: And what about global variables?
I chose to omit that in order to not confuse the OP since he asked about the difference between the stack and the heap.
As always it's difficult to give a relatively short answer matching the OP's knowledge and experience. In my opinion it's sometimes better to leave details out for clarity reasons, but unfortunately it's not a bullet proof concept.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
As already advised, you'd rather learn some tutorials on memory artchitecture. They are many in the web.
1. Which variables are allocated in stack and which are in heap.
Those that
All new-ed (or malloc-ed) variables are in the heap.
Other function scope variables are in the stack.
Globals are in the global data segment [if not created by new].
2. How can the scope of the global and static variables is through out the application.
Though not a clear question...
This needs a detailed explaination. Read some tutorials please.
3. Why the varibles in stack cannot have program scope while the variables in the heap can have?
Because the in-stack variables are managed by the stack itself and each time a function returns, its part [top] of the stack is removed and thus destroying all the variables inside that fuction scope.
--
=====
Arman
|
|
|
|
|
The main diff btn global and static variable is u can use static variable in one file and within the scope while global variable u can use thro out the program using extern keyword, u cant use extern keyword with static variable.
Global & static variable get stored in code segement it nither heap nor stack.
Thank You
Sethuraman.K
|
|
|
|
|
Hai all,
when i compile my program i am getting Debug Assertion Failure Message. If i click ignore the program is working fine. in my application i am calling a dialog from another dialog. Can Any one tell me what is the problem & how i can rectify this problem
Thanks in Advance
|
|
|
|