473,480 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3406
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4107
by: soni29 | last post by:
hi, i'm reading a c++ book and noticed that the author seems to allocate memory differently when using classes, he writes: (assuming a class called CBox exists, with member function size()): //...
3
4703
by: bml | last post by:
Could you help and answer my questions of istringstream? Thanks a lot! 1. Reuse an "istringstream" istringstream ist; ist.str("This is FIRST test string"); ist.str("This is SECOND test...
7
748
by: Luther Baker | last post by:
Hi, My question is regarding std::istringstream. I am serializing data to an ostringstream and the resulting buffer turns out just fine. But, when I try the reverse, when the istringstream...
4
2304
by: dinks | last post by:
Hi I'm really new to c++ so please forgive me if this is really basic but im stuck... I am trying to make a data class that uses istringstram and overloaded << and >> operators to input and output...
6
2354
by: JustSomeGuy | last post by:
I am passing an istringstream to a function. I want that function to get a copy of the istringstream and not a refrence to it. ie when the function returns I want the istringstream to be...
8
2074
by: Randy Yates | last post by:
Why does this: string AWord(string& line) { return line; } bool MYOBJECT::MyFunction(string &line) { int day;
6
2981
by: James Aguilar | last post by:
Hello all, I am trying to use an istringstream to do some input off of cin by lines. The following snippet does not work: char buf; cin.getline(buf, 90); istringstream line1(string(buf));
3
5032
by: Tod Birdsall | last post by:
Hi All, The organization I am working for has created a new corporate website that used Microsoft's Pet Shop website as their coding model, and dynamically served up content, but cached each...
11
2877
by: icanoop | last post by:
I would like to do this MyClass x; istringstream("XXX") >> x; // Works in VC++ but not GCC instead of MyClass x; istringstream iss("XXX"); iss >> x; // Works in both GCC and VC++
0
7048
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7050
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6743
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6966
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4787
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2999
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
564
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.