473,394 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

memory pool?

I have a situation where I'm getting a picture from a camera in small
chunks.
So the photo is a few MBytes and I have a callback function that returns
1KB until it finishes transfering the picture.
Any suggestions or examples how should I put a picture in one variable?
I heard something about using memory pools for this but I only need
something simple for this occasion.
Could I use CString?

Anyway thanks in advance for any suggestions.
Nov 10 '08 #1
20 1651
Uli Kunkel <ge*******@yahoo.comwrites:
I have a situation where I'm getting a picture from a camera in small
chunks.
So the photo is a few MBytes and I have a callback function that
returns 1KB until it finishes transfering the picture.
Any suggestions or examples how should I put a picture in one variable?
I heard something about using memory pools for this but I only need
something simple for this occasion.
Could I use CString?
Not on comp.lang.c++. For CString, you should ask on comp.lang.c.

Anyway thanks in advance for any suggestions.

typedef unsigned char byte;
const int KByte=1024;
const int MByte=1024*KByte;
const int few=6;
std::vector<byteimage(few*MByte,0);

Then you can receive copy small buffers into it:
std::vector<byte>::iterator pos=image.begin();
std::vector<bytebuffer(1*KByte);

while(receive(&buffer)){
pos=std::copy(buffer.begin(),buffer.end(),pos);
}
--
__Pascal Bourguignon__
Nov 10 '08 #2
Uli Kunkel escribió:
I have a situation where I'm getting a picture from a camera in small
chunks.
So the photo is a few MBytes and I have a callback function that returns
1KB until it finishes transfering the picture.
Any suggestions or examples how should I put a picture in one variable?
I heard something about using memory pools for this but I only need
something simple for this occasion.
Could I use CString?

Anyway thanks in advance for any suggestions.

Use a vector, taking advantage from its reserve() function.

#include <vector>

(...)

std::vector<unsigned charmyPhoto;
myPhoto.reserve(1024*1024);

for (size_t part=0; part !=1024;++part) {
<read image data into &myPhoto(part*1024) >
}
(...)
<you now have your photo in myPhoto.size() sequential bytes at
&myPhoto[0]
Just customize such piece for your exact needs, substsituting all magic
constants with nice real constants.
This code skeleton takes as granted that char size is one byte, you may
need to correct the code it it is otherwise

best regards,

Zara
Nov 10 '08 #3
Juan Antonio Zaratiegui Vallecillo wrote:
Uli Kunkel escribió:
>I have a situation where I'm getting a picture from a camera in small
chunks.
So the photo is a few MBytes and I have a callback function that returns
1KB until it finishes transfering the picture.
Any suggestions or examples how should I put a picture in one variable?
I heard something about using memory pools for this but I only need
something simple for this occasion.
Could I use CString?

Anyway thanks in advance for any suggestions.


Use a vector, taking advantage from its reserve() function.

#include <vector>

(...)

std::vector<unsigned charmyPhoto;
myPhoto.reserve(1024*1024);

