473,811 Members | 2,970 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Resizing arrays in C++?

How do I do it?

I'm doing a practice problem which includes me implementing a set of
ints as a class. I don't want to use vector because that would be
cheating. ;) I don't want to spend enough time on it to make it a
treeset, so I'm just gonna do an array set. It's gonna have a static
const int default size, and it's going to have a member private int
threshold. Once the size of the set passes the threshold, the next
call to any operation on it will resize it to double whatever its
current size is.

So the only difficulty is, how do I resize the array? I know that I
could use c's malloc and realloc if I included the libraries, but I
don't know if this is the right way to do things in C++.

Any help is much appreciated, as always.
Jul 22 '05 #1
46 4705
Blue Ocean wrote:

How do I do it?

I'm doing a practice problem which includes me implementing a set of
ints as a class. I don't want to use vector because that would be
cheating. ;) I don't want to spend enough time on it to make it a
treeset, so I'm just gonna do an array set. It's gonna have a static
const int default size, and it's going to have a member private int
threshold. Once the size of the set passes the threshold, the next
call to any operation on it will resize it to double whatever its
current size is.

So the only difficulty is, how do I resize the array? I know that I
could use c's malloc and realloc if I included the libraries, but I
don't know if this is the right way to do things in C++.

Well, malloc() and realloc() are part of C. As you dealing with POD data
(ints) there's no real problem doing so. If you use new, then you'd have
to copy the data to the new block. So pick method and go with it. I'd
use the *alloc() family, personally if I had to do this problem.

Don't forget you need to implement the [] and = operators at the least
to make it look work an array. What to do with out of bounds access is
up to you.

Brian Rodenborn
Jul 22 '05 #2
Blue Ocean wrote:
How do I do it?
Do what?
I'm doing a practice problem which includes me implementing a set of
ints as a class. I don't want to use vector because that would be
cheating. ;) I don't want to spend enough time on it to make it a
treeset, so I'm just gonna do an array set. It's gonna have a static
const int default size, and it's going to have a member private int
threshold. Once the size of the set passes the threshold, the next
call to any operation on it will resize it to double whatever its
current size is.

So the only difficulty is, how do I resize the array? I know that I
could use c's malloc and realloc if I included the libraries, but I
don't know if this is the right way to do things in C++.


Use new. If you need to resize, allocate another block with new, copy
the data over and deallocate the old block. That's what realloc usually
does, too.

Jul 22 '05 #3
Default User wrote:
Well, malloc() and realloc() are part of C.


Damn. Are part of C++ I meant to say.

Brian Rodenborn
Jul 22 '05 #4
Rolf Magnus wrote:
Use new. If you need to resize, allocate another block with new, copy
the data over and deallocate the old block. That's what realloc usually
does, too.

So why use it instead of realloc()? Besides saving coding steps, there's
a possiblity that realloc() will have some under-the-hood tricks to
speed things up.

Now, if he was creating a template version that would deal with non-POD
objects, then yes. But in this case, new only adds overhead while buying
you nothing. The allocated data is contained and managed by the array
class, it won't have the problem of incorrect deallocation that can
happen when mixing new and malloc() allocations.

Brian Rodenborn
Jul 22 '05 #5

"Default User" <fi********@boe ing.com.invalid > wrote in message
news:40******** *******@boeing. com.invalid...
Rolf Magnus wrote:
Use new. If you need to resize, allocate another block with new, copy
the data over and deallocate the old block. That's what realloc usually
does, too.

So why use it instead of realloc()? Besides saving coding steps, there's
a possiblity that realloc() will have some under-the-hood tricks to
speed things up.


I remember reading from some book, that never use malloc with C++, but use
new instead. dont remember the reason. Was it even in Bjarne's book? I have
a feeling, that new is better ... but I am not going to start arguing:).

Any experts here to tell the truth?
Jul 22 '05 #6

"Juha Kettunen" <no******@email .com> wrote in message
news:NN******** *******@newsfe5-gui.ntli.net...

