new/ resize



 DEVELOP > c-Plus-Plus > new/ resize

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "machine99"
Date: 20 Oct 2003 04:24:46 AM
Object: new/ resize
how do you resize an array allocated with new?
.

User: "Rolf Magnus"

Title: Re: new/ resize 20 Oct 2003 04:28:35 AM
machine99 wrote:

how do you resize an array allocated with new?

You don't. Yo have to allocate a new block of memory of the target size
and then move the data there and deallocate the original array.
Any reason why you don't use std::vector, which handles resizing for
you?
.
User: "machine99"

Title: Re: new/ resize 20 Oct 2003 05:49:27 AM

how do you resize an array allocated with new?


You don't. Yo have to allocate a new block of memory of the target size
and then move the data there and deallocate the original array.
Any reason why you don't use std::vector, which handles resizing for
you?

I do use std::vector quite extensively but there will always be special
cases :)
.
User: "Thomas Matthews"

Title: Re: new/ resize 20 Oct 2003 07:34:22 AM
machine99 wrote:

how do you resize an array allocated with new?


You don't. Yo have to allocate a new block of memory of the target size
and then move the data there and deallocate the original array.
Any reason why you don't use std::vector, which handles resizing for
you?



I do use std::vector quite extensively but there will always be special
cases :)

Actually, the situations where an array is preferred or required
over a std::vector are few and don't require resizing.
Some of these situations are code / memory space and execution time.
However, time spent developing (and development cost) usually
outweighs any premature optimization issues and all problems
involved with an array when a std::vector should have been used.
The programming priorities (listed with highest priority first):
1. Quality & robustness
2. Development time (also related to cost).
3. User interaction or interaction with the environment.
4. Code space and execution time (i.e. optimization).
If a program can't correctly turn a motor, turning it faster
or using less code to turn it is a mute point.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
.
User: "Howard"

Title: Re: new/ resize 20 Oct 2003 09:35:43 AM
"Thomas Matthews" <Thomas_MatthewsReallyHatesSpam@sbcglobal.net> wrote in
message news:3F93D5E9.702@sbcglobal.net...

machine99 wrote:

how do you resize an array allocated with new?


You don't. Yo have to allocate a new block of memory of the target size
and then move the data there and deallocate the original array.
Any reason why you don't use std::vector, which handles resizing for
you?



I do use std::vector quite extensively but there will always be special
cases :)


Actually, the situations where an array is preferred or required
over a std::vector are few and don't require resizing.

Why does the need to resize an array mean that it is no longer neccessary to
use an array instead of a vector?

Some of these situations are code / memory space and execution time.
However, time spent developing (and development cost) usually
outweighs any premature optimization issues and all problems
involved with an array when a std::vector should have been used.

The programming priorities (listed with highest priority first):
1. Quality & robustness
2. Development time (also related to cost).
3. User interaction or interaction with the environment.
4. Code space and execution time (i.e. optimization).
If a program can't correctly turn a motor, turning it faster
or using less code to turn it is a mute point.

--

