Re: memory leaks
In message <4b5dcf0e$0$819$4fafbaef@reader5.news.tin.it>, Larry
<dontmewithme@got.it> writes
"Bo Persson" <bop@gmb.dk> ha scritto nel messaggio
news:7s60mgFd8jU1@mid.individual.net...
[aside: why is everyone posting low-level C code this week?]
So, if it is not a string you could try std::vector<unsigned char>
instead. That's a good type for a byte buffer.
That's what I have been looking for!
You're using strftime and printf("%s") on it. That looks like a string
to me.
Anyway now I have trouble with the following:
void getDateTime(char * szTime);
Can't you make this into a function that returns a std::string?
const int numbuff = 5;
const int buflen = 30;
struct Buffer
{
public:
vector<char> vChar;
unsigned int bufferLength;
unsigned int bytesRecorded;
What's the point of bufferLength and bytesRecorded now? vector does its
own housekeeping, so there's no need for you to.
Buffer() : bytesRecorded(0), bufferLength(0), vChar(NULL) { };
Lose that. vector's default constructor will work fine.
};
int main()
{
circular_buffer<Buffer> cb(numbuff);
circular_buffer<Buffer>::const_iterator it;
You declare these but never use them.
for(int i = 0; i<10; i++)
{
And you do this 10 times for no good reason. I think you've lost track
of the need for a circular buffer somewhere along the way ;-(
// Get time
char szTime[30];
Is that 30 the same as the "buflen" you declared earlier?
Why the Hungarian prefix?
getDateTime(szTime);
// Init Buff
Buffer buff;
ZeroMemory(&buff, sizeof(Buffer));
Don't do that. Buffer's constructor should do all you need to do to
initialise it.
buff.vChar.resize(buflen);
Don't do that. Assigning to the buffer will do all that's necessary.
buff.vChar = szTime;
buff.vChar.assign(szTime, szTime+strlen(szTime));
Better still, give Buffer a constructor with appropriate arguments which
initialises the buffer.
buff.bufferLength = buflen;
buff.bytesRecorded = buflen;
Redundant. Just use buff.vChar.size();
printf("%s\n", buff.vChar);
printf("%s\n", &buff.vChar[0]);
}
system("pause");
return EXIT_SUCCESS;
}
// getDateTime (Fri, 10 Oct 2008 14:41:59 GMT)
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);
Is that "30" the same as the "buflen" you declared earlier?
}
The code fails here: buff.vChar = szTime;
If you'd used string instead of vector<char> it would have worked ;-)
Because it's such a common operation, string (unlike vector) has an
assignment operator that takes a pointer to a C-style null-terminated
string.
???
Try "Accelerated C++".
thanks
--
Richard Herring