for (size_t part=0; part !=1024;++part) {
<read image data into &myPhoto(part*1024) >
This should read "append data to 'myPhoto'". Remember, reserved
memory is just that: reserved memory. Specifically, it's not
objects you could overwrite.
[...]
Zara
Schobi
Nov 10 '08 #4
Juan Antonio Zaratiegui Vallecillo wrote:
Uli Kunkel escribió:
>I have a situation where I'm getting a picture from a camera in small
chunks.
So the photo is a few MBytes and I have a callback function that
returns 1KB until it finishes transfering the picture.
Any suggestions or examples how should I put a picture in one variable?
I heard something about using memory pools for this but I only need
something simple for this occasion.
Could I use CString?

Anyway thanks in advance for any suggestions.


Use a vector, taking advantage from its reserve() function.

#include <vector>

(...)

std::vector<unsigned charmyPhoto;
myPhoto.reserve(1024*1024);

for (size_t part=0; part !=1024;++part) {
<read image data into &myPhoto(part*1024) >
}
(...)
<you now have your photo in myPhoto.size() sequential bytes at
&myPhoto[0]
Just customize such piece for your exact needs, substsituting all magic
constants with nice real constants.
This code skeleton takes as granted that char size is one byte, you may
need to correct the code it it is otherwise

best regards,

Zara
Thanks for the answers.
I'm wondering if I could use reserve to allocate a few MB and then if
the size is not enough that the vector grows automatically?
I read that vector resizing is very costly so I presume that reserving
just 1KB is not enough.
Nov 10 '08 #5
On 10 Nov., 12:49, Uli Kunkel <genija...@yahoo.comwrote:
Juan Antonio Zaratiegui Vallecillo wrote:


Uli Kunkel escribió:
I have a situation where I'm getting a picture from a camera in small
chunks.
So the photo is a few MBytes and I have a callback function that
returns 1KB until it finishes transfering the picture.
Any suggestions or examples how should I put a picture in one variable?
I heard something about using memory pools for this but I only need
something simple for this occasion.
Could I use CString?
Anyway thanks in advance for any suggestions.
Use a vector, taking advantage from its reserve() function.
#include <vector>
(...)
* * std::vector<unsigned charmyPhoto;
* * myPhoto.reserve(1024*1024);
* * for (size_t part=0; part !=1024;++part) {
* * * *<read image data into &myPhoto(part*1024) >
* * }
(...)
* * <you now have your photo in myPhoto.size() sequential bytes at
&myPhoto[0]
Just customize such piece for your exact needs, substsituting all magic
constants with nice real constants.
This code skeleton takes as granted that char size is one byte, you may
need to correct the code it it is otherwise
best regards,
Zara

Thanks for the answers.
I'm wondering if I could use reserve to allocate a few MB and then if
the size is not enough that the vector grows automatically?
I read that vector resizing is very costly so I presume that reserving
just 1KB is not enough.
std::vector resizes autmatically. As an alternative, you could also
use std::deque which memorywise is more optimal with regard to
reallocation but overall is slightly slower (does more allocations)
than a std::vector.

/Peter
Nov 10 '08 #6
Uli Kunkel wrote:
[...]
I'm wondering if I could use reserve to allocate a few MB and then if
the size is not enough that the vector grows automatically?
I read that vector resizing is very costly so I presume that reserving
just 1KB is not enough.
There's a difference between a vector's size (member functions
'size()'/'resize()') and its capacity ('capacity()'/'reserve()').
The former is related to the number of actual objects in the
vector, the latter to the number of objects the vector is able
to store without having to reallocate.
When you tell a vector to reserve memory for an amount of
elements, it allocates the memory needed (or more), but does
not create the objects. When you tell it to resize to a
specific amount of objects, it will actually contain this
amount of constructed objects (while its capacity might be
higher). The capacity concept allows to write code adding
objects to a vector in the most obvious way (keep appending)
and, if performance or memory fragmentation etc. are issues,
reserve before-hand to reduce or eliminate reallocations. It's
only there for performance-reasons and (besides performance)
has no influence on your algorithm.

So when you know how many objects you will need before-hand,
reserve that amount and then keep appending incoming objects.
If not, strategies depend on your specific problem.

Schobi
Nov 10 '08 #7
Pascal J. Bourguignon wrote:
Uli Kunkel <ge*******@yahoo.comwrites:
>I have a situation where I'm getting a picture from a camera in small
chunks.
So the photo is a few MBytes and I have a callback function that
returns 1KB until it finishes transfering the picture.
Any suggestions or examples how should I put a picture in one variable?
I heard something about using memory pools for this but I only need
something simple for this occasion.
Could I use CString?

Not on comp.lang.c++. For CString, you should ask on comp.lang.c.

>Anyway thanks in advance for any suggestions.


typedef unsigned char byte;
const int KByte=1024;
const int MByte=1024*KByte;
const int few=6;
std::vector<byteimage(few*MByte,0);

Then you can receive copy small buffers into it:
std::vector<byte>::iterator pos=image.begin();
std::vector<bytebuffer(1*KByte);

while(receive(&buffer)){
pos=std::copy(buffer.begin(),buffer.end(),pos);
}

I wrote some code and I have a problem that I can't open the picture.
Here is the simplified code:
----------------------------
//This part of the code pushes back every character to myPhoto vector

for(unsigned long i=0; i < Length; i++)
{
myPhoto.push_back(srcData[i]);
}
if(complete) //Write myPhoto vector to a file
{
ofstream outf("C:\\test_0.jpg");
for ( pos=myPhoto.begin()
; pos!=myPhoto.end()
; ++pos)
{
outf << *pos;
}
outf.close();
}

//This part of the code append every chunk to the file
//It's for testing only
ofstream myFile ("C:\\test_1.jpg", ios::out | ios::binary | ios::app);
myFile.write ((char *)pProgress->pbData, pProgress->lLength);
myFile.close();
-----------------------------
What happens is that the first photo has 8 or 9 KB more than the second
one and it can't be opened.
The second picture is ok but I don't want to do it this way.
Also I opened them and they seem the samo at the begining but the first
one is larger..

I tried 2 different ways to writing them but with the same effect.

Any suggestions?
Nov 10 '08 #8
Uli Kunkel wrote:
I wrote some code and I have a problem that I can't open the picture.
Here is the simplified code:
----------------------------
//This part of the code pushes back every character to myPhoto vector

for(unsigned long i=0; i < Length; i++)
{
myPhoto.push_back(srcData[i]);
}
if(complete) //Write myPhoto vector to a file
{
ofstream outf("C:\\test_0.jpg");
for ( pos=myPhoto.begin()
; pos!=myPhoto.end()
; ++pos)
{
outf << *pos;
}
outf.close();
}

//This part of the code append every chunk to the file
//It's for testing only
ofstream myFile ("C:\\test_1.jpg", ios::out | ios::binary | ios::app);
myFile.write ((char *)pProgress->pbData, pProgress->lLength);
myFile.close();
-----------------------------
What happens is that the first photo has 8 or 9 KB more than the second
one and it can't be opened.
The first file is opened in text mode, while the second is opened in binary
mode. Depending on the platform you're using, that means that the first file
might get altered in some way while being written.

Nov 10 '08 #9
Rolf Magnus wrote:
Uli Kunkel wrote:
>I wrote some code and I have a problem that I can't open the picture.
Here is the simplified code:
----------------------------
//This part of the code pushes back every character to myPhoto vector

for(unsigned long i=0; i < Length; i++)
{
myPhoto.push_back(srcData[i]);
}
if(complete) //Write myPhoto vector to a file
{
ofstream outf("C:\\test_0.jpg");
for ( pos=myPhoto.begin()
; pos!=myPhoto.end()
; ++pos)
{
outf << *pos;
}
outf.close();
}

//This part of the code append every chunk to the file
//It's for testing only
ofstream myFile ("C:\\test_1.jpg", ios::out | ios::binary | ios::app);
myFile.write ((char *)pProgress->pbData, pProgress->lLength);
myFile.close();
-----------------------------
What happens is that the first photo has 8 or 9 KB more than the second
one and it can't be opened.

The first file is opened in text mode, while the second is opened in binary
mode. Depending on the platform you're using, that means that the first file
might get altered in some way while being written.
That was the problem.
I didn't pay much attention on that part..
Thanks for the help.
Nov 10 '08 #10
Juan Antonio Zaratiegui Vallecillo escribió:
Uli Kunkel escribió:
>I have a situation where I'm getting a picture from a camera in small
chunks.
So the photo is a few MBytes and I have a callback function that
returns 1KB until it finishes transfering the picture.
Any suggestions or examples how should I put a picture in one variable?
I heard something about using memory pools for this but I only need
something simple for this occasion.
Could I use CString?

Anyway thanks in advance for any suggestions.


Use a vector, taking advantage from its reserve() function.

#include <vector>

(...)

std::vector<unsigned charmyPhoto;
myPhoto.reserve(1024*1024);

for (size_t part=0; part !=1024;++part) {
<read image data into &myPhoto(part*1024) >
ThankÅ› to all for the comments.

I really meant to say
<append imaged data into myPhoto.end()>
But it was too early in the morning
}
(...)
<you now have your photo in myPhoto.size() sequential bytes at
&myPhoto[0]

(...)

And I keep on favouring the use of reserve, as it will be surely faster
than letting the container resize as needed, because we already know the
final size.

Best regards,

Zara

PS: Any error in this new message,, it is because it is too late in the
afternoon ;-)
Nov 10 '08 #11
Juan Antonio Zaratiegui Vallecillo wrote:
Juan Antonio Zaratiegui Vallecillo escribió:
>Uli Kunkel escribió:
>>I have a situation where I'm getting a picture from a camera in small
chunks.
So the photo is a few MBytes and I have a callback function that
returns 1KB until it finishes transfering the picture.
Any suggestions or examples how should I put a picture in one variable?
I heard something about using memory pools for this but I only need
something simple for this occasion.
Could I use CString?

