Problem with reference to pointers

The program below has a segmentation fault at the emboldened line. I'm not very good with these, could someone help explain what I've done wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <string>
#include <cmath>
#include <cstring>
#include <stack>
#include <algorithm>


using namespace std;

class Calculator
{
    public:
        Calculator();
        virtual ~Calculator();
        void readEquation();
        float nextNumber(int position, int*& );
        string equation;
    protected:
        float add(float firstNumber, float secondNumber);
        float subtract (float firstNumber, float secondNumber);
        float multiply (float number, float multiple);
        float divide (float numerator, float denominator);
        float squareRoot(float number);
        float square(float number);
    private:
        void temporaryPrintStack();
        float answer();
        stack<float>* numberStack;
};



Calculator::Calculator(){
    numberStack = new stack<float>();
    cout<<"Stack created.\n";
}

Calculator::~Calculator(){
    delete numberStack;
    numberStack=NULL;
    cout<<"Stack destroyed.\n";
}

float Calculator::add(float numOne,float numTwo){
    cout<<"Calculating "<<numOne<<" + "<<numTwo<<".\n";  
    return numOne+numTwo;
}

float Calculator::answer(){
     return this->numberStack->top();
}

void Calculator::temporaryPrintStack()
{
    cout<<"Stack: ";
    for(int index = 0; index <=  (int) this->numberStack->size();index++)
    {
        cout<<this->numberStack->top()<<", ";
        this->numberStack->pop();
    }
    cout<<endl;
}

float Calculator::nextNumber(int position,int*& returnPosition)
{
      stringstream convertCharacterTokensToString;
      for(string::iterator next = equation.begin()+position+1;next!=equation.end();next++)
      {
         if(*next >= '0' && *next<='9')
         {
          convertCharacterTokensToString<<*next;  
         }
         
         if(*next == '+' || *next == '-' || *next=='=')
         {
          string nextNumber;
          convertCharacterTokensToString>>nextNumber;
          cout<<"The next number is: "<<nextNumber<<".\n";
          *returnPosition = std::distance(this->equation.begin(),next);//ACCESS VIOLATION: SEGMENTATION FAULT
          cout<<"The next number is: "<<nextNumber<<".\n";
          return atof(nextNumber.c_str());         
         }
      }
}

void Calculator::readEquation()
{
    stringstream convertCharacterTokensToString;
    //add code to remove all spaces 
    for(string::iterator iterator = this->equation.begin();iterator!=this->equation.end();iterator++)
    {
        if(*iterator>='0' && *iterator<='9' || *iterator=='.')
            convertCharacterTokensToString<<*iterator;
        
        if(*iterator=='+')
        {
            string number;
            convertCharacterTokensToString>>number;
            cout<<number<<" pushed to stack."<<endl;
            this->numberStack->push(atof(number.c_str()));
            int* returnPosition;
            this->numberStack->push(this->add(this->numberStack->top(),
this->nextNumber(std::distance(this->equation.begin(),iterator), returnPosition)));
            this->temporaryPrintStack();
            cout<<"The return character is: "<<*returnPosition<<".\n";
        }

        if(*iterator=='=')
        {
            this->answer();
        }
    }
}

int main()
{
    Calculator *c=new Calculator();
    std::cout<<"Enter sum: ";
    getline(cin,c->equation);
    c->readEquation();
    system("pause");
    delete c; c = NULL;
    return 0;
}
Bjarne Stroustrup wrote:
Code that creates an object using new and then deletes it at the end of the same scope is ugly, error-prone, and inefficient.


Either don't put so much in an statement, or indent it a little better.
1
2
3
4
5
6
7
8
9
10
int* returnPosition; //uninitialised (points to garbage)
this->numberStack->push(
  this->add(
    this->numberStack->top(),
    this->nextNumber(
      std::distance(this->equation.begin(), iterator), 
      returnPosition
    )
  )
);
You dereference it in the function. ¿why is it asking for a pointer?, just pass a number by reference
i dont have anything constructive to add. however I do think reference to pointers look funny. I dont know maybe its because I dont see em that often. In fact I think this is the first time ive ever seen it used in live code. The extremely short article i read about em used an example of changing the pointer in the function and how because it was a copy it never changed the original. so they used a reference pointer and from inside the function they were able to change the original pointer outside the function. anyway the article was extremely short. so I dont know how much I took from it.
Last edited on
1
2
3
4
5
6
7
8
9
10
int* returnPosition; //uninitialised (points to garbage)
this->numberStack->push(
  this->add(
    this->numberStack->top(),
    this->nextNumber(
      std::distance(this->equation.begin(), iterator), 
      returnPosition
    )
  )
);


Above code look like functional programming paradigm to me. Function that return value is used as input to another function and then you nest them again and again. Provide quite "neat" code but personally to me, it can be hard to understand without proper indentation.

Is C/C++ created as a functional programming language? I thought they are imperative, procedural instead ? Hmmm.....
Topic archived. No new replies allowed.