length_error at memory location

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 886 Views - Last Post: 13 September 2020 - 01:56 PM Rate Topic: -----

#1 Deadweight77   User is online

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 04-May 19

length_error at memory location

Posted 12 September 2020 - 09:54 AM

So I've been making a program in which I am to open up two files containing matrices to read and display them for my class. I was able to pull this off in a different program of mine to test it, HOWEVER my professor is somewhat strict as he wants us to work off the templates he set out for us and I'm having trouble with it. Every time I run the program it aborts and found out that line 20 and 21 are to blame saying std::length_error at memory location in the main .cpp. I don't know why as I never got this error before and wish for help. I'm not sure if the full code is needed, but added it just in case as there are 3 files.

The main .cpp
#include "assignment02.h"

int main()
{
    ifstream is1, is2;          // input file streams
    open_files(is1, is2);    // open input files

    // number of rows and colums in matrices A and B 
    unsigned nrowsA = 5, ncolsA = 10, nrowsB = 5, ncolsB = 10;

    // read number of rows and colums for input matrices from data
    // files and assign number of rows and colums to resulting
    // matrix from values of input matrices; first two values in
    // data file are number rows and colums of a matrix
    is1 >> nrowsA >> ncolsA;
    is2 >> nrowsB >> ncolsB;


    // define matrices as two-dimensional vectors
    vector<vector<int> > A(nrowsA, vector<int>(ncolsA));
    vector<vector<int> > B(nrowsB, vector<int>(ncolsB));
   
    // read input values from data files
    read_data(is1, A);
    read_data(is2, B)/>/>;

   cout << "\nMatrix1: "; print_data(A);
   cout << "\nMatrix2: "; print_data(B)/>/>;
  
    return 0;
}



The header file
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>

using namespace std;

#ifndef H_assignment02
#define H_assignment02

// data files
#define FILE1 "matrix01.dat"
#define FILE2 "matrix02.dat"

#define ITEM_W 8 // no of spaces for each printed number

// to open two data files
void open_files(ifstream&, ifstream&);


// to read contents of two data files
void read_data(ifstream& , vector <vector <int> >& );


// to print contents of all matrices
void print_data(const vector <vector<int> >&);


#endif



The secondary .cpp
#include "assignment02.h"

// to open two data files
void open_files(ifstream& is1, ifstream& is2)
{
    is1.open(FILE1);
    is2.open(FILE2);
    if (is1.fail() || is2.fail())
    {
        cerr << "Error opening file(s)" << endl;
        exit(1);
    }
}

// to read contents of two data files
void read_data(ifstream& is, vector <vector <int> >& m)
{
    int temp = 0;

    for (int row = 0; row < 5; row++)
    {
        for (int col = 0; col < 10; col++)
        {
            is >> temp;
            m[row][col] = temp;
        }
    }

}

// to print contents of all matrices
void print_data(const vector <vector<int> >& m)
{
    for (int i = 0; m.size(); i++)
    {
        for (int j = 0; j < m[i].size(); j++)
        {
            cout << m[i][j] << " ";
        }
        cout << endl;
    }
}



This post has been edited by Deadweight77: 12 September 2020 - 09:55 AM


Is This A Good Question/Topic? 0
  • +

Replies To: length_error at memory location

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7530
  • View blog
  • Posts: 25,321
  • Joined: 05-May 12

Re: length_error at memory location

Posted 12 September 2020 - 10:35 AM

What is the exact error you are getting. Please don't summarize or paraphrase.
Was This Post Helpful? 0
  • +
  • -

#3 Deadweight77   User is online

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 04-May 19

Re: length_error at memory location

Posted 12 September 2020 - 11:16 AM

When running the debugger, I get this error: Unhandled exception at 0x766744C2 in matrix.cc.exe: Microsoft C++ exception: std::length_error at memory location 0x012FF16C. I'm confused on how to fix this and wish for help.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7530
  • View blog
  • Posts: 25,321
  • Joined: 05-May 12

Re: length_error at memory location

Posted 12 September 2020 - 12:10 PM

Part of learning to program is learning how to read the documentation. If you look up what the std::length_error is used for you will read:

Quote

Defines a type of object to be thrown as exception. It reports errors that result from attempts to exceed implementation defined length limits for some object.

This exception is thrown by member functions of std::basic_string and std::vector::reserve


So that means something is probably too big. Set a breakpoint at line 20. What values do you have for nrowsA, nrowsB, ncolsA, and ncolsB?
Was This Post Helpful? 0
  • +
  • -

#5 Deadweight77   User is online

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 04-May 19

Re: length_error at memory location

Posted 12 September 2020 - 12:27 PM

nrowsA and nrowsB=5 while ncolsA and nColsB=10
Was This Post Helpful? 0
  • +
  • -

