Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Again Dynamic memory allocation

Reply
Thread Tools

Again Dynamic memory allocation

 
 
whitehatmiracle@gmail.com
Guest
Posts: n/a
 
      02-16-2007
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??????????????




i guess the code gotta be somthing like this:

int counter = 0;


int main(){
clrscr();
cout<<"Input a character: ";
get_char();
getch();
return 0;



}


get_char(){
ch = getch();
counter ++;
while (ch != -1){ ///exit if usr inputs -1

for (int i = 0; i<counter; i++)
dyn_array[i] = old_array[i];


dyn_array[i+1] = ch;
delete[] old_array;
print_array(*ptr_to_dyn_array, counter)



}


print_array(*ptr, int){
for (int i = 0; i =<counter;i++){
}


}


Something like this ............!!!!!?????
Im confused.........?????????????

 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      02-16-2007
On Feb 16, 10:45 am, "whitehatmira...@gmail.com"
<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.


First of all, any good reason not to use std::vector? It gives you
everything you need and saves you the hassle of allocating/
deallocating memory.

> i guess the code gotta be somthing like this:
>
> int counter = 0;
>
> int main(){
> clrscr();
> cout<<"Input a character: ";
> get_char();
> getch();
> return 0;
>
> }
>
> get_char(){
> ch = getch();
> counter ++;
> while (ch != -1){ ///exit if usr inputs -1


Allocate the new array here.

> for (int i = 0; i<counter; i++)
> dyn_array[i] = old_array[i];
>
> dyn_array[i+1] = ch;


No, 'i' is no longer in scope and this should not compile, but on
the other hand you know the size of the array and indexing to the last
element should be no problem.

> delete[] old_array;
> print_array(*ptr_to_dyn_array, counter)


ch = getch();

> }
>
> print_array(*ptr, int){
> for (int i = 0; i =<counter;i++){


std::cout << ptr[i] << " ";

> }
>
> }


--
Erik Wikström

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
An idea for heap allocation at near stack allocation speed Bjarke Hammersholt Roune C++ 14 03-06-2011 08:07 AM
Again Dynamic memory allocation whitehatmiracle@gmail.com C++ 12 02-25-2007 05:30 AM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
Dynamic memory allocation and memory leak... s.subbarayan C Programming 10 03-22-2005 02:48 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57