1748180 Members
4285 Online
108759 Solutions
New Discussion юеВ

Initializing STL vector

 
Thiagu_1
Advisor

Initializing STL vector

We are using RW Sourcepro ED8 on HP-UX 11.11 and Solaris 5.9. Our RW is built using Native STL provided by compiler. We see a difference in behaviour of RWTPtrOrderedVector initialization in Sun and HP.
Consider the following code snippet


RWTPtrOrderedVector rwLongVec(4);

In Sun, memory is allocated only for 4 items but in HP, memory seems to be allocated for 32 items even
though we specify 4 in the constructor. But when we specify value more than 32 in RWTPtrOrderedVector
consturctor, both Sun and HP seems to behave in same way.

We create millions of such RWTPtrOrderedVector objects (each will have not more than 4 items)in our
application and hence we see a huge difference in memory footprint of our application in SUN and HP.

We reported this with Roguewave. They concluded that the issue/difference is due to different implementation in HP-UX for the vector class. How do we fix this so we allocate memory only for the number of items we provide in constructor?
4 REPLIES 4
Dennis Handly
Acclaimed Contributor

Re: Initializing STL vector

Are you using -AP or -AA? There are two different ways to control things.

RWTPtrOrderedVector rwLongVec(4);
>In Sun, memory is allocated only for 4 items but in HP, memory seems to be allocated for 32 items even though we specify 4 in the constructor.

The RW version we have assumes that you are going to grow the vector and doesn't want to keep coping the elements when you add more.

>We create millions of such RWTPtrOrderedVector objects (each will have not more than 4 items)

>We reported this with Roguewave. They concluded that the issue/difference is due to different implementation in HP-UX for the vector class.

They could have given you their documentation on how to do it. ;-)

>How do we fix this so we allocate memory only for the number of items we provide in constructor?

Using google I found it documented on:
http://docs.hp.com/en/1559/libs.htm

See "Allocation Policies for Containers".

In your case of RWTPtrOrderedVector, this creates a std::vector.

Re: Initializing STL vector

Hai,
We use -AA compiler option since RW Edition 8 requires Standard C++ libraries. We found similar issue in string initialization also.
When we run the following code we get the output as 128.

string str("abc");
cout << "size of string = " << str.capacity() << endl;

Same code when run on sun, we get 3 as output.

In our application we many such containers and strings. As a result of moving to standard C++ libraries, size of our application has increased many folds when compared to our previous version where we were using classic STL. Is there a way to minimize the memory consumption of these containers Standard C++ libraries? Please suggest.
Dennis Handly
Acclaimed Contributor

Re: Initializing STL vector

>We found similar issue in string initialization also.

Yes, all containers types assume they are going to grow. You can either see my link where __rw_new_capacity is used to change the allocation, or there are a few defines that could be added for -AA:
# define _RWSTD_MINIMUM_NEW_CAPACITY size_t(32)
# define _RWSTD_NEW_CAPACITY_RATIO float(1.618)

# define _RWSTD_MINIMUM_STRING_CAPACITY size_t(128)
# define _RWSTD_STRING_CAPACITY_RATIO float(1.618)

The first two work for ALL containers. The last two are only for strings.

Note the 32 and the 128 that you are seeing.
Dennis Handly
Acclaimed Contributor

Re: Initializing STL vector

If you have gotten the answers you wanted, you should assign points and close the thread. Please read the following:
http://forums.itrc.hp.com/service/forums/helptips.do?#33