|
But in that way, I have to waist my resources. Because in some case(I mean in a single iteration) full buffer is not use.
I appreciate your help all the time...
Eranga
|
|
|
|
|
Could you please explain the situation ?
Regards,
Paresh.
|
|
|
|
|
Sure, here it is.
Initially I've used a predefined buffer of size 5000 as follows.
char buffer[5000];
Then on my application I found that the length of a bytes stream and going to read it using the buffer. Sometimes the stream is too small, and in some cases it is too long. So I want to use a dynamic buffer to avoid wasting memory.
I appreciate your help all the time...
Eranga
|
|
|
|
|
Try using realloc .
Regards,
Paresh.
|
|
|
|
|
hai,
iam not sure weather the following logic suits your application but you can try it.
1.at the start of your loop, as you are caluculating the length, allocate that many number of bytes.
2.use them however you want them.
3.at the end of the loop, release them i mean, de allocate (free) them.
as far as i know, once the loop ends, the memory allocated should automatically be freed.
good luck.
|
|
|
|
|
Sure, the thing I found difficult is that how to create a dynamic buffer. Now it is ok.
I appreciate your help all the time...
Eranga
|
|
|
|
|
What about...
void* pBuffer = NULL;
;
;
pBuffer = new BYTE [intSize];
;
;
delete [] pBuffer;
pBuffer = NULL;
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
|
|
|
|
|
Nelek wrote: void* pBuffer = NULL;
;
//Get your size into your int
;
pBuffer = new BYTE [intSize];
I have a question here. If I'm correct buffer data type is byte, but in initialization you have use it as void. I'm confusing on it, can you explain it little more.
Nelek wrote: pBuffer = NULL;
Here, if I use this within a loop I don't want to do this, right?
I appreciate your help all the time...
Eranga
|
|
|
|
|
Eranga Thennakoon wrote: but in initialization you have use it as void
There is no wrong in doing so. A void pointer can point to a data of another type.
Regards,
Vijay.
God may not give us what we 'want', but he surely gives us what we 'need'.
|
|
|
|
|
That is interesting to me. So I can use buffers easily, I mean just define them as VOID and later I can decide the actual data type I'm going to use.
I appreciate your help all the time...
Eranga
|
|
|
|
|
That's quite dangerous to do so (specially when you delete the array). If you know the data type upfront, use a buffer of that type and stick to it, it will save you a lot of troubles.
|
|
|
|
|
Thanks for your advice. Actually I think it is better to use the buffer gain and again, that mean dynamically initialization and delete is after use. As you say, no need to deal with dangerous situations and better to work with in right way.
Reuse of the buffer will helps me to work fine with the memory.
I appreciate your help all the time...
Eranga
|
|
|
|
|
Technically yes, but any time you need to read from or write to the void* , you'd have to cast it first. That's ugly and inefficient.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
DavidCrow wrote: you'd have to cast it first. That's ugly and inefficient.
I agreed with you. When I deal with such instances easily can be misses those casting and stuff. Better to avoid such points in developments.
I appreciate your help all the time...
Eranga
|
|
|
|
|
The intialization in void is correct, you can after cast to other types.
For the second question...
If you are changing the size for every iteration... you must do it at the last line of your loop.
void* pBuffer = NULL;
for (int i = 0; i < numFiles; i++)
{
CFile f (a_szFileName[i], CFile::modeCreate | CFile::modeWrite);
pBuffer = new BYTE [(a_differentSizes[i])];
f.Write (pBuffer, (a_differentSizes[i] * sizeof (BYTE)));
delete [] pBuffer;
pBuffer = NULL;
}
If you are using a simple iteration to go through the buffer, you should logically write the creation and the deletion at the beggining and at the end of your working area respectively, not into the loop.
void* pBuffer = NULL;
pBuffer = new BYTE [nBufferSize];
for (int i = 0; i < nBufferSize; i++)
{
if (*(pBuffer + i) == "F")
}
delete [] pBuffer;
pBuffer = NULL;
was it what you wanted to know?
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
|
|
|
|
|
Nelek wrote: The intialization in void is correct, you can after cast to other types.
Please if you can read my last replay for Cedric Moonen post. I have a suggestion.
Nelek wrote: pBuffer = NULL;
Actually my question is that, why you do this after free/delete the buffer. Is it required.
I appreciate your help all the time...
Eranga
|
|
|
|
|
I have read Cedric's answer. I agree, but this doesn't mean is incorrect. If I know the format, I usually use the format. The code above was an example.
About the NULL after deleting, it actually is not a must, but it doesn't disturb at all, and I use it so, maybe to ensure that if I reuse the buffer, it won't go trhough ASSERT (pBuffer) by mistake because it still has the last address (although no datas in).
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
|
|
|
|
|
Nelek wrote: About the NULL after deleting, it actually is not a must, but it doesn't disturb at all, and I use it so, maybe to ensure that if I reuse the buffer, it won't go trhough ASSERT (pBuffer) by mistake because it still has the last address (although no datas in).
That is really interesting pal. Actually those kind of hints are really helpful in a large project. Because I wont miss anything and not cause any damage in the future developments.
I appreciate your help all the time...
Eranga
|
|
|
|
|
Hi,
How to get the date and time of when the history was cleared in the PC.
Thanks in advance
|
|
|
|
|
??
time_t GetTime( ) const;
but, what do u mean with that "history"?
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
|
|
|
|
|
I want to know the time when the IE history was cleared in the pc.
|
|
|
|
|
I don't know how to get it, sorry. IExplorer is a big unknown for me (internal programming) yet.
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
|
|
|
|
|
|
Do you know for a fact if it is actually stored somewhere, or are you just hoping/guessing?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|