473,625 Members | 3,264 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic allocation

Hi all

Im not quite sure how to use the new and delete for a whole array
dynamically.
Actually i want that, a user inputs a char in a single char array.
Everytime he inputs a char he creates a new array of (previous size +
1), he then copies the content of the old array in to the new array
and adds the new input at the end and deletes the old array. I guess
he has to pass the pointer to the array everytime.

For example.
input: a
output a
input: b
output: a b
input c:
output a b c .

Any clues?????????? ????

Feb 13 '07 #1
12 2353
"wh************ *@gmail.com" <wh************ *@gmail.comwrot e in
news:11******** **************@ a75g2000cwd.goo glegroups.com:
Hi all

Im not quite sure how to use the new and delete for a whole array
dynamically.
Actually i want that, a user inputs a char in a single char array.
Everytime he inputs a char he creates a new array of (previous size +
1), he then copies the content of the old array in to the new array
and adds the new input at the end and deletes the old array. I guess
he has to pass the pointer to the array everytime.

For example.
input: a
output a
input: b
output: a b
input c:
output a b c .

Any clues?????????? ????

Forget arrays. Look up std::vector.
Feb 13 '07 #2
wh************* @gmail.com wrote:
Hi all

Im not quite sure how to use the new and delete for a whole array
dynamically.
Actually i want that, a user inputs a char in a single char array.
Everytime he inputs a char he creates a new array of (previous size +
1), he then copies the content of the old array in to the new array
and adds the new input at the end and deletes the old array. I guess
he has to pass the pointer to the array everytime.

For example.
input: a
output a
input: b
output: a b
input c:
output a b c .

Any clues?????????? ????
char* array = new char[sizeof_of_array];

delete[] array;

But Andre is right use std::vector or std::string. Forget memory
allocation details and learn how to program.

john
Feb 13 '07 #3
Try that:

#include<vector >
#include<iostre am>
using namespace std;

vector<charvc;
char my_char;
for(;;;)
{
cin >my_char;
vc.push_back(my _char);
for(int i = 0; i < my_char.size(); i++)
{
cout << my_char[i] <<" ";
}
cout << endl;
}

Feb 13 '07 #4
On Feb 13, 2:00 am, John Harrison <john_androni.. .@hotmail.comwr ote:
whitehatmira... @gmail.com wrote:
Hi all
Im not quite sure how to use the new and delete for a whole array
dynamically.
Actually i want that, a user inputs a char in a single char array.
Everytime he inputs a char he creates a new array of (previous size +
1), he then copies the content of the old array in to the new array
and adds the new input at the end and deletes the old array. I guess
he has to pass the pointer to the array everytime.
For example.
input: a
output a
input: b
output: a b
input c:
output a b c .
Any clues?????????? ????

char* array = new char[sizeof_of_array];

delete[] array;

But Andre is right use std::vector or std::string. Forget memory
allocation details and learn how to program.
Wellll.....not quite. Having a firm grasp on memory allocation/
deallocation is a Good Thing (TM) to have in *any* programming
language. Yes, even if that language is garbage collected and you
never have to worry about the specifics.

You don't necessarily need to learn the finer details of memory paging
and cache hits vs misses but being able to write a function that is
capable of dynamically reallocating an array is IMHO something that
every programmer should know regardless of language. If you lose sight
of things like this you won't realize why your program is leaking
copius amounts of memory and you'll end up with bad software.

That little tirade out of the way, once you learn enough about memory
allocation/deallocation/reallocation using classes like std::vector or
std::string is great and even recommended. They save you the time of
writing those classes yourself. They're *thouroughly* tested and
stable. All signs of good code.
john
Back to the original posting though,
Im not quite sure how to use the new and delete for a whole array
dynamically.
Syntax for new'ing an array is like this:

int array[] = new int[array_size] ;

Syntax for deleting an array is like:

delete [] array ;
I guess
he has to pass the pointer to the array everytime.
Well, if you're not passing the array into the method somehow
( explicit argument, member variable of a class, etc ) you're going to
have a hard time modifying it.

A non-C++ method signature for doing something like this would be:

int
add_member( char array[], int current_size ) ;

In 'real' C++ this would be accomplished using a std::vector or some
other container as mentioned.

HTH,
Paul Davis

Feb 13 '07 #5
On 13 Feb, 09:14, Angelina_Novach ...@yahoo.com wrote:
Try that:

#include<vector >
#include<iostre am>
using namespace std;

