That has beem pointed out earlier. (#2, #11). This works for g++ only.
You have to use
KurtCode:node *nodes = new node*[size];
That has beem pointed out earlier. (#2, #11). This works for g++ only.
You have to use
KurtCode:node *nodes = new node*[size];
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
It should beI can't think of a good reason to use a dynamic array here.Code:std::vector<node*> nodes(size);
If you are getting errors with the vector, post your attempt and the errors you get.
alrighty, i'll give it a shot
"Anyone can aspire to greatness if they try hard enough."
- Me
It compiled!!!
But then it had a runtime error, segfault =( it didn't give me a readout of the variables though like it normally does, it pointed me to the vector header file. =(
Here it is:
Code:#include <iostream> #include <fstream> #include <string> #include <vector> using namespace std; struct node { int data; node *next; node *prev; }; struct circle { node *start; }; void createCircle(circle *circ, vector<int> &v); int main() { //Main circle *first; first = new circle; vector<int> numbers; for(int i=0; i<10; i++) { numbers.push_back(i+1); } createCircle(first, numbers); cin.ignore(); //End return 0; } void createCircle(circle *circ, vector<int> &v) { //create intitial node node *start; start = new node; start->data = v.at(0); //create the rest of the nodes int size = v.size(); size--; //data vector<node*> nodes(size); for(size; size>=0; size--) { nodes.at(size) = new node; } size = v.size(); size--; //Insert Data for(int i=0; i<size-1; i++) { nodes.at(i)->data = v.at(i); } //Connect nodes circ->start = start; start->next = nodes.at(0); for(int i=1; i<size-1; i++) { nodes.at(i-1)->next = nodes.at(i); } //do prevs start->prev = nodes.at(size-1); for(size-2; size>=0; size--) { nodes.at(size)->prev = nodes.at(size-1); } //DONE }
"Anyone can aspire to greatness if they try hard enough."
- Me
Here nodes.at() throws an out of range exception. Not really much reason to use at() if you're not going to handle the exceptions.Code:vector<node*> nodes(size); for(size; size>=0; size--) { nodes.at(size) = new node; }
>> Not really much reason to use at() if you're not going to handle the exceptions.
Actually, there is in that it will throw an exception every time instead of having undefined behavior which might silently break something. Even if you don't handle the exception it still indicates why the "crash" occured. It's actually probably better to always use at() unless you have some specific efficiency issue. Most people use operator[] because they are just used to array syntax (or are prematurely optimizing).
In this case, nodes.at(size) is out of bounds. Valid indexes are 0 to size-1. You'll want to start looping at size-1. I'd also use a separate loop variable instead of re-using size for this.
Personally I find at() a little obscure-looking (in addition to being longer), and prefer to use it only during debugging, if only for that reason.
well i know at() uses bounds checking which is why i prefer it as do most others, yeah it's obscure but if you were used to using .at() [] might look obscure too haha
"Anyone can aspire to greatness if they try hard enough."
- Me
okay, so i used a new variable and all and it stills throws the exception =(
Code:size = v.size(); size--; for(int i=0; i < size; i++) { nodes.at(i) = new node; } ...
"Anyone can aspire to greatness if they try hard enough."
- Me
Are you sure it is throwing the exception there? You use at() several times, it is possible that it is being called from a different line of code.
Yeah its throwing an unhandled exception and if i click continue then an assertion error, the part it is showing me is in the vector.h file, _XRan or something, out of range memory location
"Anyone can aspire to greatness if they try hard enough."
- Me
Right, but where in your code is the exception being thrown? Look down the call stack until you see some of your own code, and identify the call to at() that is causing the problem. You can also add cout statements between each for loop to see how many cause the error.
You can also post more complete current code to make sure you implemented the changes properly.
alrighty, i'll give it a shot, thanks for the help!
"Anyone can aspire to greatness if they try hard enough."
- Me