new/ resize

R

Rolf Magnus

machine99 said:
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?
 
M

machine99

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 :)
 
T

Thomas Matthews

machine99 said:
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
 
T

Thomas Matthews

Thomas said:
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
 
H

Howard

Thomas Matthews said:
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
 
A

Andreas =?ISO-8859-1?Q?M=FCller?=

Howard said:
Thomas Matthews said:
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!)
 
T

Thomas Matthews

Howard said:
message
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.
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
 
M

machine99

How does std::vector resize then?

Perhaps I could access its memory functions/classes/whatever and use them
for my own purpose?
 
C

Christopher Benson-Manica

Rolf Magnus said:
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?
 
R

Rolf Magnus

Christopher said:
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.
 
J

Jon Bell

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".
 
H

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!)
--

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
 
P

Peter van Merkerk

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.
 
K

Karl Heinz Buchegger

Howard said:
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.
 
R

Ron Natalie

Howard said:
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.
 
J

Jim West

Howard said:
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.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top