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

  2. peter koch Guest

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

    [snip]
    What prevents you from using a std::vector?

    /Peter
     
    peter koch, Feb 16, 2007
    #2
    1. Advertising

  3. Ismo Salonen Guest

    peter koch wrote:
    > On Feb 16, 10:45 am, ""
    > <> wrote:

    --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
     
    Ismo Salonen, Feb 16, 2007
    #3
  4. Guest

    >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, 2007
    #4
  5. 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
     
    John Harrison, Feb 17, 2007
    #5
  6. Gavin Deane Guest

    On 17 Feb, 01:31, ""
    <> 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.


    Is changing to a different book an option? If so, you might find this
    a lot more helpful:
    http://web.archive.org/web/20060524173252/http://www.acceleratedcpp.com/

    Gavin Deane
     
    Gavin Deane, Feb 17, 2007
    #6
  7. Guest


    > 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, 2007
    #7
  8. Guest

    , Feb 18, 2007
    #8
  9. Gavin Deane Guest

    On 18 Feb, 06:15, ""
    <> wrote:
    > > Is changing to a different book an option? If so, you might find this
    > > a lot more helpful:http://web.archive.org/web/20060524173252/http://www.acceleratedcpp.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
     
    Gavin Deane, Feb 18, 2007
    #9
  10. Guest

    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;
    }
     
    , Feb 19, 2007
    #10
  11. On Feb 19, 2:46 pm, ""
    <> wrote:
    > 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.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Feb 19, 2007
    #11
  12. Guest

    On Feb 19, 8:14 pm, "Erik Wikström" <>
    wrote:
    > On Feb 19, 2:46 pm, ""
    >
    > <> wrote:
    > > 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.
    >
    > --
    > Erik Wikström


    let me try and ill be back...
     
    , Feb 22, 2007
    #12
  13. Grizlyk Guest

    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 = 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/
     
    Grizlyk, Feb 25, 2007
    #13
    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:
    707
    Eric Sosman
    Mar 22, 2005
  2. Ken
    Replies:
    24
    Views:
    3,876
    Ben Bacarisse
    Nov 30, 2006
  3. chris
    Replies:
    6
    Views:
    993
    chris
    Oct 28, 2005
  4. Replies:
    1
    Views:
    288
    =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Feb 16, 2007
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,194
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page