Again Dynamic memory allocation

Discussion in 'C++' started by whitehatmiracle@gmail.com, Feb 16, 2007.

  1. Guest

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


    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.........?????????????
    , Feb 16, 2007
    #1
    1. Advertising

  2. On Feb 16, 10:45 am, ""
    <> 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 = old_array;
    >
    > 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 << " ";

    > }
    >
    > }


    --
    Erik Wikström
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Feb 16, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    681
    Eric Sosman
    Mar 22, 2005
  2. Ken
    Replies:
    24
    Views:
    3,838
    Ben Bacarisse
    Nov 30, 2006
  3. chris
    Replies:
    6
    Views:
    974
    chris
    Oct 28, 2005
  4. Replies:
    12
    Views:
    516
    Grizlyk
    Feb 25, 2007
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,170
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page