473,401 Members | 2,068 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,401 software developers and data experts.

Again Dynamic memory 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 16 '07 #1
12 1746
On Feb 16, 10:45 am, "whitehatmira...@gmail.com"
<whitehatmira...@gmail.comwrote:
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.
[snip]
What prevents you from using a std::vector?

/Peter

Feb 16 '07 #2
peter koch wrote:
On Feb 16, 10:45 am, "whitehatmira...@gmail.com"
<whitehatmira...@gmail.comwrote:
--snip--
>Actually i want that, a user inputs a char in a single char array.
--snip--
>>
[snip]
What prevents you from using a std::vector?

/Peter
Why not use std::string ? imho it works quite well for strings.
If working with wide chars use std::wstring.

ismo
Feb 16 '07 #3
>What prevents you from using a std::vector?
>Why not use std::string ?
The book by Eric Nagler on c++, doesnt introduce std::vector or
string, in the beginning.
I know thats a lame excuse but, in one of the problems in the
beginning, where they have not yet introduced vectors, they ask to use
new and delete. Maybe just to let the programmer reinvent the wheel,
and then show him that there is something simpler so that he
understands the beauty, complexity or whatever......

:)

Feb 17 '07 #4
wh*************@gmail.com wrote:
>>What prevents you from using a std::vector?
Why not use std::string ?


The book by Eric Nagler on c++, doesnt introduce std::vector or
string, in the beginning.
I know thats a lame excuse but, in one of the problems in the
beginning, where they have not yet introduced vectors, they ask to use
new and delete. Maybe just to let the programmer reinvent the wheel,
and then show him that there is something simpler so that he
understands the beauty, complexity or whatever......

:)
Well ok, so what part are you stuck on?

It seems a fairly easy exercise. You obviously need a loop (I guess the
loop ends when the user type a newline or something). Each time round
the loop you must allocate one more char than last time, so you need a
varialbe that keeps track of how many chars you have allocated. You
obviously need a variable that points to the allocated chars. Both these
variables will get updated each time round the loop. Finally you need a
method of copying chars from the old allocated memory to the newliy
allocated memory. You could use another (inner) loop for that, or you
could use memcpy.

Post some code, it's easier to help if we can see where you've got so far.

john
Feb 17 '07 #5
On 17 Feb, 01:31, "whitehatmira...@gmail.com"
<whitehatmira...@gmail.comwrote:
What prevents you from using a std::vector?
Why not use std::string ?

The book by Eric Nagler on c++, doesnt introduce std::vector or
string, in the beginning.
Is changing to a different book an option? If so, you might find this
a lot more helpful:
http://web.archive.org/web/200605241...eratedcpp.com/

Gavin Deane

Feb 17 '07 #6
Well ok, so what part are you stuck on?

It seems a fairly easy exercise. You obviously need a loop (I guess the
loop ends when the user type a newline or something). Each time round
the loop you must allocate one more char than last time, so you need a
varialbe that keeps track of how many chars you have allocated. You
obviously need a variable that points to the allocated chars. Both these
variables will get updated each time round the loop. Finally you need a
method of copying chars from the old allocated memory to the newliy
allocated memory. You could use another (inner) loop for that, or you
could use memcpy.

Post some code, it's easier to help if we can see where you've got so far.
The problem seems clearer, ill do as you say, letme post some code
then.

Feb 18 '07 #7
Is changing to a different book an option? If so, you might find this
a lot more helpful:http://web.archive.org/web/200605241...eratedcpp.com/
Changing the book is not an option but following the link u gave me is
also an option.
Cool link... thank you!!

Feb 18 '07 #8
On 18 Feb, 06:15, "whitehatmira...@gmail.com"
<whitehatmira...@gmail.comwrote:
Is changing to a different book an option? If so, you might find this
a lot more helpful:http://web.archive.org/web/200605241...eratedcpp.com/

Changing the book is not an option but following the link u gave me is
also an option.
Cool link... thank you!!
You're welcome. What I should have mentioned in my previous post is
that that URL isn't where that site *usually* lives. See the recent
thread entitled "Accelerated C++" for a brief discussion and the
original URL.

Gavin Deane

Feb 18 '07 #9
The code

int main(){
cout<<"Enter a string: ";
get_char();
getch();
return 0;
}

