do you think the following code could lead to memory leaks? it creates a
circualr buffer made up of 5 element then it pushes 10 elements (overwriting
the first 5) when overwriting , do you think the eldest pointed memory will
be overwritten, or a new pointer in memory will be written?
Code:
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <boost/circular_buffer.hpp>
using namespace std;
using namespace boost;
char * getDateTime(void);
const short numbuff = 5;
const short buflen = 30;
typedef struct
{
unsigned char * pData;
unsigned short bufferLength;
unsigned short bytesRecorded;
bool flag;
} Buffer;
int main()
{
circular_buffer<Buffer*> cb(numbuff);
circular_buffer<Buffer*>::const_iterator it;
printf("Push elements:\n");
// fill buffer
for(int i = 0; i<10; i++)
{
// set up buffer
Buffer *buff = new Buffer;
ZeroMemory(buff, sizeof(Buffer));
buff->bufferLength = buflen;
buff->bytesRecorded = buflen;
buff->flag = true;
buff->pData = new unsigned char[buflen];
buff->pData = reinterpret_cast<unsigned char *>(getDateTime());
printf("%s\n", buff->pData);
// push buffer
cb.push_back(buff);
Sleep(1000);
}
printf("\nShow elements:\n");
// show elements
for(int i = 0; i<static_cast<int>(cb.size()); i++)
{
printf("%s\n", cb[i]->pData);
}
system("pause");
return EXIT_SUCCESS;
}
// getDateTime (Fri, 10 Oct 2008 14:41:59 GMT)
char * getDateTime(void)
{
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = gmtime(&rawtime);
char * buffer = new char[30];
strftime(buffer,30,"%a, %d %b %Y %X GMT",timeinfo);
return buffer;
}
buff->pData = new unsigned char[buflen];
buff->pData = reinterpret_cast<unsigned char *>(getDateTime());
This leaks. pData is overwritten the 2nd time so the memory you created the 1st time is gone.
Also... who frees the memory that 'getDateTime' has created.
This leaks. pData is overwritten the 2nd time so the memory you created the 1st time is gone.
Also... who frees the memory that 'getDateTime' has created.
by the way, how can I change getDateTime() to return char[30] ?? I mean the return type...
Looks like you aren't taking advantage of c++ at all. this looks like c with classes approach with boost?
Code:
#include <windows.h>
#include <ctime>
#include <string>
#include <iostream>
#include <boost/circular_buffer.hpp>
#include <boost/foreach.hpp>
using namespace std;
using namespace boost;
string getDateTime()
{
time_t rawtime = time(NULL);
tm* timeinfo = gmtime(&rawtime);
char szTime[30];
strftime(szTime, 30, "%a, %d %b %Y %X GMT", timeinfo);
return szTime;
}
struct Buffer
{
string payload;
int user;
Buffer() : payload(), user() { }
Buffer( const string& payload, int user ) : payload(payload), user(user) { }
};
int main()
{
const int numbuff = 3;
circular_buffer<Buffer> cb(numbuff);
// Insert elements
cout << "Push elements:" << endl;
for( int i = 0; i < 5; i++ )
{
cb.push_back( Buffer( getDateTime(), i ) );
Sleep(1000);
}
// Show elements:
cout << "Show elements:" << endl;
BOOST_FOREACH( Buffer Record, cb )
cout << Record.payload << endl;
system("pause");
return 0;
}
This is more c++ than the c approach , but understanding how and why to use string, constructors, containers and etc will take time. If this isn't what you require, just state what is wrong with the above code.
This is more c++ than the c approach , but understanding how and why to use string, constructors, containers and etc will take time. If this isn't what you require, just state what is wrong with the above code.
I might deal with /char/ (or unsigned char) data returned by some other functions....
I know I could cast to char to string...Yet, I'd like to stick with char for the moment
I mean, binary string mostly (unsigned char) the getDateTime() is just a way to fill the buffer with something for the moment...in the near future I'll be dealing with binary string only in my application
This is more c++ than the c approach , but understanding how and why to use string, constructors, containers and etc will take time. If this isn't what you require, just state what is wrong with the above code.
Bookmarks