how do you resize an array allocated with new? 16 6179
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?
> > 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 :)
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
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
"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in
message news:3F**********@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
"Howard" wrote: "Thomas Matthews" <Th****************************@sbcglobal.net> wrote in message news:3F**********@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***********@gmx.netNO*SPAM
Howard wrote: "Thomas Matthews" <Th****************************@sbcglobal.net> wrote in message news:3F**********@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
How does std::vector resize then?
Perhaps I could access its memory functions/classes/whatever and use them
for my own purpose?
Rolf Magnus <ra******@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.
Christopher Benson-Manica wrote: Rolf Magnus <ra******@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.
In article <bn**********@chessie.cirr.com>,
Christopher Benson-Manica <at***@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 <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
"Andreas Müller" <me@privacy.net> wrote in message
news:bn************@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
Howard wrote: "Andreas Müller" <me@privacy.net> wrote in message news:bn************@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 kb******@gascad.at
> > 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
"Howard" <al*****@hotmail.com> wrote in message news:bn********@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.
In article <3f*********************@news.newshosting.com>, Ron Natalie wrote: "Howard" <al*****@hotmail.com> wrote in message news:bn********@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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Peter Mrosek |
last post by:
Hello,
I have the following declaration in an header file (abbreviated
version):
typedef struct {
unsigned char rgbBlue;
unsigned char rgbGreen;
unsigned char rgbRed;
unsigned char...
|
by: Z D |
last post by:
Hello,
BACKGROUND:
==============
I've created a Windows User Control that contains an Image Control (among
other controls).
The user control handles the picture resize event. Whenever the...
|
by: Rob Richardson |
last post by:
Greetings!
I have a form with a listview, a menu, and a few text boxes, labels and
command buttons. I want to resize the listview when the form is resized to
that the widths of the spaces...
|
by: David Lozzi |
last post by:
Howdy,
I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders!
Now I am making a public function that will take the...
|
by: Maxwell2006 |
last post by:
Hi,
I declared an array like this:
string scriptArgs = new string;
Can I resize the array later in the code?
Thank you,
Max
|
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...
|
by: Jim Langston |
last post by:
I really am not sure if this question belongs in this newsgroup, but not
sure where else to ask it.
There is someone working on a game that I tested, and it was taking >30
seconds to load. He...
|
by: Ajith Menon |
last post by:
I have created a windows application in which the form needs to be
resized on the MouseMove event. The windows resize function takes a lot
of CPU cycles. And as the resize function is called on the...
|
by: |
last post by:
I'm creating a user control where the size always needs to be divisible by three.
In the resize event within the custom control I'm having no problem maintaining the height to be the same as the...
|
by: infoseekar |
last post by:
Image Resize & Rotation
Hi
I have 2 scripts, one for Image rotation and other image resize and they both are working.
Image resize scripts load the picture and resize it and Image rotation...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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...
|
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...
| |