473,320 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

pointer help

63
I have a question. I’m writing a class to handle a dynamic array. I have a function, “resize,” that resizes the array. I assign the values to a temp dynamic array, I delete[] the original dynamic array, then assign the address stored in temp to the original array. My question is, do I need to deallocate temp in any way? Or is this good?

void resize(int size)
{
int *temp = new int[size];

if(size>0)
{
if(Size>size)
{
for(int i=0; i<size; i++)
{
temp[i]=Array[i];
}
}
else
{
for(int i=0; i<Size; i++)
{
temp[i]=Array[i];
}
}
Size=size;

delete[] Array; //<-
Array = temp; //<- points of interest
}
else
{
free();
}
}

“Size” is the old size, “size” is the new size, free() is just {delete[] Array;}
Oct 22 '06 #1
1 1143
arne
315 Expert 100+
I have a question. I’m writing a class to handle a dynamic array. I have a function, “resize,” that resizes the array. I assign the values to a temp dynamic array, I delete[] the original dynamic array, then assign the address stored in temp to the original array. My question is, do I need to deallocate temp in any way? Or is this good?

void resize(int size)
{
int *temp = new int[size];

if(size>0)
{
if(Size>size)
{
for(int i=0; i<size; i++)
{
temp[i]=Array[i];
}
}
else
{
for(int i=0; i<Size; i++)
{
temp[i]=Array[i];
}
}
Size=size;

delete[] Array; //<-
Array = temp; //<- points of interest
}
else
{
free();
}
}

“Size” is the old size, “size” is the new size, free() is just {delete[] Array;}
Looks ok to me, except for one thing: you should move the allocation into the if(size>0) clause. Otherwise you will occasionally try to allocate an int array with a negative number of elements (which will fail) and you'll never come to your free() statement ...
Oct 22 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: ali | last post by:
Hi, I'm trying to understand the reason for different output on the following codes Code1: #include <iostream.h> int main()
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
10
by: Simon | last post by:
I'm a js newbie trying to use some very simple js to call an ActiveX object's methods. I need to use a pointer to call an embedded ActiveX object's method to receive a number. As I understand it,...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
7
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
6
by: lithiumcat | last post by:
Hi, maybe you remember me, some time ago I asked about how to store an integer value into a void*, and I learned that doing pointer arithmetic yeilding a pointer outside of an object (except the...
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.