473,399 Members | 2,159 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,399 software developers and data experts.

vector resize from within function

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
2 3417
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
9
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
3
by: Daniel J Watkins | last post by:
Hi, Some runtime memory exceptions are being exhibited with some code I've written. Can you clarify the following with you to see if my understanding of the principles under question are...
8
by: Jason Heyes | last post by:
Does the STL have a function like this one? template <typename T> void remove(std::vector<T> &v, std::vector<T>::size_type index) { std::swap(v, v.back()); v.resize(index); } Unlike...
2
by: mrbrightsidestolemymoney | last post by:
Hi, I'm having a problem resizing a (very big) nested vector. It's not the most streamlined piece of code ever but I need this array to avoid having to recalculate the same quantity millions of...
9
by: Gernot Frisch | last post by:
Hi, I want to be able to write: class foo { std::vector<intm_i (64); }
6
by: Jeff.Boeker | last post by:
I'm learning a lesson in how I need to be more specific :) In C++ I can resize a vector and it will allocate memory and it will call the default constructor if necessary (or I can supply an...
3
by: Bram Kuijper | last post by:
Hi all, I am trying to resize a vector of objects (MyObj below), which contain references to other objects (OtherObj, see below). However, apparently somewhere in the resize operation an...
5
by: cham | last post by:
Hi, I am working on c++ in a linux system ( Fedora core 4 ), kernel version - 2.6.11-1.1369_FC4 gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 ) In my code i am creating a vector to store...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.