"Default User" <fi********@boe ing.com.invalid > wrote in message
news:40******** *******@boeing. com.invalid...
Rolf Magnus wrote:
Use new. If you need to resize, allocate another block with new, copy
the data over and deallocate the old block. That's what realloc usually does, too.

So why use it instead of realloc()? Besides saving coding steps, there's
a possiblity that realloc() will have some under-the-hood tricks to
speed things up.


I remember reading from some book, that never use malloc with C++, but use
new instead. dont remember the reason. Was it even in Bjarne's book? I

have a feeling, that new is better ... but I am not going to start arguing:).


I went and looked it up in Bjarne's text after you mentioned it. It does
indeed say in Ch 1's notes to C programmers not to use malloc or realloc
unless it's unavoidable.
Jul 22 '05 #7
"Aguilar, James" wrote:

"Juha Kettunen" <no******@email .com> wrote in message
news:NN******** *******@newsfe5-gui.ntli.net...

"Default User" <fi********@boe ing.com.invalid > wrote in message
news:40******** *******@boeing. com.invalid...
Rolf Magnus wrote:

> Use new. If you need to resize, allocate another block with new, copy
> the data over and deallocate the old block. That's what realloc usually > does, too.
So why use it instead of realloc()? Besides saving coding steps, there's
a possiblity that realloc() will have some under-the-hood tricks to
speed things up.


I remember reading from some book, that never use malloc with C++, but use
new instead. dont remember the reason. Was it even in Bjarne's book? I

have
a feeling, that new is better ... but I am not going to start arguing:).


I went and looked it up in Bjarne's text after you mentioned it. It does
indeed say in Ch 1's notes to C programmers not to use malloc or realloc
unless it's unavoidable.


Hopefully he went off to explain exactly why he made that statement.

Personally, I avoid all 'do' or 'don't do' generalizations -- they really serve
no purpose. I have found that having someone understand the various
idioms/solutions, and then let them make what they feel to be the appropriate
choice goes a lot further to creating good programmers and programming styles.

My recommendation to the OP is to: research the various ways to create and
maintain arrays, including C-style malloc, C++ new, STL containers, rolling
your own container, etc. If you don't have the time/desire to do all that
research up front, typically the best solution for arrays is using one of the
STL containers such as vector.

Remember, that all of the language constructs are there for a reason. There is
no perfect solution for all situations. Consider each possible solution on a
case-by-case basis, and when you are done, review what you have done and see if
it truly fit your needs/requirements. If not, either re-implement (refactor),
or learn from the experience and make improvements in the future.
Jul 22 '05 #8
On Sun, 11 Jul 2004 00:25:17 GMT, Juha Kettunen <no******@email .com> wrote:

"Default User" <fi********@boe ing.com.invalid > wrote in message
news:40******** *******@boeing. com.invalid...
Rolf Magnus wrote:
> Use new. If you need to resize, allocate another block with new, copy
> the data over and deallocate the old block. That's what realloc

usually
> does, too.

So why use it instead of realloc()? Besides saving coding steps, there's
a possiblity that realloc() will have some under-the-hood tricks to
speed things up.


I remember reading from some book, that never use malloc with C++, but
use
new instead. dont remember the reason. Was it even in Bjarne's book? I
have
a feeling, that new is better ... but I am not going to start arguing:).

Any experts here to tell the truth?


The reason is undoubtedly that new creates objects (i.e. constructors get
called) whereas malloc only allocates memory (no constructors get called).
However the OP is creating ints, so there is no constructor to call. There
are also situations where there is a constructor but you want to delay
calling it for some reason. As Julie says later on, always be suspicious
of 'never to this', 'always do that', try to understand the reasons for
the advice and realise that there often will be exceptions.

I agree with DefaultUser, in this case I think malloc, realloc would be
fine, unless the OP is thinking at some future point of trying to get his
class working with something more complex than ints.

john
Jul 22 '05 #9

