473,714 Members | 2,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

memory management and containers

when i do this

vector < vector<int> > p;

later in the code i do things like this:
vector<int> x,y,z;

p.push_back(x);
p.push_back(y);
p.push_back(z);
p[1].push_back(56);

can i do this without problems?
how will C++ allocate memory for these structures? (note that the nr of
elements in the vectors are not known at compile-time)

is a declaration like "vector < vector<int>* > p" more efficient??

Jul 22 '05 #1
5 1636
slurper wrote:
when i do this

vector < vector<int> > p;

later in the code i do things like this:
vector<int> x,y,z;

p.push_back(x);
p.push_back(y);
p.push_back(z);
p[1].push_back(56);

can i do this without problems?
Can you? I mean, you seem to be doing it just fine. Or did I get
it wrong and you do experience some problems? If so, what kind?
how will C++ allocate memory for these structures? (note that the nr of
elements in the vectors are not known at compile-time)
Why does it matter? It will allocate it somehow. If you really need
to know the inner workings of 'vector' template, use the source. It
is most likely entirely represented in the <vector> header in your
compiler 'include' directory...
is a declaration like "vector < vector<int>* > p" more efficient??


What do you mean by "more efficient"?

V
Jul 22 '05 #2

"slurper" <sl*********@ho tmail.com> wrote in message
news:41******** **************@ news.skynet.be. ..
when i do this

vector < vector<int> > p;

later in the code i do things like this:
vector<int> x,y,z;

p.push_back(x);
p.push_back(y);
p.push_back(z);
p[1].push_back(56);

can i do this without problems?
Yes. It would be simpler to write this

vector < vector<int> > p(3);
p[1].push_back(56);

But both pieces of code are correct.
how will C++ allocate memory for these structures? (note that the nr of
elements in the vectors are not known at compile-time)

In the usual way that all vector memory allocations are done, by using the
allocator object in the vector.
is a declaration like "vector < vector<int>* > p" more efficient??


Possibly, it depends on what you are going to do and how you are going to do
it.

I'm guessing that you are worried about potential inefficiencies of copying
large vectors. There is no such copying going on in the example code you
gave. But if this is a concern the answer is not to reintroduce pointers
because they lead to buggy, complicated (and sometimes inefficient) code. It
hard to give specific advice because I don't know exactly what you are
trying to do or what inefficiencies you have in mind, but I expect you can
to better than declare a vector of pointers.

john
Jul 22 '05 #3
Victor Bazarov wrote:
slurper wrote:
when i do this

vector < vector<int> > p;

later in the code i do things like this:
vector<int> x,y,z;

p.push_back(x);
p.push_back(y);
p.push_back(z);
p[1].push_back(56);

can i do this without problems?
Can you? I mean, you seem to be doing it just fine. Or did I get
it wrong and you do experience some problems? If so, what kind?
how will C++ allocate memory for these structures? (note that the nr of
elements in the vectors are not known at compile-time)


Why does it matter? It will allocate it somehow. If you really need
to know the inner workings of 'vector' template, use the source. It
is most likely entirely represented in the <vector> header in your
compiler 'include' directory...
is a declaration like "vector < vector<int>* > p" more efficient??


What do you mean by "more efficient"?


tx for answers so far
i'm probably thinking too low-level (it's not that i have a problem, i just
want to know if i get the theory about c++ right)
what i want to say is: if i declare vector < vector <int> >; i get a vector
of vectors, but at run-time the different vectors in the vector change and
their number of elements change. this means that you can't use a contiguous
block of memory to fill with ints. C++ apparently has much more
sophisticated memory allocation schemes than was the case in C. in C, you
can't make an array grow. if you don't know how many elements you need to
store in an array, you dynamically ask for memory with malloc (which is a
contiguous block of memory). so if you need an array of array (both arrays
have variable elements in time), you need to implement this by asking for a
certain amount of memory which can hold as much pointers as needed. those
pointers point at other allocated blocks which can hold the elements in the
latter array. you can't just say in C that an array holds elements which
can dynamically grow or shrink, unless you use pointers. or do i get this
wrong? that's why i hesitated...

V


Jul 22 '05 #4
slurper wrote:
when i do this

vector < vector<int> > p;

later in the code i do things like this:
vector<int> x,y,z;

p.push_back(x);
p.push_back(y);
p.push_back(z);
p[1].push_back(56);

can i do this without problems?
how will C++ allocate memory for these structures? (note that the nr of
elements in the vectors are not known at compile-time)
Each argument to push_back is COPIED to a location in the vector. The
storage for the vector is allocated in a way to make the TIME requirements
for insertion amortize to linear time.

If you really aren't putting anything in x, y, and z PRIOR to pushing them
into the the vector p. There's really no point in even declaring them.

vector<vector<i nt> > p(3);

