[Qt-interest] Resizing static array data in struct fp_print_data? 0_o How?
Constantin Makshin
cmakshin at gmail.com
Mon Mar 7 13:50:11 CET 2011
In C[++] arrays are somewhat equivalent to pointers so sometimes dynamic arrays are declared as very small small static arrays. Since one cannot [easily] malloc() or 'new' those arrays, such structures should be allocated as a whole, i.e. instead of something like:
fp_print_data pd;
// ...
pd.data = (unsigned char*)malloc(12345);
you should/must write:
fp_print_data* pd = (fp_print_data*)malloc(sizeof(fp_print_data) + 12345);
// ...
This way, all members are still the same as if you allocated the structure on the stack, but there's an extra memory block that acts as 'data' array contents.
However, I suggest using specialized libfprint functions fp_print_data_get_data() and fp_print_data_from_data() to perform [de]serialization:
unsigned char* buf;
size_t n = fp_print_get_data(&fpdata, &buf);
QByteArray byteArray((const char*)buf, qMin(n, INT_MAX));
free(buf);
// ...
fp_print_data* fpdata2 = fp_print_data_from_data((unsigned char*)byteArray.data(), byteArray.size());
// ...
fp_print_data_free(fpdata2);
On Monday 07 March 2011 04:54:55 Nicholas Shatokhin wrote:
> Hello.
>
> I found this in structure:
>
> enum fp_print_data_type {
> PRINT_DATA_RAW = 0, /* memset-imposed default */
> PRINT_DATA_NBIS_MINUTIAE,
> };
>
> struct fp_print_data {
> uint16_t driver_id;
> uint32_t devtype;
> enum fp_print_data_type type;
> size_t length;
> unsigned char data[0]; // OK, it's array from one element
> };
>
> But when I enroll my finger, the data has the length 2404 elements. How it
> change it's size?
>
> I'm porting libfprint to qt4 and I need transform this struct to
> QByteArray and back. I successfully write data to byteArray, but I have
> difficult with backward procedure. See the function:
>
>
> struct fp_print_data CFingerprintScanner::byteArrayToFpdata(QByteArray
> byteArray)
> {
> quint16 driver_id;
> quint32 devtype;
> quint32 type;
> quint64 length;
> char * data;
>
> struct fp_print_data fpdata;
>
> QBuffer buffer(&byteArray);
> buffer.open(QIODevice::ReadOnly);
>
> QDataStream in(&buffer);
>
> in >> driver_id;
> in >> devtype;
> in >> type;
> in >> length;
>
> data = (char *) malloc(length);
>
> in.readRawData(data, length);
>
> fpdata.driver_id = (uint16_t) driver_id;
> fpdata.devtype = (uint32_t) devtype;
> fpdata.type = (enum fp_print_data_type) type;
> fpdata.length = (size_t) length;
>
> // in this I must write data to fpdata.data, but how can I do it?
> // it's not possible to convert char * (or unsigned char*) to
> unsigned
> char[0]
>
> free(data);
>
> return fpdata;
> }
>
> I'm disappointed. Somebody can tell me how can I change size of this array?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110307/7c28078e/attachment.bin
More information about the Qt-interest-old
mailing list