1

I am trying to fix a method that takes a function that takes a root of a linked list and the number of vertices in a graph and organizes the linked list into an array of linked list based on the first vertex of an edge struct, for example, in a struct where an node Edge has firVertex = 1, sndVertex = 2, and weight = 2, it would be sorted into the first element of an array. and another node Edge that has another firVertex = 1 would be appended onto the previous one, and etc. I have fixed it many times but it is still giving a segmentation fault when running.

Thanks for the tips, really appreciate it. Unfortunately, I cannot leave the code up since its a school assignment.

1
  • Have you stepped through your code in the debugger? Commented Jan 31, 2014 at 20:43

3 Answers 3

1
Edge* arrayList [numberVertices]

Allocates on the stack, not the heap, and goges out of scope at the end of the function.

Sign up to request clarification or add additional context in comments.

Comments

0

The variable arrayList if declared as a local array. Therefore is is destroyed at the end of the function and shouldn't be returned. You should allocate it dynamically with a new:

Edge** arrayList = new Edge *[numberVertices];

By the way your compiler (if correctly configured) should warn you. Here is what g++ -Wall does:

bli.cpp: In function ‘Edge** _()’:
bli.cpp:5:9: warning: address of local variable ‘arrayList’ returned [-Wreturn-local-addr]
  Edge* arrayList[500];

Also this looks very suspicious to me:

for (int i=0; i<numberVertices; i++) { //initializes the array to NULL
   arrayList[numberVertices]=NULL;
}

you probably mean:

   arrayList[i]=NULL;

Comments

0

so why don't you tried StdLib? I personally recommend to avoid using pointer. you could use references (&) instead.

and if you got segmentation fault, are you able to debug till you found the line where it fails?

and you basically will got segmentation fault because you call Edge constructor with NULL as a root, and then try to reference root->next, but root is null...

3 Comments

it fails in both cases, you need to remove NULL from args of constructor
in that case, how would I linked the node to the previous elements in the array. could you give me some tips? thanks
look, the problem is only with string "root->next=this" if you send NULL to constructor, root will point to nothing, 0, this is access violation. I see in "else" case you do "last->next = .." instead you could pass "last" as an arg to constructor and it will set "last->next" and in "then" case I really have no idea that should be "root", I'm not sure I understand clearly that you trying to do. I'm talking only as compiler advocate here ;) and then after all as it was said your arrayList of pointers will dissapear after you will return from function, because it is scoped to this function only

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.