will make a vector of three empty vector<int>.


is a declaration like "vector < vector<int>* > p" more efficient??

Who knows, it depends what you are doing. However, in the case of using
pointers, you're going to have to manage them seperately.
Jul 22 '05 #5
slurper wrote:
[...]
i'm probably thinking too low-level (it's not that i have a problem, i just
want to know if i get the theory about c++ right)
Try looking in "Inside The C++ Object Model" by Stanley Lippman.
what i want to say is: if i declare vector < vector <int> >; i get a vector
of vectors, but at run-time the different vectors in the vector change and
their number of elements change. this means that you can't use a contiguous
block of memory to fill with ints.
Yes, but (a) it has a contiguous block of memory filled by 'vector<int>'
objects and (b) each of them owns another contiguous block of memory
filled with 'int's.
C++ apparently has much more
sophisticated memory allocation schemes than was the case in C.
Not really. Underneath it boils down to the same bytes.
in C, you
can't make an array grow.
Neither can you in C++. But 'vector<int>' or 'vector<anythin g>' is not
an array. It pretends to be one, but it's not. It even has one inside,
but itself it is not an array.
if you don't know how many elements you need to
store in an array, you dynamically ask for memory with malloc (which is a
contiguous block of memory).
That's what 'vector<int>' class does under the covers.
so if you need an array of array (both arrays
have variable elements in time), you need to implement this by asking for a
certain amount of memory which can hold as much pointers as needed.
There are different ways to implement dynamic multidimensiona l arrays.
those
pointers point at other allocated blocks which can hold the elements in the
latter array. you can't just say in C that an array holds elements which
can dynamically grow or shrink, unless you use pointers. or do i get this
wrong? that's why i hesitated...


Essentially you can do all what 'vector' does behind the scenes, in C. It
will be a bit involved, and you could spend some time writing it and
debugging it, but once you achieve the desired behaviour, you will have
something similar to C++ vector. That's what library implementers do.
They develop and debug the code inside <vector> and other places, which
you will later use. In fact, AIUI, most of the Standard C++ library is
implemented using straight C++.

V
Jul 22 '05 #6

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

Similar topics

2
6565
by: wtnt | last post by:
Hello, I've been using the STL libraries for some time, but still don't know the ins and outs of its implementation. Could this be because there's more than 1 implementation? Does anyone know of a good book out there or article that details how one should handle memory usage when using the STL data structures, like vector, map, etc. Specifically, I mean things like when you put a key (char*) into a map, does it copy this? Can I delete it...
2
2649
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:
3
2325
by: jakub.pieczonka | last post by:
I have three classes in my example. Foo, FooRepository and FooImpl. Foo is a lightweight wrapper around FooImpl. The idea is that Foo can be used to pass elements by value from FooRepository to the client code. One option would be to use shared_ptr, but this seems like overkill for my particular situation. The client will not be storing or copying the Foo objects, so I have chosen to implement Foo in the following way:
4
5200
by: xixi | last post by:
i have a very serious memory problem, we have db2 udb v8.1 load on a HP titanium machine with 4 G memory, it is 64bit machine, currently on DB2 instance , i have three databases, but only one is being used, on the same machine, i have an application server running connect to this DB2 server, every day there are around 12 persons has connection through the app server to db server, the database has 9 G data/index, problem is in the morning...
18
21555
by: Jan | last post by:
Hi there, i've got an STL map with something like this ( map<string, Object*> xyz; ) What happens when I call xyz.clear()? Is only the map cleared or the map and the Objects, so that the memory is free again? Have I to delete every single Object? Thanks for replying, Jan
13
1853
by: Vijay | last post by:
Hi All, I am learning C++ and have one question. Using free or delete we can release the memory. After releasing memory where this released memory will go.. Does it go to back operating system? Please explain. Thanks in advance.
9
2342
by: benoit808 | last post by:
I don't have a lot of experience with C++ so I apologize if this is a stupid question. I use Paul Nettle's memory manager (mmgr.cpp) which reports a memory leak but I don't think there's one. Here is the code (i took some stuff out to simplify): void myFunction() { Sprite *pImage; sprintf(pStr, "s04-%02d.png", i);
81
4546
by: Peter Olcott | last post by:
It looks like System::Collections::Generic.List throws and OUT_OF_MEMORY exception whenever memory allocated exceeds 256 MB. I have 1024 MB on my system so I am not even out of physical RAM, much less virtual memory. Are other people experiencing this same problem?
6
2128
by: Dave Rahardja | last post by:
I need to design a container that must hold a set of references to a variety of object types, managed by different memory managers. At some time, the container must destroy each object. Currently I have the following design in mind: class Destructible // interface { public: virtual void destroy() = 0;
0
8801
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8707
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
9314
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
9174
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
9015
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...
1
6634
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
4464
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4725
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2110
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.