0

I am a beginner in programming, I am trying to solve the problem I encountered in my assignment. Here I want to read a file and save each word as an element in a pointer pointing array. I declared pointer in Array class and reading the file in ReadAcc class. I am trying to process it outside these two classes.

class ReadAcc {

private:
double*_customerBalanceArray;

double read1;
ifstream f;
int counter = 0;
public:
ReadAcc() {
    _customerBalanceArray = new double[100];
}
int GetCounter() {
    return counter;
}

void Reading(string path)
{
    f.open(path);

    while (!f.eof()) 
    {
        f >> read1;
        _customerBalanceArray[counter] = read1;
        counter++;

    }
    f.close();
}

double* GetPointer() {
    return _customerBalanceArray;
}
void DeletePointer() {
    delete[] _customerBalanceArray;
}


};

int main()
{

    ReadAcc read;
    read.Reading("acc.txt");


    for (int i = 0; i < read.GetCounter(); i=i+2)
    {
        //cout << "Acc#   " << *(a.getCustomerBlanceArray) << "   Balance:   " << *(a.getCustomerBlanceArray) << endl;
    }
    read.DeletePointer();


}

When it compiles, the error says Exception thrown: read access violation.

Access violation writing location 0x013CE000. The pointer _customerBalanceArray value is now 0x013CE000. I do not know where it went wrong. Please help me. Thanks a million!!!

  • There's no array in `Array`, just a (null) pointer. There's no magic in pointers - if a member function `f` returned a `double`, would you expect `double d = object.f(); d = 0;` to modify a member of the object? – molbdnilo Aug 10 '16 at 09:47
  • Also, read [this](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – molbdnilo Aug 10 '16 at 09:47
  • Instead of using a class `Array`, take a look at container, like [`std::vector`](http://www.cplusplus.com/reference/vector/vector/) – Garf365 Aug 10 '16 at 09:52
  • Also, modifying a pointer inside a function doesn't change it outside. And pointing to a local object is a bad practice => at end of function, local variable disappear, but you still pointing to it. Any access to it will result to an undefined dehavior. – Garf365 Aug 10 '16 at 09:55
  • @Garf365 Thank you very much. I see I just made a typical mistake through your illustration, maybe I should use map to store the information – Thomas Han Aug 10 '16 at 10:03
  • Since it is an assignment, you should explain the specific design constraints (free standard library usage allowed?) and what you already planned in terms of a solution. – grek40 Aug 10 '16 at 10:08
  • @ThomasHan I don't know your requirments so I can't tell you is map is better than vector in your case. I just can say: use as much as possible standard library instead of redevelop yourself – Garf365 Aug 10 '16 at 10:09
  • @molbdnilo Thank you – Thomas Han Aug 10 '16 at 10:11
  • @grek40 yeah, free stand library usage. about the solution, I think I would just use pointer =new type[100] – Thomas Han Aug 10 '16 at 10:14
  • I'd strongly recommend to move away from the pointer approach and have a function signature like `void Reading(string path, std::vector& _customerBlance) { ... _customerBlance.push_back(read); ... }` – grek40 Aug 10 '16 at 12:08
  • @grek40 Thank you very much. I have looked up about the vector container. I used it instead, and it works well. But I really want to get my head around the usage of raw pointer. – Thomas Han Aug 11 '16 at 00:29

0 Answers0