473,887 Members | 2,357 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how stl container classes layout in memory works?

Hi,
I have a STL vector of of characters and the character class has a
Boost array of points.
The things are vector<Characte rchars; and
class Character{
private:
array<Point,Npo ints;
};
Now are the memory layout is contiguous? i.e all the character resides
side by side just like array, and all Points side by side insede the
character?
My intension is NOT to access them with memory location, but as I do
lots of operation on them, it is better if they resides near by places
(preferably in cache)

And during operation, I want to reuse the existing memory, when a new
character gets added or old character gets deleted rather than
allocating some extra memory & deallocating some.

Now I have large amount of data which gets processed like this. How
much this scheme improves speed? rather than having vector<Characte r*>

Also, as u can note, the boost array is static one, thus size have to
be given at compile time to a safe value, which is not very good design
as the no of points in a character is not known ...
Now if I allocate memory dynamically at runtime, will the priximity get
lost? Note I need to perform some operaton within character points as
well as between two adjacent character also.
I had developed a Matlab version of the program where a array was for
all characters, and another array was for all points. character array
was pointing to the start & end location in the points array.
Now this is not very good OO design, as there is risk to modify points
for other character, as I am sending only start & end points in the
point array, and there is no way to restrict the receivng end to use
points within this section.

Any idea about how to make it a good oo design? to use contained
relationship or use pointer in the points buffer?
In the second case, is it possible to to return a portion of the array
to work with (that is, the points within range only) as reference (no
copy of data) ratuer than the whole array?

any suggestion is welcome...
abir

Jul 28 '06 #1
7 4696
toton wrote:
Hi,
I have a STL vector of of characters and the character class has a
Boost array of points.
The things are vector<Characte rchars; and
class Character{
private:
array<Point,Npo ints;
};
Now are the memory layout is contiguous? i.e all the character resides
side by side just like array, and all Points side by side insede the
character?
std::vector guarantees its data will be contiguous in memory, as does
boost::array (aka std::tr1::array ), but the compiler does have some
liberty in padding and reordering classes with private data members. So
it depends a bit on your compiler, but assuming that Character class
has no virtual functions or other data members, I would expect them to
be pretty close to each other.

A bigger question here is, Are your worrying about speed prematurely?
See http://www.gotw.ca/gotw/033.htm, which says: "Programmer s are
notoriously poor guessers about where their code's true bottlenecks
lie. Usually only experimental evidence (a.k.a. profiling output) helps
to tell you where the true hot spots are. Nine times out of ten, a
programmer cannot identify the number-one hot-spot bottleneck in his
code without some sort of profiling tool. After more than a decade in
this business, I have yet to see a consistent exception in any
programmer I've ever worked with or heard about... even though everyone
and their kid brother may claim until they're blue in the face that
this doesn't apply to them."
My intension is NOT to access them with memory location, but as I do
lots of operation on them, it is better if they resides near by places
(preferably in cache)

And during operation, I want to reuse the existing memory, when a new
character gets added or old character gets deleted rather than
allocating some extra memory & deallocating some.

Now I have large amount of data which gets processed like this. How
much this scheme improves speed? rather than having vector<Characte r*>
You'd have to use a profiler to know what impact it has on your (or
your customer's) system. Any other answer would be pure speculation
(particularly since we have no idea what is involved in your
processing).
Also, as u can note, the boost array is static one, thus size have to
be given at compile time to a safe value, which is not very good design
as the no of points in a character is not known ...
Generally, std::vector is used for that.
Now if I allocate memory dynamically at runtime, will the priximity get
lost? Note I need to perform some operaton within character points as
well as between two adjacent character also.
Probably. A vector of vectors would only guarantee that the inner
vectors' data members (e.g., _First, _Last, _ReserveSize -- or
whatever) are in contiguous memory. If the inner vectors are thought of
as the rows of a matrix, then a single datum is guaranteed to be
contiguous with the other data in its row, but each row of data would
almost certainly not be contiguous with the rows above and/or below it.
I had developed a Matlab version of the program where a array was for
all characters, and another array was for all points. character array
was pointing to the start & end location in the points array.
Now this is not very good OO design, as there is risk to modify points
for other character, as I am sending only start & end points in the
point array, and there is no way to restrict the receivng end to use
points within this section.

