Again Dynamic memory allocation

W

whitehatmiracle

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??????????????
 
P

peter koch

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
 
I

Ismo Salonen

peter said:
On Feb 16, 10:45 am, "(e-mail address removed)"
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
 
W

whitehatmiracle

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......

:)
 
J

John Harrison

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
 
W

whitehatmiracle

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.
 
G

Gavin Deane

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
 
W

whitehatmiracle

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<<" ";
cout<<"\n";
}

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

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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 = arr;

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.
 
W

whitehatmiracle

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 = arr;

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.


let me try and ill be back...
 
G

Grizlyk

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 = arr;


if "ptr" is POD "memcpy" can be used to copying.
In some CPU "ptr = arr, ++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/
 

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

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top