vector resize from within function

M

mj

Hi,

I recently have found it necessary to move from fortran to c++ for
scientific programming... I'm working on a program that needs to resize
a 2d vector of vectors within a function... This variable "tri" is an
input arg to my function using the syntax:

function(vector<vector<int> >& tri)

The problem occurs when the tri 'matrix' is resized to triple or
quadruple the originally allocated size (which I guess requires a
complete reallocation and consequently, a change in the memory address).
Just having to deal with a slew of memory allocation issues, and I
was wondering if there is a good way to resize this 2d vector other than
defining a class to contain the tri variable, and writing a resize
function within the class?

Thanks for your help!

Matt
 
A

AnonMail2005

The issue with vectors is that they are guarenteed to have contiguous
storage. So they are not very well suited for things that will be
resized unless you know the maximum size beforehand. If you know
the size, use reserve so that resize will not trigger subsequent
memory allocations.

I would try to find something already implemented and use that. Take
a look at valarray which is part of the standard library and also
boost's multi-array to see if they serve your purposes.
 
V

Victor Bazarov

mj said:
I recently have found it necessary to move from fortran to c++ for
scientific programming...

Let me just pause here, and say, wow! It took you _so_ long?...
I'm working on a program that needs to
resize a 2d vector of vectors within a function... This variable
"tri" is an input arg to my function using the syntax:

function(vector<vector<int> >& tri)

The problem occurs when the tri 'matrix' is resized to triple or
quadruple the originally allocated size (which I guess requires a
complete reallocation and consequently, a change in the memory
address). Just having to deal with a slew of memory allocation
issues, and I was wondering if there is a good way to resize this 2d
vector other
than defining a class to contain the tri variable, and writing a
resize function within the class?

You might consider (a) reserving the size to what it should be so
there is no need to reallocate when you resize, or (b) use some
other structure, like a single-dimentional "vector" with the two-
dimensional interface, it's easier to resize, maybe.

Generally speaking, instead of wrapping your 'tri' in a class, do
write just a stand-alone function for resizing. There is no need
to roll out your own class just for the sake or resizing. Of course
using some pre-defined matrix class (I am sure you can find a slew
of them on the 'net) might prove beneficial. But I've never heard
of matrices changing (growing) their sizes during any operation.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top