473,796 Members | 2,520 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

new/ resize

how do you resize an array allocated with new?
Jul 19 '05 #1
16 6213
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?

Jul 19 '05 #2
> > 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 :)
Jul 19 '05 #3
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.l earn.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

Jul 19 '05 #4
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.l earn.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

Jul 19 '05 #5

"Thomas Matthews" <Th************ *************** *@sbcglobal.net > wrote in
message news:3F******** **@sbcglobal.ne t...
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

Jul 19 '05 #6
"Howard" wrote:

"Thomas Matthews" <Th************ *************** *@sbcglobal.net >
wrote in message news:3F******** **@sbcglobal.ne t...
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***********@g mx.netNO*SPAM
Jul 19 '05 #7
Howard wrote:
"Thomas Matthews" <Th************ *************** *@sbcglobal.net > wrote in
message news:3F******** **@sbcglobal.ne t...

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.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 19 '05 #8
How does std::vector resize then?

Perhaps I could access its memory functions/classes/whatever and use them
for my own purpose?
Jul 19 '05 #9
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)cybers pace.org | don't, I need to know. Flames welcome.
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3406
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 rgbReserved;
3
3270
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 parent that holds my user control is resized, I resize my image so that it uses the maximum available space. Note: It takes about 2 seconds to regenerate the
4
3490
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 between the borders of the listview and the borders of the form remain constant. I am finding this to be unexpectedly hard. For one thing, I initialized some values in the form's Load event, and I'm doing the control resizing in the form's Resize...
15
5367
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 path of the uploaded image, and resize it with the provided dimensions. My function is below. The current function is returning an error when run from the upload function: A generic error occurred in GDI+. Not sure what exactly that means. From what...
12
14458
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
2
2666
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 times! The (relevant) snippets of code are below : since it's relevant though L=28,T=96 (so TasteProps weighs in at a hefty 8*96*28*28*28*96=1,618,477,056 doubles )
3
2476
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 stated that everyone else was taking 2 or 3 seconds. Then he found one other person taking >30 seconds, and it turns out the common denominator was both of us have Intel chips (Celeron) where the other people have AMD. I had him send me his code...
11
3478
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 MouseMove, the form is resized around a 30-100 times in one second. This leads to a high CPU utilization and all other application comes to a stand still. The form does not have any controls i.e. buttons, text boxes etc. It is completely...
1
2041
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 width, but as soon as I try to add some code to make sure that the width is always divisible by three, VS crashes. How can I force the width 9and height) to always be a factor of three? Thanks. J
8
9433
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 rotate the image by 90 deg. They are two differennt files i.e. resize.php and rotate.php. What I want to do is to combine both rotate.php & resize.php files, so when the script resized the image than it will call rotate script to rotate the...
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10455
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10228
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10173
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10006
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.