Anyway thanks in advance for any suggestions.


Use a vector, taking advantage from its reserve() function.

#include <vector>

(...)

std::vector<unsigned charmyPhoto;
myPhoto.reserve(1024*1024);

for (size_t part=0; part !=1024;++part) {
<read image data into &myPhoto(part*1024) >
ThankÅ› to all for the comments.

I really meant to say
<append imaged data into myPhoto.end()>
But it was too early in the morning
> }
(...)
<you now have your photo in myPhoto.size() sequential bytes at
&myPhoto[0]

(...)

And I keep on favouring the use of reserve, as it will be surely faster
than letting the container resize as needed, because we already know the
final size.

Best regards,

Zara

PS: Any error in this new message,, it is because it is too late in the
afternoon ;-)
In the end I did something like this:
First I reserved 2 MB. Then I check the capacyty and reserve a 1KB more
if needed:

if(myPhoto.size() + pProgress->lLength < myPhoto.capacity())
myPhoto.reserve(1024*1024);

Thank you all for your help.
Nov 10 '08 #12
Pascal J. Bourguignon wrote:
Uli Kunkel <ge*******@yahoo.comwrites:
I have a situation where I'm getting a picture from a camera in
small chunks.
So the photo is a few MBytes and I have a callback function that
returns 1KB until it finishes transfering the picture.
Any suggestions or examples how should I put a picture in one
variable? I heard something about using memory pools for this but
I only need something simple for this occasion.
Could I use CString?

