472,337 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 1574
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*********@hotmail.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<int> > 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<anything>' 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 multidimensional 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
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...
2
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
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...
4
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...
18
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...
13
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...
9
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...
81
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...
6
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,...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

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.