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

Can size of array allocated through new be changed ??

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

May 8 '07 #1
2 1516
On May 8, 5:24 pm, nishit.gu...@st.com 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.
May 8 '07 #2
ni**********@st.com wrote:
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
May 8 '07 #3

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

Similar topics

11
by: eggie2486 | last post by:
What is the maximum size of an array? I tried to edit an extremely large array for a magic square, for example, array, and when I ran the program, it would not display the array. I changed the...
9
by: pvinodhkumar | last post by:
The number of elemets of the array, the array bound must be constant expression?Why is this restriction? Vinodh
22
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a...
43
by: Frodo Baggins | last post by:
Hi all, We are using strcpy to copy strings in our app. This gave us problems when the destination buffer is not large enough. As a workaround, we wanted to replace calls to strcpy with strncpy....
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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
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,...
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
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...

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.