Best way of representing binary data

Peter Lakatoš

I am writing a Qt C++ application where I am reading a pcap file and interpreting packets described in given file. As I am reading packet by packet, I need to somehow store binary data. It needs to be flexible enough so I can pass it across multiple functions as parameter and being able to work with it as char*. It also should be safe enough to avoid memory leaks.

I came up with 2 approaches:

1. unique_ptr<char[]>

So basically I can use std::ifstream for reading from file and storing binary data in std::unique_ptr<char[]> like so :

std::ifstream ifs(fullFilename.c_str(), std::fstream::binary | std::fstream::in, _SH_DENYNO);
std::unique_ptr<char[]> buff = std::make_unique<char[]>(65535); //size is set according to pcap_hdr_s.snaplen
ifs.read(buff.get(), 65535);

And then passing it to function via raw pointer (void f(const unsigned char* p) => f((unsigned char*)buff.get())) or via reference (void f(std::unique_ptr<char[]>& p) => f(buff))

2. QByteArray

In this approach, I would use QFile for reading from file and storing data in QByteArray like this :

QFile file(fileName.c_str());
QByteArray buff;
buff.resize(65535);
file.read(buff.data(), 65535);

And then passing it to the functions via reference ( void f(QByteArray& buff) => f(buff) ).

Which approach is better ? Or do you have some better way of doing this ?

ypnos

Some notes about Qt:

  1. You can use QByteArray QIODevice::readAll() directly to obtain a buffer:
    QFile file(fileName.c_str());
    auto buff = file.readAll();
    
  2. QByteArray is implicite sharing / copy-on-write. So while you can pass by reference, you should either pass by const reference or simply pass by value.

Your solution with std::unique_ptr<> looks like a good thing to do, but also a bit cumbersome. If your project is already heavily using Qt, I would go for the Qt route.

Passing a reference to a unique_ptr is also quite unexpected. Either pass the raw pointer/reference (for example to a helper method, that will return while you hold the unique_ptr), or by moving the unique_ptr (if you want to transfer ownership). See an explanation here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Pandas - is there an 'out the box' way to combine boolean columns representing enumerated data?

From Dev

Best way to sort data

From Dev

Git and binary data, best storage method

From Dev

Best way to handle disposable data

From Dev

Best way to build this data structure

From Dev

best way to store data in session

From Dev

What is the best way to parse binary protocols with Rust

From Dev

What is the best data structure for representing an upper triangular matrix in Java?

From Dev

Best way to encode data

From Dev

Best way to import data in Swift?

From Dev

What is the best way of deleting a section of a binary file in Java 7

From Dev

What is the best way to evaluate two variables representing a single pipeline command in bash?

From Dev

Best way to model data in Firebase

From Dev

Best way to merge binary files in Java

From Dev

Best way of representing polygonal chain

From Dev

Best way in Java to convert a double (representing a price) to a long with a price scale of 8

From Dev

Best way to access data in react

From Dev

Best way of representing binary data

From Dev

Best way to save data in android

From Dev

Best way for store data into client

From Dev

Best way to deal with xyz data

From Dev

Binary search works best with which data structure?

From Dev

Best way to learn data structures

From Dev

Best way to store JSON data in to Core Data

From Dev

Best way to import data in Swift?

From Dev

Best way to merge binary files in Java

From Dev

Best way to aggregate logging data

From Dev

Best way to transform data

From Dev

Best way to persist data?