PDA

View Full Version : Debug error: Invalid parameter passed to C runtime function.



metRo_
24th October 2010, 19:09
I want use binaysearch but it give an error when bsearch try to call comparator. The debug error is: "Invalid parameter passed to C runtime function"; I can't figure it out :S


#include "mainwindow.h"
#include <QtCore>
#include <QtGui>

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

#define RECORD_SIZE (6+10+1)

int comparator(const void *a, const void *b);

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
//QString *ssid = new QString("60EFF9");
QString dir = QFileDialog::getOpenFileName(this, tr("Select Text File"),"",tr("Text (*.txt)"));
QFile f(dir);
qint64 fileSize = f.size();

uchar* ptr = f.map(0,fileSize,QFile::NoOptions);

char *ssid="60EFF9";

char *res =(char*) bsearch(ssid, ptr, fileSize / RECORD_SIZE, RECORD_SIZE, (int(*)(const void*, const void*))comparator);

if (NULL == res) {
qDebug()<<("No match!!");
} else {
qDebug("Match found: %.10s\n", res+6);
}

}

MainWindow::~MainWindow()
{

}

int comparator(const void *a, const void *b)
{
qDebug()<<"IN";

const char *aa = (const char *)a, *bb = (const char *)b;

for (int i = 0; i < 6; ++i) {
if (aa[i] != bb[i]) {
return aa[i] - bb[i];
}
}
return 0;
}


Added after 55 minutes:

SOLVED: i call f.map(...) before open the file :)