Credo che l'unico modo per non avere memory leaks sia la seguente:
codice:
#include <windows.h>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <boost/circular_buffer.hpp>
using namespace std;
using namespace boost;
void getDateTime(char * szTime);
const int numbuff = 3;
const int buflen = 30;
struct Buffer
{
public:
char payload[4096];
int bufferLength;
int bytesRecorded;
int user;
Buffer() : bufferLength(0), bytesRecorded(0), user(0) { }
};
int main()
{
circular_buffer<Buffer> cb(numbuff);
// Insert elements
printf("Push elements:\n");
for(int i = 0; i<5; i++)
{
// Get time
char szTime[30]; getDateTime(szTime);
// Init Buff
Buffer buff;
ZeroMemory(&buff, sizeof(Buffer));
memcpy(static_cast<void*>(buff.payload), szTime, buflen);
buff.user = i;
buff.bufferLength = buflen;
buff.bytesRecorded = buflen;
cb.push_back(buff);
printf("%s\n", buff.payload);
Sleep(1000);
}
// Show elements:
printf("Show elements:\n");
for(int i = 0; i<(int)cb.size(); i++)
{
printf("%s\n", cb[i].payload);
}
system("pause");
return EXIT_SUCCESS;
}
void getDateTime(char * szTime)
{
time_t rawtime = time(NULL);
struct tm timeinfo;
gmtime_s(&timeinfo, &rawtime);
strftime(szTime, 30, "%a, %d %b %Y %X GMT", &timeinfo);
}
dove char payload[4096] e settato ad un numero grande come 4096, ogni volta il buffer sarà nettamente minore...ma almeno so che posso far affidamento ad un buffer di lunghezza fino a 4096 bytes...userà poi memcpy per copiare dalla sorgente a payload (senza aggiungere null etc...) gli altri campi della struttura mi diranno quanto è veramente lunga la stringa...
che ne dite??