Can size of array allocated through new be changed ??

N

nishit.gupta

Hi
i need some help related to memory allocated through new.
In my code, depending upon some parameter i allocate memory to a
variable dynamically through new.
But due to change in requirement it may happen that i need some more
memory later on. Can it
be done now without changing previous implementation ???
I cant change it to link list my code is a part of some other
projects.
i cant even delete and reallocate new memory as in that case i need to
modify
classes (for copy constructors )......
requirement is :
i initialize a pointer, let say:
i = new class_name*[x];
later on i need i to be (let say ) x + y
so cant i do some more new and add it to previous x?????
as i told i cant delete i or change it to link list....
is their any way to do it????
rgds
 
G

Gianni Mariani

On May 8, 5:24 pm, (e-mail address removed) wrote:
....
is their any way to do it????

No.

The best you can do is to reserve a very large block of memory. Best
possible scenario is to if the size of this array becomes very large
is memory map (not standard C++) a large section of memory (from disk)
and reserve it so as to avoid loosing all your swap file to it.

Best possible scenario is to allocate what would be an insane number
of elements - where insane is larger than you can possibly expect and
then some using malloc (or new char[]) and then initialize only the
number of elements you need at a time. This usually consumes virtual
memory but if insane is less than 100meg, you're probably never going
to notice on an appropriately equipped machine.

With the memory mapped approach your limit gets to about 1 gig on a 32
bit machine and probably a whole lot more on a 64 bit machine.

However, to answer your specific question, NO.
 
D

Default User

Hi
i need some help related to memory allocated through new.
In my code, depending upon some parameter i allocate memory to a
variable dynamically through new.
But due to change in requirement it may happen that i need some more
memory later on.


The best way to handle this problem is by using std::vector.




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top