Feeding in data from a file to a dynamically allocated array in C++

1

So, I'm doing a college assignment where basically what is needed is that you use a pointer that points to a dynamically allocated array of the correct size based on input from a data file. The problem is, I keep getting different errors when the program runs. The program compiles perfectly fine, but yet when it runs, it gives me this: "0xC0000005: Access violation reading location 0xFDFDFDFD."

Here is the code I'm using (do note that some of the code was written by my professor and before you ask, I attempted to contact him, and never got anything back from him):

Written by Professor

struct emprec   /*Employee record with two fields*/
{
    int id;
    float salary;
};
typedef emprec* emprecptr; /*Employee record pointer type*/
typedef emprecptr* indexarr; /* Pointer to a dynamic array of employee record pointers*/

Written by me

void readdata(indexarr& ptrs, int& size)
{
    size = 0;

    ptrs = nullptr;
    ptrs = new emprecptr[size];
    ifstream in;
    in.open("p1data.txt");
    if (!in) {
        cout << "Unable to open p1data.txt"; //if the file is not in the folder
        exit(1);
    }
    while (!in.eof()) {
        in >> ptrs[size]->id;        //putting to read line for id
        in.ignore();
        in >> ptrs[size]->salary;       //putting to read line for salary
        in.ignore();
        size = size + 1;

    }
    in.close();
};

c++
asked on Stack Overflow Sep 12, 2020 by noobprogrammer26 • edited Sep 12, 2020 by noobprogrammer26

2 Answers

2
size = 0;

ptrs = nullptr;
ptrs = new emprecptr[size];

This sets size to 0, then creates an array, using new containing size values. Since size is 0 this creates an array containing exactly 0 values.

ptrs is a completely empty array (and the first assignment to a nullptr does not do anything useful, by the way).

    in >> ptrs[size]->id;        //putting to read line for id

The shown code then proceeds and attempts to put data into this array. But this array is completely empty. Its size is zero. It does not contain anything, so this doesn't go very far, and your program crashes.

And even if that wasn't the case, all that ptrs is, is an array of pointers. Even assuming that this array, itself is correctly allocated they are pointing to absolutely nothing whatsoever, until you actually new those pointers also.

    size = size + 1;

It looks like you believe that just because you incremented size here, and it was used earlier in the program to new an array, this must mean that this array will automatically grow by one value, too. This is not true. C++ does not work this way. Your C++ program does exactly what you tell it to do, one statement at a time. Your program news an array of a given size (zero size). That's what happens. The End. Nothing more will happen to that array, until you implement the code that does that.

Your homework assignment requires you to implement intelligent, sophisticated, memory management and fully understand how memory management works in C++. You need to implement moderately complicated logic that initially creates an array of some reasonable size, an array of ten or twenty pointers (pick some reasonable number yourself), then read one record at a time, new-ing each new record (apparently), and saving the newed pointer to it in this array. If the array becomes full, you will need to new a larger array, copy all the pointers from the smaller array to the larger array, delete the smaller array, and continue reading, until all records are read.

I am assuming that your assignment requires you to dynamically grow an array of indirect pointers. It is not quite clear, but it's possible that you simply need to dynamically grow the array of records themselves, instead. If so, this becomes slightly simpler, but the basic approach will be the same.

while (!in.eof()) {

Oh, and and this is also a nasty bug that you will need to fix, too.

answered on Stack Overflow Sep 12, 2020 by Sam Varshavchik • edited Sep 13, 2020 by Sam Varshavchik
0

Looks like the memory that you're trying to reference is not allocated to the ptrs variable. It is beyond the size of memory that you've have allocated or it. Could you please check the size of variables allocated, that might give you some idea.

answered on Stack Overflow Sep 12, 2020 by Vallabh Karanjkar

User contributions licensed under CC BY-SA 3.0