473,804 Members | 2,164 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
16 6214
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.

Jul 19 '05 #11
In article <bn**********@c hessie.cirr.com >,
Christopher Benson-Manica <at***@nospam.c yberspace.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++.m oderated 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*******@pres by.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 19 '05 #12

"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
Jul 19 '05 #13


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
Jul 19 '05 #14
> > 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.merke rk(at)dse.nl


Jul 19 '05 #15

"Howard" <al*****@hotmai l.com> wrote in message news:bn******** @dispatch.conce ntric.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.
Jul 19 '05 #16
In article <3f************ *********@news. newshosting.com >, Ron Natalie wrote:

"Howard" <al*****@hotmai l.com> wrote in message news:bn******** @dispatch.conce ntric.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 electromagnetic s), this is not a problem. I may switch
certain things over to vector, however, if it really is safe.

Jul 19 '05 #17

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
3491
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
5371
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
14460
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
2667
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
2477
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
2042
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
9436
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
9714
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
9594
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
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
10350
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...
0
10096
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
9174
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
5534
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...
1
4311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3834
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.