Weird error

Hi!

As you will soon note, I am such a noob to this C++ thing...
I am creating (or at least trying) a calculator that allows you to make operation with mathematic vectors, as a way of getting some practice. I have recently come up with this error message when debuging: "An Access voilation was raised in your program". When running the program without debuging the error messaje displayed is the following: "The instruction in 0x0004015a6 refers to the memory in 0xfffffffe. The memory could not be written." (As I roughly translate from Spanish). The complier Im using is wxDev-C++.
Here I paste the code of the program, which is quite short and has explaining comments:

wxwidgets:
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
#include <cstdlib> 
#include <iostream> 

using namespace std; 

int main(int argc, char *argv[]) 
{ 
    char p[10]; 
    cout<<"Vectors Calculator"<<endl<<endl;         //introduction to the 
    cout<<"Developed by X"<<endl;    //program 
    GOBACK: 
    cout<<"Do you wish to start? (y/n)"<<endl;      //dumb question 
    cin>>p; 

    while ((*p!='y' && *p!='n')||p[1]!=00){         //will print "ERROR" 
        cout<<"ERROR"<<endl;                        //if you type any weird 
        cin>>p;                                     //stuff different from 'y' 
    }                                               //or "n". 
    if (*p=='y') 
        cout<<"Introduce the dimension of the vectors"<<endl; //Further instructions 
    else if (*p=='n'){ 
        char ex [10];                                      //Will let you leave the program 
        cout<<"Enter 'e' to exit"<<endl;                   //if you are not ready to start... 
        cin>>ex;                                           //bleh... 
        if (*ex=='e'){ 
            return EXIT_SUCCESS; 
            } 
        else if (*ex!='e'||ex[1]!=00){       //If you dont want to leave... 
            goto GOBACK; 
            } 
        } 
    int R;                                  //R woudl be the "dimension" 
    cin>>R;                                 //which you will now enter. 
    long double **vectorStore;              //Creates a matrix where vectors you 
    for(int i=0;i<100;i++){                 //introduce will be stored. 
        vectorStore[i]= new long double [R];//<------------------- 
    }                                                          //This loop creates 
    cout<<"Now enter the vectors elements one by one"<<endl;   // 100 vectors of the decired size. 
    cout<<"Vectors will be printed for control."<<endl; 
    for (int i=0; i<100; i++){       //This loop lets you introduce 
        vectorStore [i];             //vecotors elements. 
        for (int j=0; j<R; j++){ 
            cin>>vectorStore [i] [j];  
        }                              
        cout<<"V"<<(i+1)<<"=[ "; 
        for (int j=0; j<R; j++){                //This loop prints each vector 
            cout<<vectorStore [i] [j]<<" ";     //you introduce. 
        } 
        cout<<"]"<<endl; 
        cout<<"Do you wish to introduce another vector?(y/n)"<<endl; 
        char p[10]; 
        cin>>p; 
        while ((*p!='y' && *p!='n')||p[1]!=00){ //Same as the first "while"  
        cout<<"ERROR"<<endl;                    //but for the vector adding. 
        cin>>p;    
        } 
        if (*p=='y'){                    //Go on with the loop... 
            cout<<"Continue..."<<endl; 
        } 
        else if (*p=='n'){ 
            break;                      //Break the loop... 
        } 
    } 
    cout<<"Now you can:"<<endl; 
    cout<<"-Print all vectors.(p)"<<endl; 
    cout<<"-Operation with vectors.(o)"<<endl;   //More work needs to be done. 
system ("PAUSE"); 
    return 0; 
}



When I choose to break into the process (while debuging), it takes me to this line:
vectorStore[i]= new long double [R];//<-------------------
Which leaves me clueless, because I had been running this exact code, added some stuff, got the error for the first time, then went back to the good version and now I cant get this error off no matter what I do.

Thank you very much,
Benjamin.
Last edited on by admin
Segmentation faults are lots of fun.

That said, why aren't you using actual vectors?
vector<vector<long double> > vectorStore;
http://cplusplus.com/reference/stl/vector/

-Albatross

Thanks for the quick reply Albatross,

I havent used actual vector because my level of noobness doesnt allow it. Ill explain: I have roughly read a basic tutorial, one like the one this site provides. My intention is to keep reading, but I just wanted to give it a try, when I came up to this... a real P.I.A. I believe that using actual vectors may be more convenient, but am I doing something wrong here? or is just the complier and/or windows vista feels like making me misserable?

Again, thanks.
Benjamin.
Your issue is that you are trying to access the ith element of your vectorStore, but you haven't initialized the first row yet. You need something like:

vectorStore = new long double*[100];

Although I would really suggest you use vectors instead so you don't have to deal with memory management.
EDIT: firedraco beat me to the error.

After you get around all the template nonsense, vectors are actually quite easy to use. Here...
1
2
3
4
5
6
vector<type> naem; //Declaring a vector.
//You can use vectors as type, but be sure to include a space to differentiate <vector<type> > and <vector<type>>.
naem.at(location); //Getting stuff from a vector.
//Where location is an address just like the one you would use in an array.
naem.push_back(new_stuff); //Appending stuff to a vector.
//Where new_stuff has type... type. 


-Albatross
Last edited on
Well... It worked!!

Thank you!

I inserted that line between lines 34 and 35 of the first code and it worked as spected.
Now I will do as suggested and keep reading.

Benjamin.

EDIT: Albatross, thanks for the extra explanation. You can consider this threat solved.

thank you both guys.
Last edited on
Topic archived. No new replies allowed.