Not on comp.lang.c++. For CString, you should ask on comp.lang.c.
No, he should not. CString is not a C construct, but rather an MFC
class.


Brian
Nov 10 '08 #13
On Nov 10, 12:49*pm, Uli Kunkel <genija...@yahoo.comwrote:
Juan Antonio Zaratiegui Vallecillo wrote:
I'm wondering if I could use reserve to allocate a few MB and
then if the size is not enough that the vector grows
automatically? I read that vector resizing is very costly so
I presume that reserving just 1KB is not enough.
What you read was wrong. Resizing isn't that costly for
primitive object types (like unsigned char). There are other
reasons you might want to avoid it, but they probably don't
apply here.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 11 '08 #14
James Kanze wrote:
On Nov 10, 12:49 pm, Uli Kunkel <genija...@yahoo.comwrote:
>Juan Antonio Zaratiegui Vallecillo wrote:
I'm wondering if I could use reserve to allocate a few MB and
then if the size is not enough that the vector grows
automatically? I read that vector resizing is very costly so
I presume that reserving just 1KB is not enough.

What you read was wrong. Resizing isn't that costly for
primitive object types (like unsigned char). There are other
reasons you might want to avoid it, but they probably don't
apply here.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
I tried 2 things:

1. reserve 3MB and then push_back every unsigned char (or byte) to the
vector.
2. Reserve 1KB on every callback

U didn't measure the time precisely, but it is approximatly the same.
It takes very long to complete, around 10 seconds.
This is to much.I don't know what the problem is.
Nov 11 '08 #15
On 11 Nov., 11:21, Uli Kunkel <genija...@yahoo.comwrote:
James Kanze wrote:
On Nov 10, 12:49 pm, Uli Kunkel <genija...@yahoo.comwrote:
Juan Antonio Zaratiegui Vallecillo wrote:
I'm wondering if I could use reserve to allocate a few MB and
then if the size is not enough that the vector grows
automatically? *I read that vector resizing is very costly so
I presume that reserving just 1KB is not enough.
What you read was wrong. *Resizing isn't that costly for
primitive object types (like unsigned char). *There are other
reasons you might want to avoid it, but they probably don't
apply here.
--
James Kanze (GABI Software) * * * * * * email:james.ka...@gmail.com
Conseils en informatique orientée objet/
* * * * * * * * * *Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

I tried 2 things:

1. reserve 3MB and then push_back every unsigned char (or byte) to the
vector.
2. Reserve 1KB on every callback
That is the wrong approach. You either reserve once at the beginning
or you don't reserve at all. Reserving in small chunks will cause your
algorithm to run much slower than without any reserves at all.
>
U didn't measure the time precisely, but it is approximatly the same.
It takes very long to complete, around 10 seconds.
This is to much.I don't know what the problem is.
That is far to much - something else must be wrong. You can isolate
the vector code by simply appending "random data" to the vector
without reading it from the source. If that is not a snap, you should
post the codde here.

