-3

To specify the question above, I'm taking a class to learn c++. We recently just learned about dynamic arrays and classes. In a recent assignment, we were suppose to take information from a text file and initialize specific cells of the dynamic array inside of a constructor with the given struct and class (which is shown below). That being said, my problem is that whenever I try running the program it crashes.

class call_record
{
public:
    string firstname;
    string lastname;
    string cell_number;
    int relays;
    int call_length;
    double net_cost;
    double tax_rate;
    double call_tax;
    double total_cost;
};

class call_class
{
public:
    call_class();
    ~call_class();
    bool Is_empty();
    bool Is_full();
    int Search(const string key);
    void Add();
    void Remove(const string key);
    void Double_size();
    void Process();
    void Print();
private:
    int count;
    int size;
    call_record *call_DB;
};

// default constructor
call_class::call_class()
{

    size = 5;
    count = 0; 
    ifstream in; 
    in.open("callstats_data.txt");  

    while (!in.eof()) 
    {

        if(Is_full())
        {
            Double_size();
        }

        in >> call_DB[count].firstname
            >> call_DB[count].lastname
            >> call_DB[count].cell_number
            >> call_DB[count].relays
            >> call_DB[count].call_length;

        count++;  
    }

    in.close();  

}

To include more context, I found out that this problems arises mainly when I try reading a file into a dynamic array that is inside the constructor of a class. I tried using a static array instead of a dynamic array and it worked with no problems at all; however, I can't really use a static array. So I was wondering if there was specific way to place information from a text file into a dynamic array that is inside of a default constructor.

  • First of all: Change `call_record *call_DB;` to `std::vector call_DB;` to have a _dynamic array_. – πάντα ῥεῖ Nov 08 '16 at 22:19
  • You've not shown your `Is_full` or `Double_size` methods, and the problem is probably somewhere in one of those. `while (!in.eof())` is also likely to cause problems, but those will probably manifest as bad data rather than crashes. – Miles Budnek Nov 08 '16 at 22:21
  • Second: Mind [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – πάντα ῥεῖ Nov 08 '16 at 22:21
  • Third: Post a [MCVE] please. – πάντα ῥεῖ Nov 08 '16 at 22:22
  • Fourth: You never allocated memory for `call_DB` hence the segfault. It's undefined behavior to dereference an uninitialized pointer. – πάντα ῥεῖ Nov 08 '16 at 22:23
  • Run program with [the debugger that almost certainly came with your development environment](https://en.wikipedia.org/wiki/Debugger). The debugger will halt execution when the program crashes allowing you to inspect the variables involved in the crash. Sometimes the cause will be obvious and sometimes you will have to lay a few traps to see how the program got into such a bad state. – user4581301 Nov 08 '16 at 22:30

1 Answers1

0

The issue is that there is no memory allocated for call_DB[count];

Allocate it dynamically or statically before you use call_DB[count];

For example:

call_record call_DB[5];

instead of call_record *call_DB;

Also if call_DB has a specific size do not go over it, make sure you do not try to access above allocated memory:

while (!in.eof() && (count < 5))

If you declare call_DB using STL a std::vector then you will avoid above protection and your vector will grow as needed.

std::vector<call_record> call_DB;

Remember to use required include file:

#include <vector>

sg7
  • 5,365
  • 1
  • 30
  • 39