473,394 Members | 1,935 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,394 software developers and data experts.

leak of pointer

Hi,
I got this problem when try to used the pointer
sample code:

int *A;
A= new A[10];
//...some codes here add data
A= new A[20];
// .. data is lost

So, I want to use the Array "Dyamically". As the words of being
"dynamic array", We can change the array size "resizable". By my code,
I lost the first time input data when I second time declaration. I
don't want to lost the data. And also I want to enlarge the array size.
I don't know how to solve this. Plz help me.

with regard,
ToeNi

Jul 22 '05 #1
4 1508

<to*******@gmail.com> wrote in message
Hi,
I got this problem when try to used the pointer
sample code:

int *A;
A= new A[10];
//...some codes here add data
A= new A[20];

Memory leak...
// .. data is lost

So, I want to use the Array "Dyamically". As the words of being
"dynamic array", We can change the array size "resizable". By my code,
I lost the first time input data when I second time declaration. I
don't want to lost the data. And also I want to enlarge the array size.
I don't know how to solve this. Plz help me.


Use std::vector. Read about it in your favourite textbook.

Sharad
Jul 22 '05 #2
to*******@gmail.com wrote:
Hi,
I got this problem when try to used the pointer
sample code:

int *A;
A= new A[10];
//...some codes here add data
A= new A[20];
// .. data is lost

So, I want to use the Array "Dyamically". As the words of being
"dynamic array", We can change the array size "resizable". By my code,
I lost the first time input data when I second time declaration.
Right. You created a new array of 20 A's and let your pointer point to that,
ignoring the fact that it pointed to another array already.
I don't want to lost the data. And also I want to enlarge the array size.
Arrays in C++ have a fixed size. Note that in your code above, the two
arrays returned by new are in no way related to each other. They are two
distinct objects.
I don't know how to solve this. Plz help me.


You have to copy the contents of the old array to the new array, then delete
the old one, and then overwrite the pointer. Of course you have to make
sure that _all_ pointers to the old array are updated to point to the new
one.
Alternatively, use std::vector, which does that all for you.

Jul 22 '05 #3

<to*******@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Hi,
I got this problem when try to used the pointer
sample code:

int *A;
A= new A[10];
Do you mean...

int *A = new int[10];

? A is the array name, int is the type...right?
//...some codes here add data
A= new A[20];
Same here.
// .. data is lost

So, I want to use the Array "Dyamically". As the words of being
"dynamic array", We can change the array size "resizable". By my code,
I lost the first time input data when I second time declaration. I
don't want to lost the data. And also I want to enlarge the array size.
I don't know how to solve this. Plz help me.


You can't "resize" the array. But you can create a different one (with a
different name, say, B), and the size you want. Then, copy what you need
from A to B. Then delete [] A. After that, you can either work with B, or
copy the B pointer to A and work with A.

You should note, this is not the best way to handle resizeable arrays. The
std::vector class is much better at doing this kind of thing, and more
easily and safely. Plus, if you're creating an array of objects (instead of
just ints), then the work of copying from A to B described above is very
wasteful.

-Howard


Jul 22 '05 #4
Thanks alot Sharad,
Now std::vector can solve my problems. It's very cool.
I learn it from this site
http://www.codeguru.com/Cpp/Cpp/cpp_...icle.php/c4027
with regard,
ToeNi

Jul 22 '05 #5

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

Similar topics

11
by: Eitan Michaelson | last post by:
Hi, Can any one tell me what's wrong with this code? It leaks (acceding to bound checker), when it attempts to reallocate memory. The code is not pure C, but with minor adjustments any C...
32
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes...
7
by: mosaic | last post by:
Hi, all I really interested in how to check the memory leak of a program. Your smart guys, do you have excellent ideas that could share with me? Thank you. The following is my idea: In C...
10
by: s.subbarayan | last post by:
Dear all, I happen to come across this exciting inspiring article regarding memory leaks in this website: http://www.embedded.com/story/OEG20020222S0026 In this article the author mentions:...
8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
10
by: Grahamo | last post by:
Hi, Question relating to pointers, void* and destructors. assuming I have code like this; int main(int argc, char** argv) { X* x = new X(); void* tmp = x;
9
by: benoit808 | last post by:
I don't have a lot of experience with C++ so I apologize if this is a stupid question. I use Paul Nettle's memory manager (mmgr.cpp) which reports a memory leak but I don't think there's one. Here...
9
by: Joakim Hove | last post by:
Hello, I have the following code: foo_ptr * alloc_foo(int size, ...) { /* This function allocates an instance of the foo_ptr type, and returns a pointer to the newly allocated storage. */ }
17
by: Mike | last post by:
Hello, I have following existing code. And there is memory leak. Anyone know how to get ride of it? function foo has been used in thousands places, the signature is not allowed to change. ...
1
by: cody314 | last post by:
Versions: Python 2.5.1 (r251:54863, May 14 2007, 10:50:04) SWIG Version 1.3.20 Hello I have some code that wraps a C++ library so I may use it in python. The code basically just gets some data...
0
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...
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
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,...
0
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 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.