Commit c6b3d0876cb291fbb063969a3e7734e51eee3b9f
Merge pull request #160 from biometrics/no_alloc
Alternative for #159, avoid some dynamic memory allocations
Showing
1 changed file
with
12 additions
and
11 deletions
openbr/plugins/stream.cpp
| ... | ... | @@ -347,10 +347,11 @@ public: |
| 347 | 347 | } |
| 348 | 348 | |
| 349 | 349 | // first 4 bytes store 0xEDFE, next 24 store 'Norpix seq ' |
| 350 | - char *firstFour = new char[4], *nextTwentyFour; | |
| 350 | + char firstFour[4]; | |
| 351 | 351 | seqFile.seekg(0, ios::beg); |
| 352 | 352 | seqFile.read(firstFour, 4); |
| 353 | - nextTwentyFour = readText(24); | |
| 353 | + char nextTwentyFour[24]; | |
| 354 | + readText(24, nextTwentyFour); | |
| 354 | 355 | if (firstFour[0] != (char)0xED || firstFour[1] != (char)0xFE || strncmp(nextTwentyFour, "Norpix seq", 10) != 0) { |
| 355 | 356 | qDebug("Invalid header in seq file"); |
| 356 | 357 | return false; |
| ... | ... | @@ -363,7 +364,8 @@ public: |
| 363 | 364 | qDebug("Invalid header size"); |
| 364 | 365 | return false; |
| 365 | 366 | } |
| 366 | - char *desc = readText(512); | |
| 367 | + char desc[512]; | |
| 368 | + readText(512, desc); | |
| 367 | 369 | basis.file.set("Description", QString(desc)); |
| 368 | 370 | |
| 369 | 371 | width = readInt(); |
| ... | ... | @@ -414,9 +416,9 @@ public: |
| 414 | 416 | // but there might be 16 extra bytes instead of 8... |
| 415 | 417 | if (i == 1) { |
| 416 | 418 | seqFile.seekg(s, ios::beg); |
| 417 | - char *zero = new char[1]; | |
| 418 | - seqFile.read(zero, 1); | |
| 419 | - if (zero[0] == 0) { | |
| 419 | + char zero; | |
| 420 | + seqFile.read(&zero, 1); | |
| 421 | + if (zero == 0) { | |
| 420 | 422 | s += 8; |
| 421 | 423 | extra += 8; |
| 422 | 424 | } |
| ... | ... | @@ -498,14 +500,13 @@ private: |
| 498 | 500 | // apparently the text in seq files is 16 bit characters (UTF-16?) |
| 499 | 501 | // since we don't really need the last byte, snad since it gets interpreted as |
| 500 | 502 | // a terminating char, let's just grab the first byte for storage |
| 501 | - char* readText(int bytes) | |
| 503 | + void readText(int bytes, char * buffer) | |
| 502 | 504 | { |
| 503 | - char *text = new char[bytes], *ret = new char[bytes/2]; | |
| 504 | - seqFile.read(text, bytes); | |
| 505 | + seqFile.read(buffer, bytes); | |
| 505 | 506 | for (int i=0; i<bytes; i+=2) { |
| 506 | - ret[i/2] = text[i]; | |
| 507 | + buffer[i/2] = buffer[i]; | |
| 507 | 508 | } |
| 508 | - return ret; | |
| 509 | + buffer[bytes/2] = '\0'; | |
| 509 | 510 | } |
| 510 | 511 | |
| 511 | 512 | protected: | ... | ... |