Allocating istringstream objects

Discussion in 'C++' started by Samuele Armondi, Jul 11, 2003.

  1. Hi everyone,
    Since istringstream objects are not assignable, I'm using the following code
    to allocate some dynamically. My question is: Is this the correct way of
    doing it? Am I deleting all the allocated memory correctly? Or am I missing
    something glaringly simple?
    Thanks in advance,
    S. Armondi


    std::istringstream** ArgStream;
    std::string* TempStrings;
    try
    {
    ArgStream = new std::istringstream*[CurrentCommandNumArgs];
    TempStrings = new std::string[CurrentCommandNumArgs];
    }
    catch (const std::bad_alloc& exception)
    {
    REPORT(exception.what());
    return NULL;
    }
    for (int n = 0; n < CurrentCommandNumArgs; ++n)
    {
    std::string::size_type pos = Arguments.find_first_of(',');
    TempStrings[n] = Arguments.substr(0, pos);
    Arguments.erase(0, ++pos);
    std::cout<< (TempStrings[n]) << '\n';
    try
    {
    ArgStream[n] = new std::istringstream(TempStrings[n]);
    }
    catch (const std::bad_alloc& exception)
    {
    REPORT(exception.what());
    return NULL;
    }
    }

    delete[] TempStrings;

    // The istringstream objects get used here

    for (n = 0; n < CurrentCommandNumArgs; ++n)
    delete ArgStream[n];

    delete[] ArgStream;
    --
    To contact me by email, remove _NOSPAM_ from the address.
    Samuele Armondi, Jul 11, 2003
    #1
    1. Advertising

  2. "Victor Bazarov" <> wrote in message
    news:...
    > "Samuele Armondi" <> wrote...
    > > Since istringstream objects are not assignable, I'm using the following

    > code
    > > to allocate some dynamically. My question is: Is this the correct way of
    > > doing it? Am I deleting all the allocated memory correctly? Or am I

    > missing
    > > something glaringly simple?
    > > Thanks in advance,
    > > S. Armondi
    > >
    > >
    > > std::istringstream** ArgStream;
    > > std::string* TempStrings;
    > > try
    > > {
    > > ArgStream = new std::istringstream*[CurrentCommandNumArgs];
    > > TempStrings = new std::string[CurrentCommandNumArgs];
    > > }
    > > catch (const std::bad_alloc& exception)
    > > {
    > > REPORT(exception.what());
    > > return NULL;
    > > }
    > > for (int n = 0; n < CurrentCommandNumArgs; ++n)
    > > {
    > > std::string::size_type pos = Arguments.find_first_of(',');
    > > TempStrings[n] = Arguments.substr(0, pos);
    > > Arguments.erase(0, ++pos);
    > > std::cout<< (TempStrings[n]) << '\n';
    > > try
    > > {
    > > ArgStream[n] = new std::istringstream(TempStrings[n]);
    > > }
    > > catch (const std::bad_alloc& exception)
    > > {
    > > REPORT(exception.what());
    > > return NULL;
    > > }
    > > }
    > >
    > > delete[] TempStrings;
    > >
    > > // The istringstream objects get used here
    > >
    > > for (n = 0; n < CurrentCommandNumArgs; ++n)
    > > delete ArgStream[n];
    > >
    > > delete[] ArgStream;

    >
    >
    > To simplify your program you could use vector<> instead of
    > arrays. You wouldn't need to 'new' or 'delete[]' either
    > TempStrings or ArgStream...
    >
    > Aside from that, seems OK.
    >
    > Victor
    >
    >

    Ok, thanks for the tips
    Samuele
    Samuele Armondi, Jul 11, 2003
    #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. cppaddict
    Replies:
    2
    Views:
    337
    Chris Uppal
    Aug 10, 2004
  2. hall
    Replies:
    4
    Views:
    425
  3. Replies:
    7
    Views:
    1,180
  4. Rakesh Kumar
    Replies:
    5
    Views:
    665
    James Kanze
    Dec 21, 2007
  5. yatko
    Replies:
    3
    Views:
    2,496
    yatko
    Dec 28, 2008
Loading...

Share This Page