"John Harrison" <jo************ *@hotmail.com> wrote in message
news:opsaymu9ol 212331@andronic us...
On Sun, 11 Jul 2004 00:25:17 GMT, Juha Kettunen <no******@email .com> wrote: I agree with DefaultUser, in this case I think malloc, realloc would be fine, unless the OP is thinking at some future point of trying to get his
class working with something more complex than ints.


But this fact is maybe just the reason to use new at the very beginning
....so that your code is ready to all changes better. You dont need to
remember in future whether you need to change malloc to new or not ... the
code is ready for complicated modifications. I personally prefer to write
code, which is ready for the future, and you need to change as little as
possible to get it work after adding new things (of course this is not
always possible, for example you cannot code so that Windows code will be
easily to translated to Unix code for example). I am always keeping in my
mind, that the code is easily ready for the future addings. Thats why for
example, if you need to define human (and at the first version you need only
integers to define it), I will NOT define it as:

int Human[10];

but

class CHuman
{
.....
};

CHuman Human[10];
So that it is ready for the future when I want to add something more for the
Human.

Is this right?
Jul 22 '05 #10

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

Similar topics

10
2340
by: riki | last post by:
Hi, i have a big problem...i'm using one jscript for resizing of all of my pics in popUp...in main html i'm having many little pics and clicking on them they open in popUp and resize to larger version of the same pic. now, it works fine and sometimes when i click on little one it doesn't resize well... this is the code: main.html
0
2632
by: TJ Talluto | last post by:
<facts> I have a "month calendar" that always displays exactly 42 days... and alongside is a vertical box that displays the detail (form fields) of any particular select event that appears on the calendar, so that the event may be changed, or a new event may be created. monthname X X X X X X X form X X X X X X X form X X X X X X X form
5
4733
by: Jim | last post by:
I've heard that resizing images through PHP (either GD2 or ImageMagick) is a processor intensive exercise. I'm setting up a site where users will be uploading up to 10 images along with the details of their product. For each image uploaded (max 500Kb), I'll be resizing it to create a small, medium and large version after which I'll discard the original. My worry is that as the site becomes more popular, the processor time spent resizing...
7
12160
by: Kjell | last post by:
H I'm a former VB programmer and I have a issue with Arrays in C If I can't tell the size in advance how can I solve the issue since C# does not suppor resizing arrays like VB do (i.e. Redim Preserve MyArray(x) Kjell
11
17887
by: Sharon | last post by:
I'm writing a new control derived from UserControl. I need to get an event when the control is done resizing. I tried the Resize, SizeChanged, Move and the Layout events and I also tried to override them. But they all invoked when the control is in the middle of the resizing process. I'm not using breakpoints, I'm using trace to see which one is invoked and when.
4
5587
by: John A Grandy | last post by:
As there is no ReDim in C# , what is the preferred technique for dynmically increasing the size of any array ? Does the answer depend on whether the array holds primitives versus instances of classes ?
9
5317
by: dli07 | last post by:
Hello, I'm trying to convert a piece of code that creates a dynamic vertical resizing bar in a table from internet explorer to firefox. It's based on a post from http://blogs.crankygoblin.com/blogs/geoff.appleby/pages/50712.aspx. I've also read the post on this topic by bggraphics, but he doesn't arrive at a final result. The main problem I am having is that the layerX and layerY event properties don't work. They're supposed to return the...
0
1804
by: mdwhite76 | last post by:
Hello, I have a 2 dimensional array that I need to add more elements to. Array.Copy and Array.Resize only work for one dimensional arrays. Any thoughts as to how I can accomplish this with a 2 dimensional array? Thanks
10
7083
by: mishrarajesh44 | last post by:
hii all, I am facing a problem currently.. i have a script for image uploading and resizing.. the image uploading takes place properly for every size images.. but, the resizing works for only small sized iamages.. for eg. resizing takes place for 70 kb sized images but fails for 600kb or more.. my code is below..
0
9607
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
10652
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
10395
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
10408
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
10137
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
9211
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
5561
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...
2
3874
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3026
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.