/Peter
Nov 11 '08 #16
peter koch wrote:
On 11 Nov., 11:21, Uli Kunkel <genija...@yahoo.comwrote:
>James Kanze wrote:
>>On Nov 10, 12:49 pm, Uli Kunkel <genija...@yahoo.comwrote:
Juan Antonio Zaratiegui Vallecillo wrote:
I'm wondering if I could use reserve to allocate a few MB and
then if the size is not enough that the vector grows
automatically? I read that vector resizing is very costly so
I presume that reserving just 1KB is not enough.
What you read was wrong. Resizing isn't that costly for
primitive object types (like unsigned char). There are other
reasons you might want to avoid it, but they probably don't
apply here.
--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
I tried 2 things:

1. reserve 3MB and then push_back every unsigned char (or byte) to the
vector.
2. Reserve 1KB on every callback

That is the wrong approach. You either reserve once at the beginning
or you don't reserve at all. Reserving in small chunks will cause your
algorithm to run much slower than without any reserves at all.
>U didn't measure the time precisely, but it is approximatly the same.
It takes very long to complete, around 10 seconds.
This is to much.I don't know what the problem is.

That is far to much - something else must be wrong. You can isolate
the vector code by simply appending "random data" to the vector
without reading it from the source. If that is not a snap, you should
post the codde here.

/Peter
I agree that it is better to allocate a couple of MB than to allocate
1KB so many times.
People on the group said that the latter isn't such a big deal.
Anyway the code is simple:
-----------------------------
//first I allocate 3MB in some initialization
myPhoto.reserve(3*1024*1024);

//then I assign one char at a time
for(unsigned long i=0; i < pProgress->lLength; i++)
{
myPhoto.push_back(pProgress->pbData[i]);
}
-------------------------------

What do you think?
Maybe I should use insert, instead of putting one char at the time.
I tried this but pbData is unsigned char* type and could't do it...
Nov 11 '08 #17
Uli Kunkel wrote:
peter koch wrote:
>On 11 Nov., 11:21, Uli Kunkel <genija...@yahoo.comwrote:
>>James Kanze wrote:
On Nov 10, 12:49 pm, Uli Kunkel <genija...@yahoo.comwrote:
Juan Antonio Zaratiegui Vallecillo wrote:
I'm wondering if I could use reserve to allocate a few MB and
then if the size is not enough that the vector grows
automatically? I read that vector resizing is very costly so
I presume that reserving just 1KB is not enough.
What you read was wrong. Resizing isn't that costly for
primitive object types (like unsigned char). There are other
reasons you might want to avoid it, but they probably don't
apply here.
--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
I tried 2 things:

1. reserve 3MB and then push_back every unsigned char (or byte) to the
vector.
2. Reserve 1KB on every callback

That is the wrong approach. You either reserve once at the beginning
or you don't reserve at all. Reserving in small chunks will cause your
algorithm to run much slower than without any reserves at all.
>>U didn't measure the time precisely, but it is approximatly the same.
It takes very long to complete, around 10 seconds.
This is to much.I don't know what the problem is.

That is far to much - something else must be wrong. You can isolate
the vector code by simply appending "random data" to the vector
without reading it from the source. If that is not a snap, you should
post the codde here.

/Peter

I agree that it is better to allocate a couple of MB than to allocate
1KB so many times.
People on the group said that the latter isn't such a big deal.
Anyway the code is simple:
-----------------------------
//first I allocate 3MB in some initialization
myPhoto.reserve(3*1024*1024);

//then I assign one char at a time
for(unsigned long i=0; i < pProgress->lLength; i++)
{
myPhoto.push_back(pProgress->pbData[i]);
}
-------------------------------

What do you think?
Maybe I should use insert, instead of putting one char at the time.
I tried this but pbData is unsigned char* type and could't do it...
I had one big oversight.
In the callback function I can define the length of each chunk.I thought
it is integer type.As I recall that is only 36635.
I check again and saw it is actually unsigned long.That was very stupid
of me.
Now I pass 1MB and it takes 2,3 seconds which is much better.

If I'm still doing something wrong I would appreciate any help.

