Dynamic allocation

Discussion in 'C++' started by whitehatmiracle@gmail.com, Feb 13, 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 13, 2007
    #1
    1. Advertising

  2. Andre Kostur Guest

    "" <> wrote in
    news::

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


    Forget arrays. Look up std::vector.
     
    Andre Kostur, Feb 13, 2007
    #2
    1. Advertising

  3. 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.
    >
    > For example.
    > input: a
    > output a
    > input: b
    > output: a b
    > input c:
    > output a b c .
    >
    > Any clues??????????????
    >


    char* array = new char[sizeof_of_array];

    delete[] array;

    But Andre is right use std::vector or std::string. Forget memory
    allocation details and learn how to program.

    john
     
    John Harrison, Feb 13, 2007
    #3
  4. Guest

    Try that:

    #include<vector>
    #include<iostream>
    using namespace std;

    vector<char> vc;
    char my_char;
    for(;;;)
    {
    cin >> my_char;
    vc.push_back(my_char);
    for(int i = 0; i < my_char.size(); i++)
    {
    cout << my_char <<" ";
    }
    cout << endl;
    }
     
    , Feb 13, 2007
    #4
  5. Guest

    On Feb 13, 2:00 am, John Harrison <> wrote:
    > 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.

    >
    > > For example.
    > > input: a
    > > output a
    > > input: b
    > > output: a b
    > > input c:
    > > output a b c .

    >
    > > Any clues??????????????

    >
    > char* array = new char[sizeof_of_array];
    >
    > delete[] array;
    >
    > But Andre is right use std::vector or std::string. Forget memory
    > allocation details and learn how to program.
    >


    Wellll.....not quite. Having a firm grasp on memory allocation/
    deallocation is a Good Thing (TM) to have in *any* programming
    language. Yes, even if that language is garbage collected and you
    never have to worry about the specifics.

    You don't necessarily need to learn the finer details of memory paging
    and cache hits vs misses but being able to write a function that is
    capable of dynamically reallocating an array is IMHO something that
    every programmer should know regardless of language. If you lose sight
    of things like this you won't realize why your program is leaking
    copius amounts of memory and you'll end up with bad software.

    That little tirade out of the way, once you learn enough about memory
    allocation/deallocation/reallocation using classes like std::vector or
    std::string is great and even recommended. They save you the time of
    writing those classes yourself. They're *thouroughly* tested and
    stable. All signs of good code.

    > john


    Back to the original posting though,

    > > Im not quite sure how to use the new and delete for a whole array
    > > dynamically.


    Syntax for new'ing an array is like this:

    int array[] = new int[array_size] ;

    Syntax for deleting an array is like:

    delete [] array ;

    > > I guess
    > > he has to pass the pointer to the array everytime.


    Well, if you're not passing the array into the method somehow
    ( explicit argument, member variable of a class, etc ) you're going to
    have a hard time modifying it.

    A non-C++ method signature for doing something like this would be:

    int
    add_member( char array[], int current_size ) ;

    In 'real' C++ this would be accomplished using a std::vector or some
    other container as mentioned.

    HTH,
    Paul Davis
     
    , Feb 13, 2007
    #5
  6. Gavin Deane Guest

    On 13 Feb, 09:14, wrote:
    > Try that:
    >
    > #include<vector>
    > #include<iostream>
    > using namespace std;
    >
    > vector<char> vc;
    > char my_char;
    > for(;;;)
    > {
    > cin >> my_char;
    > vc.push_back(my_char);
    > for(int i = 0; i < my_char.size(); i++)


    i < vc.size()

    In this loop, i should probably be std::vector<char>::size_type. My
    personal preference would be for pre-increment, rather than post-
    increment too, although in this case it makes no difference in
    practice.

    > {
    > cout << my_char <<" ";


    cout << vc <<" ";

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

    On 13 Feb, 09:20, ""
    <> wrote:
    > That little tirade out of the way, once you learn enough about memory
    > allocation/deallocation/reallocation using classes like std::vector or
    > std::string is great and even recommended. They save you the time of
    > writing those classes yourself. They're *thouroughly* tested and
    > stable. All signs of good code.


    Any competent C++ programmer should of course know how to answer the
    OP's question using new[] and delete[] explicitly (and should know to
    use a container in preference). However, there is no need for someone
    learning C++ to wait until they understand how to answer the question
    the hard way before learning how to use standard containers to answer
    the question the easy way. I expect that is what Andre Kostur and John
    Harrison were getting at when they suggested std::vector to the OP.

    The one time that advice isn't helpful of course, is when the OP is
    doing this specifically to learn about new[] and delete[]. But that
    wasn't mentioned as a specific goal. From what was written, the OP
    could equally well have been using manual memory management simply
    because they didn't know there was a better way.

    Gavin Deane
     
    Gavin Deane, Feb 13, 2007
    #7
  8. Guest

    On Feb 13, 4:50 am, "Gavin Deane" <> wrote:
    > On 13 Feb, 09:20, ""
    >
    > <> wrote:
    > > That little tirade out of the way, once you learn enough about memory
    > > allocation/deallocation/reallocation using classes like std::vector or
    > > std::string is great and even recommended. They save you the time of
    > > writing those classes yourself. They're *thouroughly* tested and
    > > stable. All signs of good code.

    >
    > Any competent C++ programmer should of course know how to answer the
    > OP's question using new[] and delete[] explicitly (and should know to
    > use a container in preference). However, there is no need for someone
    > learning C++ to wait until they understand how to answer the question
    > the hard way before learning how to use standard containers to answer
    > the question the easy way. I expect that is what Andre Kostur and John
    > Harrison were getting at when they suggested std::vector to the OP.
    >
    > The one time that advice isn't helpful of course, is when the OP is
    > doing this specifically to learn about new[] and delete[]. But that
    > wasn't mentioned as a specific goal. From what was written, the OP
    > could equally well have been using manual memory management simply
    > because they didn't know there was a better way.
    >
    > Gavin Deane


    I guess I didn't specify clearly enough that my response was aimed at
    this line:

    'Forget memory allocation details and learn how to program.'

    Which translates to: "Forget how to add and subtract numbers, use a
    calculator instead."

    Personally, I'd rather teach someone to fish than be stradled with
    fishing for them.

    Paul Davis
     
    , Feb 13, 2007
    #8
  9. Grizlyk Guest

    wrote: >>
    >
    > Syntax for new'ing an array is like this:
    >
    > int array[] = new int[array_size] ;


    int *array = new int[array_size] ;

    --
    Maksim A. Polyanin

    "In thi world of fairy tales rolls are liked olso"
    /Gnume/
     
    Grizlyk, Feb 13, 2007
    #9
  10. Guest

    Actually i have just shifted from c to c++. I have not yet encountered
    vector, anyway letme see what i can do with all the info i got here,
    ill get back when i have some more code written.

    Thank you all of u.

    PS: I like that saying
    >>Personally, I'd rather teach someone to fish than be stradled with

    fishing for them.
     
    , Feb 14, 2007
    #10
  11. Rolf Magnus Guest

    wrote:

    > Actually i have just shifted from c to c++. I have not yet encountered
    > vector, anyway letme see what i can do with all the info i got here,
    > ill get back when i have some more code written.
    >
    > Thank you all of u.
    >
    > PS: I like that saying
    >>>Personally, I'd rather teach someone to fish than be stradled with

    > fishing for them.


    Probably just based on the common saying: "Give a man a fish and you feed
    him for a day; teach a man to fish and you feed him for a lifetime."
     
    Rolf Magnus, Feb 14, 2007
    #11
  12. Guest

    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 14, 2007
    #12
  13. red floyd Guest

    OT: Fishing

    Rolf Magnus wrote:
    > wrote:
    >> PS: I like that saying
    >>>> Personally, I'd rather teach someone to fish than be stradled with

    >> fishing for them.

    >
    > Probably just based on the common saying: "Give a man a fish and you feed
    > him for a day; teach a man to fish and you feed him for a lifetime."
    >


    Of course, there's the alternate version: "Give a man a fish, and you
    feed him for a day; teach him to fish, and you give up your monopoly on
    fisheries."
     
    red floyd, Feb 14, 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. Rave

    dynamic array allocation

    Rave, Sep 25, 2003, in forum: C++
    Replies:
    5
    Views:
    628
    Kevin Goodsell
    Sep 25, 2003
  2. lestat
    Replies:
    2
    Views:
    701
    lestat
    Oct 11, 2003
  3. Ken
    Replies:
    24
    Views:
    3,876
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    992
    chris
    Oct 28, 2005
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,192
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page