• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

c++ problem

emory

n00b
Joined
Nov 6, 2005
Messages
18
First off, before I started this program, Id never made a class in c++ just to give you an idea of my competency level, though I know java backwards and forwards.
Code:
void Graph::addRow(int row[], int rowIndex){
        for(int i = 0; i < vertexCount; i++){
                adjacencyList[rowIndex][i] = new Vertex(row[i]);
                cout<<(*adjacencyList[rowIndex][i]).getWeight()<<" ";
        }
 
        cout<<"testing row"<<endl;
                for(int i = 0; i < vertexCount; i++)
                cout<<(*adjacencyList[rowIndex][i]).getWeight()<<" ";
}

adjacencyList is a 2d array of pointers to Vertices. This function prints out the weight of the row in the first loop perfectly. After "testing row" prints, I get a segfault.

My only working theory is that the memory is going out of scope, but I dont think it should be working that way.
 
Gah, keep missing key points. one sec.
I'm pretty sure your right that it is not going out of scope, but I think your problem might be comming from the dereferencing operator. Too be safe, and make sure your code isn't dereferencing the wrong thing, wrap the adjacencyList inside parenthesis as well. Like this

Code:
void Graph::addRow(int row[], int rowIndex){
        for(int i = 0; i < vertexCount; i++){
                adjacencyList[rowIndex][i] = new Vertex(row[i]);
                cout<<(*[color=red](adjacencyList[rowIndex][i])[/color]).getWeight()<<" ";
        }
 
        cout<<"testing row"<<endl;
                for(int i = 0; i < vertexCount; i++)
                cout<<(*[color=red](adjacencyList[rowIndex][i])[/color]).getWeight()<<" ";
}

I don't think thats the problem, but its probably not a bad idea. You can also use the -> notation

Code:
void Graph::addRow(int row[], int rowIndex){
        for(int i = 0; i < vertexCount; i++){
                adjacencyList[rowIndex][i] = new Vertex(row[i]);
                cout<<adjacencyList[rowIndex][i][color=red]->[/color]getWeight()<<" ";
        }
 
        cout<<"testing row"<<endl;
        for(int i = 0; i < vertexCount; i++)
        {
                cout<<adjacencyList[rowIndex][i][color=red]->[/color]getWeight()<<" ";
        }
}

I added the { } around the second for loop as well (personal preference thing)
 
I actually think I found the problem while talking to our friendly neighborhood disgruntled phd student. The code in question was changed at about 12-1ish last night, but I think I forgot to change how I was allocating the memory. I actually never allocated the memory for the row arrays because origionally I was just copying over the array from the parameter.

Xipher, thanks for the tips.
 
Back
Top