Nov 11 '08 #18
On 11 Nov., 12:49, Uli Kunkel <genija...@yahoo.comwrote:
Uli Kunkel wrote:
peter koch wrote:
On 11 Nov., 11:21, Uli Kunkel <genija...@yahoo.comwrote:
James Kanze wrote:
On Nov 10, 12:49 pm, Uli Kunkel <genija...@yahoo.comwrote:
Juan Antonio Zaratiegui Vallecillo wrote:
I'm wondering if I could use reserve to allocate a few MB and
then if the size is not enough that the vector grows
automatically? *I read that vector resizing is very costly so
I presume that reserving just 1KB is not enough.
What you read was wrong. *Resizing isn't that costly for
primitive object types (like unsigned char). *There are other
reasons you might want to avoid it, but they probably don't
apply here.
--
James Kanze (GABI Software) * * * * * * email:james.ka....@gmail.com
Conseils en informatique orientée objet/
* * * * * * * * * *Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 0034
I tried 2 things:
>1. reserve 3MB and then push_back every unsigned char (or byte) to the
vector.
2. Reserve 1KB on every callback
That is the wrong approach. You either reserve once at the beginning
or you don't reserve at all. Reserving in small chunks will cause your
algorithm to run much slower than without any reserves at all.
U didn't measure the time precisely, but it is approximatly the same.
It takes very long to complete, around 10 seconds.
This is to much.I don't know what the problem is.
That is far to much - something else must be wrong. You can isolate
the vector code by simply appending "random data" to the vector
without reading it from the source. If that is not a snap, you should
post the codde here.
/Peter
I agree that it is better to allocate a couple of MB than to allocate
1KB so many times.
People on the group said that the latter isn't such a big deal.
Anyway the code is simple:
-----------------------------
//first I allocate 3MB in some initialization
myPhoto.reserve(3*1024*1024);
//then I assign one char at a time
for(unsigned long i=0; i < pProgress->lLength; i++)
{
* * myPhoto.push_back(pProgress->pbData[i]);
No need to push_back each item individually. Better would be to use
std::vector.insert or std::copy with a back_inserter. Look these up.
}
-------------------------------
What do you think?
Maybe I should use insert, instead of putting one char at the time.
I tried this but pbData is unsigned char* type and could't do it...

I had one big oversight.
In the callback function I can define the length of each chunk.I thought
it is integer type.As I recall that is only 36635.
It is implementation defined, but i doubt max(int) is 36635 - it might
be 32767.
I check again and saw it is actually unsigned long.That was very stupid
of me.
Now I pass 1MB and it takes 2,3 seconds which is much better.

If I'm still doing something wrong I would appreciate any help.
See above.

/Peter
Nov 11 '08 #19
peter koch wrote:
On 11 Nov., 12:49, Uli Kunkel <genija...@yahoo.comwrote:
>Uli Kunkel wrote:
>>peter koch wrote:
On 11 Nov., 11:21, Uli Kunkel <genija...@yahoo.comwrote:
James Kanze wrote:
>On Nov 10, 12:49 pm, Uli Kunkel <genija...@yahoo.comwrote:
>>Juan Antonio Zaratiegui Vallecillo wrote:
>>I'm wondering if I could use reserve to allocate a few MB and
>>then if the size is not enough that the vector grows
>>automatically? I read that vector resizing is very costly so
>>I presume that reserving just 1KB is not enough.
>What you read was wrong. Resizing isn't that costly for
>primitive object types (like unsigned char). There are other
>reasons you might want to avoid it, but they probably don't
>apply here.
>--
>James Kanze (GABI Software) email:james.ka...@gmail.com
>Conseils en informatique orientée objet/
> Beratung in objektorientierter Datenverarbeitung
>9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
I tried 2 things:
1. reserve 3MB and then push_back every unsigned char (or byte) to the
vector.
2. Reserve 1KB on every callback
That is the wrong approach. You either reserve once at the beginning
or you don't reserve at all. Reserving in small chunks will cause your
algorithm to run much slower than without any reserves at all.
U didn't measure the time precisely, but it is approximatly the same.
It takes very long to complete, around 10 seconds.
This is to much.I don't know what the problem is.
That is far to much - something else must be wrong. You can isolate
the vector code by simply appending "random data" to the vector
without reading it from the source. If that is not a snap, you should
post the codde here.
/Peter
I agree that it is better to allocate a couple of MB than to allocate
1KB so many times.
People on the group said that the latter isn't such a big deal.
Anyway the code is simple:
-----------------------------
//first I allocate 3MB in some initialization
myPhoto.reserve(3*1024*1024);
//then I assign one char at a time
for(unsigned long i=0; i < pProgress->lLength; i++)
{
myPhoto.push_back(pProgress->pbData[i]);

No need to push_back each item individually. Better would be to use
std::vector.insert or std::copy with a back_inserter. Look these up.
>>}
-------------------------------
What do you think?
Maybe I should use insert, instead of putting one char at the time.
I tried this but pbData is unsigned char* type and could't do it...
I had one big oversight.
In the callback function I can define the length of each chunk.I thought
it is integer type.As I recall that is only 36635.

