Topic: exceptions - what()-member - memory management
Author: "Greg Colvin" <spam@me.not>
Date: 1998/07/25 Raw View
kanze@my-dejanews.com wrote in article <6p83kf$eef$1@nnrp1.dejanews.com>...
> I do remember some discussion, at one particular point of time,
suggesting
> that using dynamic memory in an exception would not be acceptable.
A long and bitter discussion, resolved in Austin by "The Gang of Four":
myself, Nathan, Steve Clamage, and Jerry Schwarz. In that afternoon's
library working group we had failed, for the umpteenth time, to resolve
the issue of "the return type of exception::what()". We sat down to
our evening meal resolved to settle the matter, and resolved that in
cases where we couldn't agree we would defer to the working paper.
Since we had failed to bring the paper to dinner we could each hope
that it said what we wanted, which kept us moving towards consensus.
And, as I recall, the redfish was excellent.
> (Obviously, if the exception in question is bad_alloc...) I suspect,
> however, that the point of having what return a char const* is only
> to allow bad_alloc to be able to return a static string, and not
> to require all of the others to do so.
Yes. Sorry for misremembering.
Greg Colvin
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Mark Fink <mark_fink_german@hotmail.com>
Date: 1998/07/27 Raw View
Hi!
My problem is yust a problem of the usage of the exception class. At the
moment I have no compiler that supports exceptions correctly, so I can't try
it out.
I'm writing a parser which throws an exception whenever it finds an error in
its token list. This exception must give the exact error-position. I want my
parser-exceptions to derive from this std::exception. Is there a manual,
tutorial or something where I can read about using this std::exception
correctly (the information in the ARM is to short for me)?
Thanks Mark
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: AllanW@my-dejanews.com
Date: 1998/07/27 Raw View
In article <35BC2859.E8E0B756@hotmail.com>,
Mark Fink <mark_fink_german@hotmail.com> wrote:
> Hi!
> My problem is yust a problem of the usage of the exception class. At the
> moment I have no compiler that supports exceptions correctly, so I can't try
> it out.
> I'm writing a parser which throws an exception whenever it finds an error in
> its token list. This exception must give the exact error-position. I want my
> parser-exceptions to derive from this std::exception. Is there a manual,
> tutorial or something where I can read about using this std::exception
> correctly (the information in the ARM is to short for me)?
You are allowed to throw any data type you like. Your specifications
are already fairly clear: you want to derive from std::exception, and
you want information on the exact error-position. Presumably you also
wish a description of the error.
Below, we'll assume that two integers are sufficient to report an
error position: the line number and the column number. If there is
only one source line, you can omit the line number; conversely, if
there are multiple input files you would add a filename member.
struct parser_error : public std::exception {
string m_what;
string m_err;
int m_line;
int m_column;
void set(const char*err, int line, int column) {
m_err = err;
m_line = line;
m_column = column;
char buffer[15];
itoa(line, buffer, 10);
m_what = "Parse error: " + m_err + " at line " + buffer;
itoa(column, buffer, 10);
m_what += ", column " + buffer;
}
parser_error(const char*err, int line, int column)
{ set(err, line, column); }
parser_error(const parser_error &p)
{ set(p.err.data(), p.line, p.column); }
virtual const char *what() { return m_what.data(); }
virtual const string err() { return m_err.data(); }
virtual int line() { return m_line; }
virtual int column() { return m_column; }
};
// ...
throw(parser_error("missing left parenthesis",
current_line, current_column));
// ...
try {
// ...
} catch (parser_error p) {
// Show the source line that got the error
std::cout << source_line[p.line()] << std::endl;
// Show the column number, if we have one
int i=p.column();
if (i) {
while (--i) std::cout << ' ';
std::cout << '^' << std::endl;
}
// Issue diagnostic message
std::cout << p.err() << std::endl;
// Tell user why we're exiting early
std::cerr << p.what();
exit(ERR_PARSE);
}
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/07/22 Raw View
Greg Colvin <spam@me.not> wrote:
>Mark Fink <mark_fink_german@hotmail.com> wrote in article
><35B5C31C.80ABB1F9@hotmail.com>...
>> The what()-member of the exception returns a pointer to char
>> (char*). Is this pointer valid after destroing the exception itself?
>> ... Or does it return a pointer to its internal data which is
>> unvalid after destroying the exception.
>
>The standard says
>
> virtual const char* what() const throw();
> Returns:
> An implementation-defined NTBS.
> Notes:
> The message may be a null-terminated multibyte string,
> suitable for conversion and display as a wstring.
>
>which unfortunately begs the question of lifetime. But it was our
>intention that the NTBS returned from what() have static storage
>duration, since the whole point of returning a char* was to be sure
>that the exception class did not have to allocate memory for the
>NTBS, and would not be dependant on the string class.
Sorry to create confusion, but my memory, the standard, and
the implementations I know about disagree with Greg. A prudent
coder would assume that the pointer is valid only as long as
the exception object itself exists.
The string might be stored physically in the exception object
itself, or statically. In any case you don't need to delete it,
but if you want to use it later you must make a copy.
--
Nathan Myers
ncm@nospam.cantrip.org http://www.cantrip.org/
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: kanze@my-dejanews.com
Date: 1998/07/23 Raw View
In article <6p5haf$gf2$1@shell7.ba.best.com>,
ncm@nospam.cantrip.org (Nathan Myers) wrote:
> Greg Colvin <spam@me.not> wrote:
> >Mark Fink <mark_fink_german@hotmail.com> wrote in article
> ><35B5C31C.80ABB1F9@hotmail.com>...
> >> The what()-member of the exception returns a pointer to char
> >> (char*). Is this pointer valid after destroing the exception itself?
> >> ... Or does it return a pointer to its internal data which is
> >> unvalid after destroying the exception.
> >
> >The standard says
> >
> > virtual const char* what() const throw();
> > Returns:
> > An implementation-defined NTBS.
> > Notes:
> > The message may be a null-terminated multibyte string,
> > suitable for conversion and display as a wstring.
> >
> >which unfortunately begs the question of lifetime. But it was our
> >intention that the NTBS returned from what() have static storage
> >duration, since the whole point of returning a char* was to be sure
> >that the exception class did not have to allocate memory for the
> >NTBS, and would not be dependant on the string class.
>
> Sorry to create confusion, but my memory, the standard, and
> the implementations I know about disagree with Greg. A prudent
> coder would assume that the pointer is valid only as long as
> the exception object itself exists.
I've just checked the VC++ implementation, and it uses a string object
internally, which confirms what Nathan says.
I do remember some discussion, at one particular point of time, suggesting
that using dynamic memory in an exception would not be acceptable.
(Obviously, if the exception in question is bad_alloc...) I suspect,
however, that the point of having what return a char const* is only
to allow bad_alloc to be able to return a static string, and not
to require all of the others to do so.
--
James Kanze +33 (0)1 39 23 84 71 mailto: kanze@gabi-soft.fr
+49 (0)69 66 45 33 10 mailto: jkanze@otelo.ibmmail.com
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique orient e objet --
-- Beratung in objektorientierter Datenverarbeitung
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/07/24 Raw View
Greg Colvin <spam@me.not> writes:
> The standard says
>
> virtual const char* what() const throw();
> Returns:
> An implementation-defined NTBS.
> Notes:
> The message may be a null-terminated multibyte string,
> suitable for conversion and display as a wstring.
>
> which unfortunately begs the question of lifetime. But it was our
> intention that the NTBS returned from what() have static storage
> duration, since the whole point of returning a char* was to be sure
> that the exception class did not have to allocate memory for the
> NTBS, and would not be dependant on the string class.
That isn't the interpretation of some implementors.
How do you implement what() ? Surely the static buffer doesn't
keeps its value forever. When does it get overwritten ?
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://pages.pratique.fr/~bonnardv/
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Mark Fink <mark_fink_german@hotmail.com>
Date: 1998/07/22 Raw View
Hi
I'm going to use exceptions, but there is on question to be answered
first. The what()-member of the exception returns a pointer to char
(char*). Is this pointer valid after destroing the exception itself?
That means, that the what() - member returns a copy of the
message-string and i have to manage the string memory. Or does it return
a pointer to its internal data which is unvalid after destroing the
exception.
At the moment i'm very confused about this char* stuff. It would be very
kind of you in helping me out.
Thanks Mark
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Greg Colvin <spam@me.not>
Date: 1998/07/22 Raw View
Mark Fink <mark_fink_german@hotmail.com> wrote in article
<35B5C31C.80ABB1F9@hotmail.com>...
> Hi
> I'm going to use exceptions, but there is on question to be answered
> first. The what()-member of the exception returns a pointer to char
> (char*). Is this pointer valid after destroing the exception itself?
> That means, that the what() - member returns a copy of the
> message-string and i have to manage the string memory. Or does it return
> a pointer to its internal data which is unvalid after destroing the
> exception.
> At the moment i'm very confused about this char* stuff. It would be very
> kind of you in helping me out.
>
> Thanks Mark
The standard says
virtual const char* what() const throw();
Returns:
An implementation-defined NTBS.
Notes:
The message may be a null-terminated multibyte string,
suitable for conversion and display as a wstring.
which unfortunately begs the question of lifetime. But it was our
intention that the NTBS returned from what() have static storage
duration, since the whole point of returning a char* was to be sure
that the exception class did not have to allocate memory for the
NTBS, and would not be dependant on the string class.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]