vector<charvc;
char my_char;
for(;;;)
{
cin >my_char;
vc.push_back(my _char);
for(int i = 0; i < my_char.size(); i++)
i < vc.size()

In this loop, i should probably be std::vector<cha r>::size_type. My
personal preference would be for pre-increment, rather than post-
increment too, although in this case it makes no difference in
practice.
{
cout << my_char[i] <<" ";
cout << vc[i] <<" ";

Gavin Deane

Feb 13 '07 #6
On 13 Feb, 09:20, "paul.joseph.da ...@gmail.com"
<paul.joseph.da ...@gmail.comwr ote:
That little tirade out of the way, once you learn enough about memory
allocation/deallocation/reallocation using classes like std::vector or
std::string is great and even recommended. They save you the time of
writing those classes yourself. They're *thouroughly* tested and
stable. All signs of good code.
Any competent C++ programmer should of course know how to answer the
OP's question using new[] and delete[] explicitly (and should know to
use a container in preference). However, there is no need for someone
learning C++ to wait until they understand how to answer the question
the hard way before learning how to use standard containers to answer
the question the easy way. I expect that is what Andre Kostur and John
Harrison were getting at when they suggested std::vector to the OP.

The one time that advice isn't helpful of course, is when the OP is
doing this specifically to learn about new[] and delete[]. But that
wasn't mentioned as a specific goal. From what was written, the OP
could equally well have been using manual memory management simply
because they didn't know there was a better way.

Gavin Deane

Feb 13 '07 #7
On Feb 13, 4:50 am, "Gavin Deane" <deane_ga...@ho tmail.comwrote:
On 13 Feb, 09:20, "paul.joseph.da ...@gmail.com"

<paul.joseph.da ...@gmail.comwr ote:
That little tirade out of the way, once you learn enough about memory
allocation/deallocation/reallocation using classes like std::vector or
std::string is great and even recommended. They save you the time of
writing those classes yourself. They're *thouroughly* tested and
stable. All signs of good code.

Any competent C++ programmer should of course know how to answer the
OP's question using new[] and delete[] explicitly (and should know to
use a container in preference). However, there is no need for someone
learning C++ to wait until they understand how to answer the question
the hard way before learning how to use standard containers to answer
the question the easy way. I expect that is what Andre Kostur and John
Harrison were getting at when they suggested std::vector to the OP.

The one time that advice isn't helpful of course, is when the OP is
doing this specifically to learn about new[] and delete[]. But that
wasn't mentioned as a specific goal. From what was written, the OP
could equally well have been using manual memory management simply
because they didn't know there was a better way.

Gavin Deane
I guess I didn't specify clearly enough that my response was aimed at
this line:

'Forget memory allocation details and learn how to program.'

Which translates to: "Forget how to add and subtract numbers, use a
calculator instead."

Personally, I'd rather teach someone to fish than be stradled with
fishing for them.

Paul Davis

Feb 13 '07 #8

pa************* **@gmail.com wrote: >>
>
Syntax for new'ing an array is like this:

int array[] = new int[array_size] ;
int *array = new int[array_size] ;

--
Maksim A. Polyanin

"In thi world of fairy tales rolls are liked olso"
/Gnume/
Feb 13 '07 #9
Actually i have just shifted from c to c++. I have not yet encountered
vector, anyway letme see what i can do with all the info i got here,
ill get back when i have some more code written.

Thank you all of u.

PS: I like that saying
>>Personally, I'd rather teach someone to fish than be stradled with
fishing for them.

Feb 14 '07 #10

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

Similar topics

9
4957
by: Tom | last post by:
What I mean is why can I only allocate const size stuff on the stack in C++? If I want to allocate a variable amount I need to use the OS API (Win32 in my case). Thanks, Tom.
6
8200
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory allocation to create object of that class ? 2. If it says "dynamic memory allocation", is it mean the following code : DestinationAddress* dest = new DestinationAddress(); // code 1
11
3037
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
24
19065
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array is faster than malloc, but dynamic memory allocation is more flexible. Please comment... thanks.
7
8215
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
1
7960
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this? Second Program: /* Best Method to allocate memory for 2D Array because it's ...
3
2986
by: ranjeetasharma81 | last post by:
Hi all, I have a big C-cod, in which there are lots of dynamic memory allocation used. I want to replace dynamic memroy allocation by static arrays. The following are the problems that i am facing: 1- From structure and dynamic memory allocation point of view, the code is very complicated. The code has various “nested structures” with a number of levels. The size of memory allocated for pointer to structure or its pointer...
14
3820
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is this stored in stack. If yes whether it will be deleted on exiting from the function. is dynamic memory allocation needed for this purpose
10
4410
by: swornavidhya.mahadevan | last post by:
Which allocation (Static / Dynamic) is suitable for the situation when we are trying to allocate for a overloaded memory when the memory is full and no space to allocate. What will happen if both the allocation is impossible. Sworna vidhya
0
2228
by: Kelly Bert Manning | last post by:
I didn't get any hits when I searched deja for DSN1COPY dynamic allocation so either everyone else had no trouble realizing that this happens or nobody else has ever come across it. I've used DSN1COPY for years for moving data between z/OS subsystems for testing, and for refreshing tables from Image Copies for regression testing. Developers seem to find it less confusing to use the same JCL to move data between 1 of 14 test...
0
8256
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8189
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
8497
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
7184
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...
1
6118
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4089
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...
0
4193
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1500
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.