Any idea about how to make it a good oo design? to use contained
relationship or use pointer in the points buffer?
This is not so much a C++ question as an OO question. Best to ask in
comp.object or similar.
In the second case, is it possible to to return a portion of the array
to work with (that is, the points within range only) as reference (no
copy of data) ratuer than the whole array?
Sure. Use iterators.

Cheers! --M

Jul 28 '06 #2

mlimber wrote:
toton wrote:
Hi,
I have a STL vector of of characters and the character class has a
Boost array of points.
The things are vector<Characte rchars; and
class Character{
private:
array<Point,Npo ints;
};
Now are the memory layout is contiguous? i.e all the character resides
side by side just like array, and all Points side by side insede the
character?

std::vector guarantees its data will be contiguous in memory, as does
boost::array (aka std::tr1::array ), but the compiler does have some
liberty in padding and reordering classes with private data members. So
it depends a bit on your compiler, but assuming that Character class
has no virtual functions or other data members, I would expect them to
be pretty close to each other.
Yes, I can assure the Character or the Point class won't get
subclassed. Thus they don't have any virtual function. Also they don't
have parent. Basically they are not in the class hierarchy. So size is
known prior.
A bigger question here is, Are your worrying about speed prematurely?
See http://www.gotw.ca/gotw/033.htm, which says: "Programmer s are
notoriously poor guessers about where their code's true bottlenecks
lie. Usually only experimental evidence (a.k.a. profiling output) helps
to tell you where the true hot spots are. Nine times out of ten, a
programmer cannot identify the number-one hot-spot bottleneck in his
code without some sort of profiling tool. After more than a decade in
this business, I have yet to see a consistent exception in any
programmer I've ever worked with or heard about... even though everyone
and their kid brother may claim until they're blue in the face that
this doesn't apply to them."
Yes, I also guess so. But as the program is based on computational
geometry & I have some valid reason with existing data support, I am
little worried. May be for a JIT compiled language like Java the worry
is less, because JIT will get a clear hint about memory allocation from
the program structure. But for static compiled language like C++ I am
thinking some way to help the compiler. It is not absolute necessity,
but I want to have a speed comparison and cache hit statistics to say
which one is better.
My intension is NOT to access them with memory location, but as I do
lots of operation on them, it is better if they resides near by places
(preferably in cache)

And during operation, I want to reuse the existing memory, when a new
character gets added or old character gets deleted rather than
allocating some extra memory & deallocating some.

Now I have large amount of data which gets processed like this. How
much this scheme improves speed? rather than having vector<Characte r*>

You'd have to use a profiler to know what impact it has on your (or
your customer's) system. Any other answer would be pure speculation
(particularly since we have no idea what is involved in your
processing).
Also, as u can note, the boost array is static one, thus size have to
be given at compile time to a safe value, which is not very good design
as the no of points in a character is not known ...

Generally, std::vector is used for that.
Now if I allocate memory dynamically at runtime, will the priximity get
lost? Note I need to perform some operaton within character points as
well as between two adjacent character also.

Probably. A vector of vectors would only guarantee that the inner
vectors' data members (e.g., _First, _Last, _ReserveSize -- or
whatever) are in contiguous memory. If the inner vectors are thought of
as the rows of a matrix, then a single datum is guaranteed to be
contiguous with the other data in its row, but each row of data would
almost certainly not be contiguous with the rows above and/or below it.
I had developed a Matlab version of the program where a array was for
all characters, and another array was for all points. character array
was pointing to the start & end location in the points array.
Now this is not very good OO design, as there is risk to modify points
for other character, as I am sending only start & end points in the
point array, and there is no way to restrict the receivng end to use
points within this section.

Any idea about how to make it a good oo design? to use contained
relationship or use pointer in the points buffer?

This is not so much a C++ question as an OO question. Best to ask in
comp.object or similar.
In the second case, is it possible to to return a portion of the array
to work with (that is, the points within range only) as reference (no
copy of data) ratuer than the whole array?
Thus i think I can allocate a fare amount of memory for Points in a
Pool, and constructs the Points there with placement new. Even I think
I can make the pool as a circular buffer of nearly fixed size. And The
character can have the pointer in the pool for its section.
Sure. Use iterators.
Yes, iterators are solution. But most of the time, I want to have all
of the points for a character from the Pool at a time (they are side by
side in the pool). Wont it call the iterator a large no of times to
get the points? Or it can be done in a single call?
Blitz has array facility where the data recides in memory, but 2 or
more array can share the data. That is basically different viwe for the
same array/matrix. But the library is rather big. can it be done in STL
with different allocator?
like if i have a vector<Pointvec ; It will acquires require length of
chunk, and allocate the Points with placement new at the proper
position in vector.
Similarly I want a subvector vector<Point>ve c1 = vector<Point>(v ec,
range); Now both will have same data but different size. Some reference
counting is needed, but in my case the original vector lives through
the program. so avability of data is assured.

