472,119 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

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::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.
Jul 19 '05 #1
1 3333
"Victor Bazarov" <v.********@attAbi.com> wrote in message
news:vg************@corp.supernews.com...
"Samuele Armondi" <sa****************@hotmail.com> 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
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by soni29 | last post: by
3 posts views Thread by bml | last post: by
7 posts views Thread by Luther Baker | last post: by
6 posts views Thread by JustSomeGuy | last post: by
8 posts views Thread by Randy Yates | last post: by
6 posts views Thread by James Aguilar | last post: by
3 posts views Thread by Tod Birdsall | last post: by
11 posts views Thread by icanoop | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.