By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,537 Members | 1,750 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,537 IT Pros & Developers. It's quick & easy.

vector resize from within function

P: n/a
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
Mar 5 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

mj wrote:
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


Mar 5 '06 #2

P: n/a
mj wrote:
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
--
Please remove capital As from my address when replying by mail
Mar 5 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.