C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old February 7th, 2003, 04:42 PM
lfindle lfindle is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 3 lfindle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to lfindle
access violation w/ linked list pointers

I am writing a method that will append a node onto the end of a linked list.

I am getting an "unhandled exception: access violation" from the following line of code:

// temp pointer's next pointer needs to now point to new node

temp->next = p;

I attached a file with my method in it. I had to name it .java so it would upload correctly.

If anyone has any suggestions, I'd appreciate it.

-----------------------------------------
// insert user data at the end of the list
bool LinkedList::append(const UserData& d){

// if head pointer points to null, then it is an empty list
// node can be immediately added
if (head == NULL){
cout << "This is an empty list.";

// first step, create a new node to put information in
// use 2 arg constructor so that your new node will have the same information as the argument's data/link
// create the node with the d object as your data & point next to NULL indicating end of list
Node* n = new Node(d, NULL);

// point head pointer to new node
head = n;

// point tail pointer to new node since it is the end of the list
tail = n;

// increment node count by one
count++;


return true;
// else head pointer does not point to null (list is not empty)
} else if (head != NULL){
cout << "Trying to add a node to a list that is not empty" << endl;
// traverse to last node (NOT all the way to NULL pointer)
for (Node* temp = head; temp != NULL; temp = temp->next){
cout << "Is it even getting here?";

}

// create a new node
Node* p = new Node(d, NULL);

// temp pointer's next pointer needs to now point to new node
temp->next = p;

// do not delete the temp pointer because it is still pointing to your new node & it would delete that also
// increment node count by one
count++;

// point tail pointer to n
tail = p;
return true;
}
return false;
}
Attached Files
File Type: java appendmethod.java (0 Bytes, 250 views)

Last edited by lfindle : February 7th, 2003 at 04:47 PM.

Reply With Quote
  #2  
Old February 8th, 2003, 11:38 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 22 m 8 sec
Reputation Power: 797
I think your bug is here:
Code:
// traverse to last node (NOT all the way to NULL pointer)
for (Node* temp = head; temp != NULL; temp = temp->next){ 
    cout << "Is it even getting here?";
}


Irrespective of what your comment says, you are traversing your way to the NULL pointer, not to the last node! IMHO it should say:
Code:
// traverse to last node (NOT all the way to NULL pointer)
for (Node* temp = head; temp->next != NULL; temp = temp->next){ 
    cout << "Is it even getting here?";
}

See the difference?

Also, while you're at it, there's a scoping problem in your code, which may lead to non-portable code for other C++ compilers. I'm referring to your declaring a variable in a for loop and referring to it later outside the loop. In the above code you're declaring Node *temp in the for loop and then using it again a few lines below like this:
// temp pointer's next pointer needs to now point to new node
temp->next = p;
Older C++ compilers would allow this, because the old C++ standard allowed it. However, the latest C++ standards dictate that the scope of the variable declared within the for loop does not exist outside the for loop. This means that a newer C++ compiler will refuse to compile the line temp->next = p;, saying that temp is an undeclared variable. See http://www.mozilla.org/hacking/port...ariables_in_for for more. Hope this helps!

Reply With Quote
  #3  
Old February 10th, 2003, 06:14 AM
Teaser Teaser is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 6 Teaser User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
From your code you appear to have pointer to the last node, so use this instead of searching for the last node.

Something like http://pastecode.net/?action=viewpost&tag=8.

Reply With Quote
  #4  
Old February 28th, 2003, 09:46 AM
lfindle lfindle is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 3 lfindle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to lfindle
Thank you both for the responses. That solved the problem.

I appreciate it.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > access violation w/ linked list pointers


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway