473,378 Members | 1,378 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,378 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 1620
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 more than 1 implementation? Does anyone know of...
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 to pass elements by value from FooRepository to...
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 instance , i have three databases, but only one is...
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 cleared or the map and the Objects, so that the memory...
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 memory will go.. Does it go to back operating...
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 a memory leak but I don't think there's one. Here...
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 system so I am not even out of physical RAM, much...
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, the container must destroy each object. Currently I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.