can u point any article related with this kind of allocation & data
sharing?

thanks for reply.

abir
>
Cheers! --M
Jul 28 '06 #3
In article <11************ **********@i42g 2000cwa.googleg roups.com>,
ab*******@gmail .com says...
Hi,
I have a STL vector of of characters and the character class has a
Boost array of points.
The things are vector<Characte rchars; and
class Character{
private:
array<Point,Npo ints;
};
Now are the memory layout is contiguous?
No, not overall. The storage for each individual vector is
contiguous, but each vector will have its own, dynamically allocated,
contiguous block of memory that's usually going to be separate from
the others.

In theory, you could cure this to some degree by creating your own
allocator class, and using it for these vectors. In reality, this
will be a partial cure at best though. A vector has a block of memory
allocated, and at any given time it will typically have at least some
empty space at the end of that space -- and if the vector is large,
that empty space can be large as well (in a typical implementation,
it'll average 25 to 50% of the current in-use size of the vector).

IOW, attempting to keep the data contiguous will be a lot of work,
and will ultimately fail in any case.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 28 '06 #4
Jerry Coffin wrote:
In article <11************ **********@i42g 2000cwa.googleg roups.com>,
ab*******@gmail .com says...
Hi,
I have a STL vector of of characters and the character class has a
Boost array of points.
The things are vector<Characte rchars; and
class Character{
private:
array<Point,Npo ints;
};
Now are the memory layout is contiguous?

No, not overall. The storage for each individual vector is
contiguous, but each vector will have its own, dynamically allocated,
contiguous block of memory that's usually going to be separate from
the others.

In theory, you could cure this to some degree by creating your own
allocator class, and using it for these vectors. In reality, this
will be a partial cure at best though. A vector has a block of memory
allocated, and at any given time it will typically have at least some
empty space at the end of that space -- and if the vector is large,
that empty space can be large as well (in a typical implementation,
it'll average 25 to 50% of the current in-use size of the vector).
The OP is basically asking about std::vector<std ::tr1::array<Po int,
N>>, not vector<vector<P oint>>, which seems to be what you're
discussing here. If the vector does need to grow, it would still
maintain contiguity, and if it doesn't need to grow, std::tr1::array or
boost::circular _buffer (currently approved but in the sandbox) could be
used instead.

Cheers! --M

Jul 28 '06 #5
In article <11************ **********@i3g2 000cwc.googlegr oups.com>,
ml*****@gmail.c om says...
Jerry Coffin wrote:
In article <11************ **********@i42g 2000cwa.googleg roups.com>,
ab*******@gmail .com says...
Hi,
I have a STL vector of of characters and the character class has a
Boost array of points.
The things are vector<Characte rchars; and
class Character{
private:
array<Point,Npo ints;
};
[ ... ]
The OP is basically asking about std::vector<std ::tr1::array<Po int,
N>>, not vector<vector<P oint>>, which seems to be what you're
discussing here.
As I originally read his explanation, I thought he was talking about
an array<vector<Ch aracter>, N>, but re-reading it, that probably
wasn't correct. When he said "the things" are vector<Characte r>, I
thought the "things" in question were the Point's. Looking more
carefully, that's apparently not the case, since the definitions
would then be infinitely recursive...

My apologies.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 28 '06 #6

Jerry Coffin wrote:
In article <11************ **********@i3g2 000cwc.googlegr oups.com>,
ml*****@gmail.c om says...
Jerry Coffin wrote:
In article <11************ **********@i42g 2000cwa.googleg roups.com>,
ab*******@gmail .com says...
Hi,
I have a STL vector of of characters and the character class has a
Boost array of points.
The things are vector<Characte rchars; and
class Character{
private:
array<Point,Npo ints;
};

[ ... ]
The OP is basically asking about std::vector<std ::tr1::array<Po int,
N>>, not vector<vector<P oint>>, which seems to be what you're
discussing here.

As I originally read his explanation, I thought he was talking about
an array<vector<Ch aracter>, N>, but re-reading it, that probably
wasn't correct. When he said "the things" are vector<Characte r>, I
thought the "things" in question were the Point's. Looking more
carefully, that's apparently not the case, since the definitions
would then be infinitely recursive...
Yes, it is vector<array<Po int>>, not the other way. And, I wanted all
the Points in the adjacent memory.
Now I am using a circular buffer for character, and one circular
buffer for Point's .
And character points to the Points circular buffer in proper position.
And I am creating the Character & Points in the buffer using placement
new. Thus all the memory is adjacent.
I had bypassed stl totally from my process, and used some simple
classes instead ...

abir

Thanks for suggestions and comments
My apologies.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 31 '06 #7
In article <11************ **********@h48g 2000cwc.googleg roups.com>,
ab*******@gmail .com says...

[ ... ]
Yes, it is vector<array<Po int>>, not the other way. And, I wanted all
the Points in the adjacent memory.
That should have given you contiguous memory.
Now I am using a circular buffer for character, and one circular
buffer for Point's .
And character points to the Points circular buffer in proper position.
And I am creating the Character & Points in the buffer using placement
new. Thus all the memory is adjacent.
Well, if you really want a circular buffer, there's an argument to be
made for doing your own in any case -- there's nothing in the library
specifically intended as a circular buffer, though you can obviously
build one on top of quite a few other kinds of containers.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 31 '06 #8

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

Similar topics

6
7157
by: Florian Brucker | last post by:
Here's the code: <html> <head> <title>CSS Test</title> <style> div.container { border-width:1px; border-color:#000; border-style:solid;
4
25836
by: wls | last post by:
I've got a problem that requires rendering DIVs at different offset locations, and while I've got a solution that works, the absolute positioning is working to the client window, not to the parent container. The stuff I write will get placed inside a table cell produce by someone else's template -- I have no control other than to emit HTML/CSS within that sole cell. So, I decided to construct a simple case and am having problems...
2
2656
by: mosfets | last post by:
Hi, I'm having a little trouble figuring out the difference in terms of memory allocation between: class person_info; class A { private:
6
2708
by: Robert | last post by:
Hello. I have been trying out the Lebans ToolTip Classes at http://www.lebans.com/tooltip.htm, to display "balloon" style help tips in a form. The classes I am using are located at http://www.lebans.com/DownloadFiles/A2kTooltip.zip So far the classes work perfectly, except that now I need to extend it to support other controls besides the ones given in the example form. I have gotten it to work with some controls, but not others. I...
0
1953
by: khurram.shakir | last post by:
I am developing an application, which uses .NET 2.0/WinForms and has a designer for screen layout designing. User has an option to design the layout of screen, and for that we developed our own controls based on UserControl Class. After the designing the layout is saved as an XML, and we have a Loader of XML that renders the layout afterwards. When we load the controls and layout from xml, every thing works fine except we are facing...
7
20020
by: toton | last post by:
Hi, I want a circular buffer or queue like container (queue with array implementation). Moreover I want random access over the elements. And addition at tail and remove from head need to be low cost. STL vector is suitable for removing form tail? or it is as costly as removing from middle? any other std container to serve this purpose? (note , I dont need linked list implementation of any container, as I want random access)
11
4536
by: Henryk | last post by:
I have something like class Params { public: const static char nOne = 1; const static int nTwo = 2; const static char nThree = 3; }; This is just a wrapper for globally used parameters in an embedded
1
5945
by: Miked | last post by:
Hello: I'm relatively new to CSS, and I'm doing a site where I don't want to use any tables. I've gotten pretty far, and the site has the layout I want. My only problem is that I'm using the flot positional paremter a lot on containers, and it really seems to do what I want. Except when I put other containers in these containers, they don't expand vertically like I want. Here's the stylesheet:
36
2054
by: Peter Olcott | last post by:
So far the only way that I found to do this was by making a single global instance of the container class and providing access to the contained class, through this single global instance. Are there any other no-overhead ways that a contained class can access its container? The obvious choice of passing (a pointer or a reference to the container) to the contained class is not a no-overhead solution, it requires both memory and time. I am...
0
9799
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11173
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10770
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10432
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9593
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7988
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7141
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6011
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4632
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.