#6 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1866
  • View blog
  • Posts: 6,737
  • Joined: 19-February 09

Re: length_error at memory location

Posted 12 September 2020 - 12:52 PM

Hi, in print_data there is a problem with the first for loop condition.
Was This Post Helpful? 0
  • +
  • -

#7 Deadweight77   User is online

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 04-May 19

Re: length_error at memory location

Posted 12 September 2020 - 01:22 PM

Thank you for pointing that out! Unfortunately, I'm still getting the same error as before and seeing how my vector is too big according to the documentation. I do appreciate that little flaw you pointed out however.
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg   User is online

  • member icon

Reputation: 5880
  • View blog
  • Posts: 17,867
  • Joined: 25-December 09

Re: length_error at memory location

Posted 12 September 2020 - 01:40 PM

Well you have this:

Quote

// read number of rows and colums for input matrices from data
// files and assign number of rows and colums to resulting
// matrix from values of input matrices; first two values in
// data file are number rows and colums of a matrix


Yet you have several "magic numbers" in your read, and write functions. Wouldn't it be nice if there was an easy way to get the sizes of the vectors?

Jim

Also here.
Was This Post Helpful? 2
  • +
  • -

#9 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7530
  • View blog
  • Posts: 25,321
  • Joined: 05-May 12

Re: length_error at memory location

Posted 12 September 2020 - 02:04 PM

Based on that thread on the other forum, that matrix data files there did not have the sizes before the matrix data. In this thread, the size should be on the file. I wonder if it OP is accidentally using the previous assignments files, and did not actually set a breakpoint as asked for in post #4.
Was This Post Helpful? 1
  • +
  • -

#10 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1866
  • View blog
  • Posts: 6,737
  • Joined: 19-February 09

Re: length_error at memory location

Posted 12 September 2020 - 02:38 PM

It looks as if it should work, you could try commenting out lines 15 and 16 temporarily.
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg   User is online

  • member icon

Reputation: 5880
  • View blog
  • Posts: 17,867
  • Joined: 25-December 09

Re: length_error at memory location

Posted 12 September 2020 - 03:29 PM

Quote

I wonder if it OP is accidentally using the previous assignments files, and did not actually set a breakpoint as asked for in post #4.

That's possible, however even if he is using the correct files he could be having problems. If the values happen to be less than those "magic numbers", or less than zero, getting a segmentation fault/exception wouldn't be a surprise.

Jim

This post has been edited by jimblumberg: 12 September 2020 - 03:30 PM

Was This Post Helpful? 0
  • +
  • -

#12 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7530
  • View blog
  • Posts: 25,321
  • Joined: 05-May 12

Re: length_error at memory location

Posted 12 September 2020 - 06:52 PM

Well, I guess there's one way to find out...

Deadweight77: Can you post the contents of "matrix01.dat" and "matrix02.dat"?
Was This Post Helpful? 0
  • +
  • -

#13 Deadweight77   User is online

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 04-May 19

Re: length_error at memory location

Posted 13 September 2020 - 07:38 AM

This is from matrix01.dat:
64 -79 -5 81 22 86 1 46 -79 19
33 -90 -83 61 -68 31 44 -53 -41 65
17 -49 -15 -97 65 -28 -20 20 -63 -72
75 77 -51 -53 35 47 -67 -63 -30 -47
32 -97 -61 48 40 -52 -43 -16 6 8

This is from matrix02.dat:
-89 -35 69 46 -87 27 -99 -51 -81 64
49 -74 64 34 -12 -42 45 -68 86 -1
90 -95 79 -18 -38 -18 76 9 73 -21
-56 83 21 89 6 33 17 6 59 12
-53 8 37 -89 -57 -97 -54 87 8 6
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7530
  • View blog
  • Posts: 25,321
  • Joined: 05-May 12

Re: length_error at memory location

Posted 13 September 2020 - 07:44 AM

Et viola! No size information at the beginning of the matrix.

So the supposed nrowsA == 5 and ncolsA == 10 according to post #4 is actually, nrowsA == 64 and nolsA == -79. No wonder you get an exception trying to set -79 columns.

Deadweight77: This is why I asked you to set a breakpoint and tell us what the actual values are. We could have gotten you moving along by post #5 if you had simply told us the truth, rather than only figuring this out at post #13.
Was This Post Helpful? 2
  • +
  • -

#15 jimblumberg   User is online

  • member icon

Reputation: 5880
  • View blog
  • Posts: 17,867
  • Joined: 25-December 09

Re: length_error at memory location

Posted 13 September 2020 - 07:46 AM

Quote

In this thread, the size should be on the file. I wonder if it OP is accidentally using the previous assignments files, and did not actually set a breakpoint as asked for in post #4.


Looks like you guessed correctly, however I question the "accidentally" part.

I also doubt that he knows how to use his debugger.




Jim
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2