Allocating istringstream objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Samuele Armondi

    Allocating istringstream objects

    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::istringstr eam** ArgStream;
    std::string* TempStrings;
    try
    {
    ArgStream = new std::istringstr eam*[CurrentCommandN umArgs];
    TempStrings = new std::string[CurrentCommandN umArgs];
    }
    catch (const std::bad_alloc& exception)
    {
    REPORT(exceptio n.what());
    return NULL;
    }
    for (int n = 0; n < CurrentCommandN umArgs; ++n)
    {
    std::string::si ze_type pos = Arguments.find_ first_of(',');
    TempStrings[n] = Arguments.subst r(0, pos);
    Arguments.erase (0, ++pos);
    std::cout<< (TempStrings[n]) << '\n';
    try
    {
    ArgStream[n] = new std::istringstr eam(TempStrings[n]);
    }
    catch (const std::bad_alloc& exception)
    {
    REPORT(exceptio n.what());
    return NULL;
    }
    }

    delete[] TempStrings;

    // The istringstream objects get used here

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

    delete[] ArgStream;
    --
    To contact me by email, remove _NOSPAM_ from the address.


  • Samuele Armondi

    #2
    Re: Allocating istringstream objects

    "Victor Bazarov" <v.Abazarov@att Abi.com> wrote in message
    news:vgue0kpudn lff2@corp.super news.com...[color=blue]
    > "Samuele Armondi" <sammyboyuk_NOS PAM_@hotmail.co m> wrote...[color=green]
    > > Since istringstream objects are not assignable, I'm using the following[/color]
    > code[color=green]
    > > 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[/color]
    > missing[color=green]
    > > something glaringly simple?
    > > Thanks in advance,
    > > S. Armondi
    > >
    > >
    > > std::istringstr eam** ArgStream;
    > > std::string* TempStrings;
    > > try
    > > {
    > > ArgStream = new std::istringstr eam*[CurrentCommandN umArgs];
    > > TempStrings = new std::string[CurrentCommandN umArgs];
    > > }
    > > catch (const std::bad_alloc& exception)
    > > {
    > > REPORT(exceptio n.what());
    > > return NULL;
    > > }
    > > for (int n = 0; n < CurrentCommandN umArgs; ++n)
    > > {
    > > std::string::si ze_type pos = Arguments.find_ first_of(',');
    > > TempStrings[n] = Arguments.subst r(0, pos);
    > > Arguments.erase (0, ++pos);
    > > std::cout<< (TempStrings[n]) << '\n';
    > > try
    > > {
    > > ArgStream[n] = new std::istringstr eam(TempStrings[n]);
    > > }
    > > catch (const std::bad_alloc& exception)
    > > {
    > > REPORT(exceptio n.what());
    > > return NULL;
    > > }
    > > }
    > >
    > > delete[] TempStrings;
    > >
    > > // The istringstream objects get used here
    > >
    > > for (n = 0; n < CurrentCommandN umArgs; ++n)
    > > delete ArgStream[n];
    > >
    > > delete[] ArgStream;[/color]
    >
    >
    > 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
    >
    >[/color]
    Ok, thanks for the tips
    Samuele


    Comment

    Working...