Re: [Dev-C++] delete statement gives segmentation fault error
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: David M. <ci...@ya...> - 2003-08-15 08:44:41
|
Hmm, it works fine for me.
I tried a few changes to your main() which follow:
int main(int argc, char* argv[])
{
argv[0] = "dc";
argv[1] = "rate = 2.5; distance = rate * 5;";
argc = 2;
switch (argc) {
case 1:
input = &cin ;
break;
case 2:
//Modified
input = new istringstream(argv[1]);
if(input == NULL) {
cout << "Error with new" << endl;
return 1;
}
if(!input->good()) {
cout << "Stringstream not created properly" <<
endl;
}
break;
default:
error("too many arguements");
return 1;
}
table ["pi"] = 3.1415926535897932385;
table ["e"] = 2.718281828459042354;
while (*input) {
get_token();
if (curr_tok == END) break;
if (curr_tok == PRINT) continue;
cout << expr(false) << '\n';
}
if (input != cin) {
//Modified
cout << "About to delete input" << endl;
delete input;
}
//Modified
system("PAUSE");
return no_of_errors;
}
My only other change is instead of including
#include <strstream>
I included
#include <sstream>
Your code should work fine though. All I did was do some
error checking to
see if something is going wrong before we reach the delete.
----- Original Message -----
From: "Dawood Vora" <da...@ku...>
To: "Dev C++" <dev...@li...>
Sent: Thursday, August 14, 2003 8:39 PM
Subject: [Dev-C++] delete statement gives segmentation
fault error
> Hello every body,
> below is a straight code from Stroustrup's classic. I am
running
> bloodshed DEV-C++. At the end, it is trying to delete the
object "input"
> using delete statement. It straightaway gives me invalid
memory or
> segmentation fault error. Also, if I comment the delete
statement, the
> code works fine as it should be. Actually it happens,
only when I try to
> pass the command line arguement. As u all know, it is the
famous
calculator
> example from the 6th chapter of the book. Right now, I
am in the 6th
> chapter, so I do not have a very good idea about new and
delete
> statements. According to me the code should work, as we
are trying to
> delete the object we explicitly created.
>
> #include <iostream>
> #include <string>
> #include <map>
> #include <cctype>
> #include <strstream>
>
> enum Token_value{
> NAME, NUMBER,END,
> PLUS = '+',MINUS = '-', MUL = '*', DIV = '/',
> PRINT = ';', ASSIGN = '=', LP = '(', RP = ')'
> };
>
> Token_value curr_tok = PRINT;
>
> double number_value;
> string string_value;
> istream* input;
>
>
> int no_of_errors;
> double error(const string& s) {
> no_of_errors++;
> cerr<<"error: "<<s<<'\n';
> return 1;
> }
>
> Token_value get_token() {
> char ch;
> do { // skip whitespace except \n
> if (!((*input).get(ch))) return curr_tok = END;
> } while (ch!= '\n' && isspace(ch));
>
> switch (ch) {
> case ';':
> case '\n':
> return curr_tok = PRINT;
> case '*':
> case '/':
> case '+':
> case '-':
> case '(':
> case ')':
> case '=':
> return curr_tok = Token_value(ch);
>
> case '0':
> case '1':
> case '2':
> case '3':
> case '4':
> case '5':
> case '6':
> case '7':
> case '8':
> case '9':
> case '.':
> (*input).putback(ch);
> (*input)>> number_value;
> return curr_tok = NUMBER;
> default:
> if (isalpha(ch)) {
> string_value = ch;
> while ((*input).get(ch) && isalnum(ch))
> string_value+=ch;
> (*input).putback(ch);
> return curr_tok = NAME;
> }
> error ("bad token");
> return curr_tok = PRINT;
> }
> }
>
> //------------------------------------------------
prim(bool get)
> double expr(bool);
> map<string,double>table;
>
> double prim(bool get) { // handle primaries
> if (get) get_token();
>
> switch(curr_tok){
> case NUMBER: //floating point constant
> {
> double v = number_value;
> get_token();
> return v;
> }
> case NAME:
> {
> double& v = table[string_value];
> if (get_token() == ASSIGN)
> v = expr(true);
> return v;
> }
> case MINUS: // unary minus
> cout<<"reading negative number\n";
> return -prim(true);
> case LP:
> {
> double e = expr(true);
> if (curr_tok != RP) return error("')' expected");
> get_token(); //eat ')'
> return e;
> }
> default:
> return error ("primary expected");
> }
> }
>
> //------------------------------------------------
term(bool get)
> double term(bool get){ // multiply and divide
> double left = prim(get);
>
> for (;;)
> switch (curr_tok) {
> case MUL:
> left *= prim(true);
> break;
> case DIV:
> if (double d = prim(true)) {
> left /= d;
> break;
> }
> return error("divide by zero");
> default:
> return left;
> }
> }
>
> //------------------------------------------------
expr(bool get)
> double expr(bool get) { // add and subtract
> double left = term(get);
>
> for(;;) //forever
> switch (curr_tok) {
> case PLUS:
> left += term(true);
> break;
> case MINUS:
> left -= term(true);
> break;
> default:
> return left;
> }
> }
>
>
> int main(int argc, char* argv[])
> {
> argv[0] = "dc";
> argv[1] = "rate = 2.5; distance = rate * 5;";
> argc = 2;
> switch (argc) {
> case 1:
> input = &cin ;
> break;
> case 2:
> input = new istrstream(argv[1]);
> break;
> default:
> error("too many arguements");
> return 1;
> }
> table ["pi"] = 3.1415926535897932385;
> table ["e"] = 2.718281828459042354;
>
> while (*input) {
> get_token();
> if (curr_tok == END) break;
> if (curr_tok == PRINT) continue;
> cout << expr(false) << '\n';
> }
>
> if (input != cin) delete input;
> return no_of_errors;
>
> system("PAUSE");
> return 0;
> }
>
>
>
> -------------------------------------------------------
> This SF.Net email sponsored by: Free pre-built ASP.NET
sites including
> Data Reports, E-commerce, Portals, and Forums are
available now.
> Download today and enter to win an XBOX or Visual Studio
.NET.
>
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE:
http://www23.brinkster.com/noicys/devcpp/ub.htm
>
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
|