You're missing one of the most important priorities for many of us:
compatibility with existing code. We don't all get to choose how we would
write code. Much of it is forced upon us by what was written before, (or by
what our operating system or third-party API's require).
-Howard
.
User: "Andreas =?ISO-8859-1?Q?M=FCller?="

Title: Re: new/ resize 20 Oct 2003 10:39:20 AM
"Howard" wrote:


"Thomas Matthews" <Thomas_MatthewsReallyHatesSpam@sbcglobal.net>
wrote in message news:3F93D5E9.702@sbcglobal.net...

machine99 wrote:

[snip]

You're missing one of the most important priorities for many of us:
compatibility with existing code. We don't all get to choose how we
would write code. Much of it is forced upon us by what was written
before, (or by what our operating system or third-party API's
require).

-Howard


When it comes to a point where compatibility to arrays comes into the
game, I usually stick with std::vector, as I can easily pass the content
of a vector into a legacy function (&vector[0]) and converting a
returned array into a vector normally doesn't affect performance in the
stuff that I do. (If it does and it was proven by a profiler, then that
is another story!)
--
To mail me directly, remove the NO*SPAM parts in
NO*SPAMxmen40@gmx.netNO*SPAM
.
User: "Howard"

Title: Re: new/ resize 21 Oct 2003 09:36:26 AM
"Andreas Müller" <me@privacy.net> wrote in message
news:bn0vj8$rri5o$1@ID-83644.news.uni-

When it comes to a point where compatibility to arrays comes into the
game, I usually stick with std::vector, as I can easily pass the content
of a vector into a legacy function (&vector[0]) and converting a
returned array into a vector normally doesn't affect performance in the
stuff that I do. (If it does and it was proven by a profiler, then that
is another story!)
--

I wasn't aware you could do that. Is the memory for the objects stored in
the vector guaranteeed to be contiguous? If so, that would definitely allow
me to use vectors in some places where I've avoided them. But how does the
vector handle the case where re-sizing has moved the data in memory?
Overriding the [] (or &) operator?
-Howard
.
User: "Ron Natalie"

Title: Re: new/ resize 21 Oct 2003 09:52:14 AM
"Howard" <alicebt@hotmail.com> wrote in message news:bn3g9a$9sr@dispatch.concentric.net...


I wasn't aware you could do that. Is the memory for the objects stored in
the vector guaranteeed to be contiguous? If so, that would definitely allow
me to use vectors in some places where I've avoided them. But how does the
vector handle the case where re-sizing has moved the data in memory?
Overriding the [] (or &) operator?

Technically the standard is mute on the subject, but you can sort of intuit that it
has to be. This will be corrected in the TC1. Nobody knows of any implementations
that don't do it "right."
The value of the address of the internal array is not valid after any capacity changes.
.
User: "Jim West"

Title: Re: new/ resize 23 Oct 2003 09:36:33 AM
In article <3f954aef$0$171$9a6e19ea@news.newshosting.com>, Ron Natalie wrote:


"Howard" <alicebt@hotmail.com> wrote in message news:bn3g9a$9sr@dispatch.concentric.net...


I wasn't aware you could do that. Is the memory for the objects stored in
the vector guaranteeed to be contiguous? If so, that would definitely allow
me to use vectors in some places where I've avoided them. But how does the
vector handle the case where re-sizing has moved the data in memory?
Overriding the [] (or &) operator?


Technically the standard is mute on the subject, but you can sort of intuit that it
has to be. This will be corrected in the TC1. Nobody knows of any implementations
that don't do it "right."

Hmmm...I was warned away from doing this recently becaus it is non-
standard. However, I found that valarray met most of my needs since it is
guaranteed to be in contiguous memory and it has a resize function (although
data is lost). As I only resize when the data changes completely
(computational electromagnetics), this is not a problem. I may switch
certain things over to vector, however, if it really is safe.
.


User: "Peter van Merkerk"

Title: Re: new/ resize 21 Oct 2003 09:45:40 AM

When it comes to a point where compatibility to arrays comes into

the

game, I usually stick with std::vector, as I can easily pass the

content

of a vector into a legacy function (&vector[0]) and converting a
returned array into a vector normally doesn't affect performance in

the

stuff that I do. (If it does and it was proven by a profiler, then

that

is another story!)


I wasn't aware you could do that. Is the memory for the objects

stored in

the vector guaranteeed to be contiguous?

Officially not (yet). Practically yes.

If so, that would definitely allow
me to use vectors in some places where I've avoided them. But how

does the

vector handle the case where re-sizing has moved the data in memory?
Overriding the [] (or &) operator?

In that case the address obtained with &vector[0] before the resize is
no longer valid (to be more precise not guaranteed to be valid), just
like iterators get invalidated when the vector is changed. Because
std::vector overloads the [] operator, moving of the actual objects in
memory is invisible to the user.
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
.

User: "Karl Heinz Buchegger"

Title: Re: new/ resize 21 Oct 2003 09:46:55 AM
Howard wrote:


"Andreas Müller" <me@privacy.net> wrote in message
news:bn0vj8$rri5o$1@ID-83644.news.uni-

When it comes to a point where compatibility to arrays comes into the
game, I usually stick with std::vector, as I can easily pass the content
of a vector into a legacy function (&vector[0]) and converting a
returned array into a vector normally doesn't affect performance in the
stuff that I do. (If it does and it was proven by a profiler, then that
is another story!)
--


I wasn't aware you could do that. Is the memory for the objects stored in
the vector guaranteeed to be contiguous?

Strictly speaking it is not guaranteed.
But most people feel that it has to be that way and some forced properties
of std::vector force the memory to be practically contiguos. Practically there
is no known implementation which hasn't contigous memory.
That said: There is a defect report on this issue and as far as I know
the next standard will contain the critical sentence.

If so, that would definitely allow
me to use vectors in some places where I've avoided them. But how does the
vector handle the case where re-sizing has moved the data in memory?
Overriding the [] (or &) operator?

Yep. It's a simple exercise.
--
Karl Heinz Buchegger
kbuchegg@gascad.at
.



User: "Thomas Matthews"

Title: Re: new/ resize 20 Oct 2003 12:52:56 PM
Howard wrote:

"Thomas Matthews" <Thomas_MatthewsReallyHatesSpam@sbcglobal.net> wrote in
message news:3F93D5E9.702@sbcglobal.net...

Why does the need to resize an array mean that it is no longer neccessary to
use an array instead of a vector?

IMHO, the primary difference between a vector and an array is the
resizing issue. By the C++ definition, an array is a static structure
in that its size doesn't change. If the array size changes, then a
vector is a better choice (since the code already exists for
resizing and has been tested). There are other qualities of a
vector, such as bounds checking, that are useful during the
development stage.



Some of these situations are code / memory space and execution time.
However, time spent developing (and development cost) usually
outweighs any premature optimization issues and all problems
involved with an array when a std::vector should have been used.

The programming priorities (listed with highest priority first):
1. Quality & robustness
2. Development time (also related to cost).
3. User interaction or interaction with the environment.
4. Code space and execution time (i.e. optimization).
If a program can't correctly turn a motor, turning it faster
or using less code to turn it is a mute point.

--



You're missing one of the most important priorities for many of us:
compatibility with existing code. We don't all get to choose how we would
write code. Much of it is forced upon us by what was written before, (or by
what our operating system or third-party API's require).

-Howard

I still believe that quality and robustness is top priority especially
when maintaining legacy systems. I know from personal experience that
small changes can cause an existing system to blow up (primarily due
to undocumented dependencies).
Don't get me wrong, compatibility is an issue. A correct modification
that takes more time to develop is better than a quick and dirty fix
that isn't 100% correct. Also, if the new code is not compatible
with the existing code, it is worthless. I would place compatibility
under quality and robustness.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
.


User: "machine99"

Title: Re: new/ resize 21 Oct 2003 01:45:32 AM
How does std::vector resize then?
Perhaps I could access its memory functions/classes/whatever and use them
for my own purpose?
.

User: "Thomas Matthews"

Title: Re: new/ resize 20 Oct 2003 07:36:02 AM
Thomas Matthews wrote:

machine99 wrote:

how do you resize an array allocated with new?



You don't. Yo have to allocate a new block of memory of the target size
and then move the data there and deallocate the original array.
Any reason why you don't use std::vector, which handles resizing for
you?




I do use std::vector quite extensively but there will always be special
cases :)



Actually, the situations where an array is preferred or required
over a std::vector are few and don't require resizing.

Some of these situations are code / memory space and execution time.
However, time spent developing (and development cost) usually
outweighs any premature optimization issues and all problems
involved with an array when a std::vector should have been used.

The programming priorities (listed with highest priority first):
1. Quality & robustness
2. Development time (also related to cost).
3. User interaction or interaction with the environment.
4. Code space and execution time (i.e. optimization).
If a program can't correctly turn a motor, turning it faster
or using less code to turn it is a mute point.

Oops, that should be "moot point".
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
.



User: "Christopher Benson-Manica"

Title: Re: new/ resize 21 Oct 2003 07:18:11 AM
Rolf Magnus <ramagnus@t-online.de> spoke thus:

You don't. Yo have to allocate a new block of memory of the target size
and then move the data there and deallocate the original array.
Any reason why you don't use std::vector, which handles resizing for
you?

Just for curiosity's sake, why isn't there something like realloc() for new?
Wouldn't that make situations like this one easier to manage?
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
.
User: "Rolf Magnus"

Title: Re: new/ resize 21 Oct 2003 07:45:19 AM
Christopher Benson-Manica wrote:

Rolf Magnus <ramagnus@t-online.de> spoke thus:

You don't. Yo have to allocate a new block of memory of the target
size and then move the data there and deallocate the original array.
Any reason why you don't use std::vector, which handles resizing for
you?


Just for curiosity's sake, why isn't there something like realloc()
for new? Wouldn't that make situations like this one easier to manage?

For POD types maybe, but if you have "real" classes with constructors,
virtual functions "and stuff", it's not so easy anymore.
Also note that realloc() also uses the "allocate-copy-delete" way.
OTOH, C++ more or less has that functionality, but it's built into
std::vector.
.

User: "Jon Bell"

Title: Re: new/ resize 21 Oct 2003 08:39:48 AM
In article <bn3863$41d$2@chessie.cirr.com>,
Christopher Benson-Manica <ataru@nospam.cyberspace.org> wrote:


Just for curiosity's sake, why isn't there something like realloc() for new?

There's been a *huge* thread recently in comp.lang.c++.moderated about
this during the past month. I suggest that you look it up in Google
Groups. Use the "Advanced Groups Search", restrict your search to
c.l.c++.m, and search for "realloc".
--
Jon Bell <jtbellap8@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER