Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > vector resize from within function

Reply
Thread Tools

vector resize from within function

 
 
mj
Guest
Posts: n/a
 
      03-05-2006
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
 
Reply With Quote
 
 
 
 
AnonMail2005@gmail.com
Guest
Posts: n/a
 
      03-05-2006
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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-05-2006
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
Removing a vector element using std::swap and std::vector::resize. Jason Heyes C++ 8 01-15-2006 10:40 PM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM



Advertisments