It is implementation defined, but i doubt max(int) is 36635 - it might
be 32767.
>I check again and saw it is actually unsigned long.That was very stupid
of me.
Now I pass 1MB and it takes 2,3 seconds which is much better.

If I'm still doing something wrong I would appreciate any help.

See above.

/Peter
I have a silly problem.
Insert takes const byte&. And variable pProgress->pbData that I pass is
unsigned char*.
Anyway it inserts an empty value, something is not right..
Nov 11 '08 #20
In message <gf**********@news.metronet.hr>, Uli Kunkel
<ge*******@yahoo.comwrites
>peter koch wrote:
>On 11 Nov., 12:49, Uli Kunkel <genija...@yahoo.comwrote:
>>Uli Kunkel wrote:
[...]
>>>I agree that it is better to allocate a couple of MB than to allocate
1KB so many times.
People on the group said that the latter isn't such a big deal.
Anyway the code is simple:
-----------------------------
//first I allocate 3MB in some initialization
myPhoto.reserve(3*1024*1024);
//then I assign one char at a time
for(unsigned long i=0; i < pProgress->lLength; i++)
{
myPhoto.push_back(pProgress->pbData[i]);
> No need to push_back each item individually. Better would be to use
std::vector.insert or std::copy with a back_inserter. Look these up.
>>>}
-------------------------------
What do you think?
Maybe I should use insert, instead of putting one char at the time.
I tried this but pbData is unsigned char* type and could't do it...
[...]
>>If I'm still doing something wrong I would appreciate any help.
See above.

I have a silly problem.
Insert takes const byte&. And variable pProgress->pbData that I pass is
unsigned char*.
Use the two-iterator form of insert:

myPhoto.insert(myPhoto.end(), pProgress->pBData, pProgress->pBData+pProgress->lLength);

Pointers are iterators.
>Anyway it inserts an empty value, something is not right..
--
Richard Herring
Nov 12 '08 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Tron Thomas | last post by:
As part of applying for a programming position at a company, I recently I had submitted some code samples to one of the developers for review. This is the feedback I received: One of his...
22
by: xixi | last post by:
hi, we are using db2 udb v8.1 for windows, i have changed the buffer pool size to accommadate better performance, say size 200000, if i have multiple connection to the same database from...
5
by: bull | last post by:
hi could any one explain with example the following in a better way to understand 1. what is stack in memory, how the programs are stored in stack , what is the use of stack 2. What is heap in...
10
by: Markus.Elfring | last post by:
Some APIs/SDKs are available to modify settings like "readable", "writeable" or "executeable". Examples: - http://msdn.microsoft.com/library/en-us/memory/base/memory_protection_constants.asp -...
17
by: frederic.pica | last post by:
Greets, I've some troubles getting my memory freed by python, how can I force it to release the memory ? I've tried del and gc.collect() with no success. Here is a code sample, parsing an XML...
11
by: Grey Alien | last post by:
Any one know of an open source memory pool library?. I can't seem to find any implemented in C (many available in C++ e.g. Boost). Google is not turning up anything useful ...
11
by: Grey Alien | last post by:
I am looking to write a very simple memory pool library to store only one data type at a time - i.e. to provide a contiguous block of memory to be alloc'd free'd by the calling program. I am I...
34
by: jacob navia | last post by:
Suppose that you have a module that always allocates memory without ever releasing it because the guy that wrote it was lazy, as lazy as me. Now, you want to reuse it in a loop. What do you do?...
6
by: CANCER.0707 | last post by:
The problem statement is as follows Create a library that creates pools of memory of different sizes.For e.g. one pool of 32 byte size, another pool of 64 byte size and so on.Create an array of...
16
by: graham.keellings | last post by:
hi, I'm looking for an open source memory pool. It's for use on an embedded system, if that makes any difference. Something with garbage collection/defragmentation would be nice. It should have...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.