void get_char(){
counter = 0;
while (getch != -1)
ch = getch();
counter++;
char* ptr = new char[counter];
/////////////////////
/////////////////////
////////////////
////////////////////
what do i do with the pointer and the 2 arrays? how
:(
...
...

print_array(*ptr, counter)
del(*ptr, counter);
}
}

void print_array(*char, counter){
for int i =0; i<counter, i++)
cout<<ptr[i]<<" ";
cout<<"\n";
}

void delete(*ptr, counter){
for (int i =0; i<counter i++)
delete [] ptr[i];
delete [] ptr;
}


Feb 19 '07 #10
On Feb 19, 2:46 pm, "whitehatmira...@gmail.com"
<whitehatmira...@gmail.comwrote:
The code

int main(){
cout<<"Enter a string: ";
get_char();
getch();
return 0;

}

void get_char(){
counter = 0;
You forgot the type: int counter = 0;

You need to keep track of the array:
char* arr = 0;
while (getch != -1)
That does not look quite right to me, try something like:
char ch;
std::cin >ch; // Read a character
while (ch != '.') // Terminate on .
{
counter++;
char* ptr = new char[counter];
Copy the content of the old array to the new one, the old contains
count - 1 characters:
for (int i = 0; i < count - 1; ++i)
ptr[i] = arr[i];

Put the character read onto the end of the array:
ptr[counter - 1] = ch;
print_array(*ptr, counter)
Almost, skip the * and it will be fine. If you use the * you will try
to dereference the pointer and the result will be a char (the first in
the array).
del(*ptr, counter);
No, this is more complicated that needed, you want to do two things;
delete the old array and make its point to the new one:
delete[] arr; // Notice the []
arr = ptr;

And read in a new character for the next iteration:
std::cin >ch;
}

}
You could move the print_array()-call to the end, just before the
std::cin, and use arr as parameter instead.

You have some syntax errors too but I think you can find and fix them
on your own.

--
Erik Wikström

Feb 19 '07 #11
On Feb 19, 8:14 pm, "Erik Wikström" <eri...@student.chalmers.se>
wrote:
On Feb 19, 2:46 pm, "whitehatmira...@gmail.com"

<whitehatmira...@gmail.comwrote:
The code
int main(){
cout<<"Enter a string: ";
get_char();
getch();
return 0;
}
void get_char(){
counter = 0;

You forgot the type: int counter = 0;

You need to keep track of the array:
char* arr = 0;
while (getch != -1)

That does not look quite right to me, try something like:
char ch;
std::cin >ch; // Read a character
while (ch != '.') // Terminate on .
{
counter++;
char* ptr = new char[counter];

Copy the content of the old array to the new one, the old contains
count - 1 characters:
for (int i = 0; i < count - 1; ++i)
ptr[i] = arr[i];

Put the character read onto the end of the array:
ptr[counter - 1] = ch;
print_array(*ptr, counter)

Almost, skip the * and it will be fine. If you use the * you will try
to dereference the pointer and the result will be a char (the first in
the array).
del(*ptr, counter);

No, this is more complicated that needed, you want to do two things;
delete the old array and make its point to the new one:
delete[] arr; // Notice the []
arr = ptr;

And read in a new character for the next iteration:
std::cin >ch;
}
}

You could move the print_array()-call to the end, just before the
std::cin, and use arr as parameter instead.

You have some syntax errors too but I think you can find and fix them
on your own.

--
Erik Wikström
let me try and ill be back...

Feb 22 '07 #12
whitehatmira...@gmail.com wrote:
>
counter++;
char* ptr = new char[counter];
Do not use POD pointers if the pointer is not member of class and the class
is tracing ownership of dynamic memory.
The piece of code with "ptr" is place of possible errors, due to memory
leakage if any throw exception befor you call "delete[]".
Erik Wikström wrote:
Copy the content of the old array to the new one, the old contains
count - 1 characters:
for (int i = 0; i < count - 1; ++i) ptr[i] = arr[i];
if "ptr[i]" is POD "memcpy" can be used to copying.
In some CPU "ptr[i] = arr[i], ++i" is less effective than "*ptr++ = *arr++".

--
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new

"In thi world of fairy tales rolls are liked olso"
/Gnume/
Feb 25 '07 #13

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

Similar topics

6
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...
24
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...
1
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...
14
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...
10
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...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
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...
0
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...

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.