Go Back   Tactical Gamer > General Forums > Hardware & Software Discussion


Hardware & Software Discussion Hardware and Software discussion and troubleshooting. Tweakers and Overclockers welcome!

Reply
 
Thread Tools
Old 08-25-2007, 12:49 PM   #1 (permalink)

 
Iceberg's Avatar
 
Join Date: Dec 2004
Location: Illinois
Age: 25
Posts: 1,929
C++ Gurus

I took a few intensive courses on C++ and Data structures my freshmen year of college. 5 years later I'm a bit rusty, but have a few requests to critique my logic:

Simple function to remove a node in a list of type list, I think I've got the right idea but I'm so damn rusty on pointers it's killing me:
Code:
//This isn't right, but just shows what I'm working with
    struct list() {
        list *next;
        list *prev;
        list *data;
}

//This is the function I wrote
    void remove(list node) {
list *nPrev = node->prev;
list *nNext = node->next;

nPrev->next = nNext;
nNext->prev = nPrev;

delete node;

return 1;
}
__________________
It only takes 2 bites [insert parasite] to get to the center of a meatpop.
TGNS Admin | NS Forum Moderator Digz | NS Play Tester
Natural Selection | TGNS Primer | Communication Awareness!
Army Info:
Deployment or Bust - June 2009
Iceberg is offline   Reply With Quote
Old 08-25-2007, 02:39 PM   #2 (permalink)
 
=Sarc='s Avatar
 
Join Date: May 2003
Location: Ottawa, Canada
Posts: 4,600
Re: C++ Gurus

You probably want to do some kind of comparison with node and the current node you're looking at. If they are the same, delete it.

This part looks funny...
Code:
nPrev->next = nNext;
You probably want that reversed or something...

Code:
nNext = node->next;
nPrev = node->prev;
So you have your references to nodes on either side of the node you want to delete. Make sure they reference each other and deallocate the memory for the node you want to delete.
__________________
JO Guides & Tutorials
Team Element - It's who you game with.
=Sarc= is offline   Reply With Quote
Sponsored links
Old 08-26-2007, 03:04 AM   #3 (permalink)

 
Iceberg's Avatar
 
Join Date: Dec 2004
Location: Illinois
Age: 25
Posts: 1,929
Re: C++ Gurus

Hm, I thought that was what I was doing? I thought I was taking the node, selecting its next pointer as a node, and then changing the "next" nodes previous to the original nodes previous. And doing the same with the nodes previous pointer.

Or am I just using them wrong?
__________________
It only takes 2 bites [insert parasite] to get to the center of a meatpop.
TGNS Admin | NS Forum Moderator Digz | NS Play Tester
Natural Selection | TGNS Primer | Communication Awareness!
Army Info:
Deployment or Bust - June 2009
Iceberg is offline   Reply With Quote
Old 08-26-2007, 03:26 AM   #4 (permalink)
 
=Sarc='s Avatar
 
Join Date: May 2003
Location: Ottawa, Canada
Posts: 4,600
Re: C++ Gurus

Ah, I see... I got all mixed up when I first looked at it.

What happens when you run it?

The only other thing that might cause a problem is the function signature. If the function's argument is pass by value, it might not delete when you want it to. You'll have to pass the node argument by reference since the parameter calls for the list struct and not a pointer to a list struct.

Change the function's parameter to
Code:
void remove (list *node)
or call the function with
Code:
remove(&node);
__________________
JO Guides & Tutorials
Team Element - It's who you game with.
=Sarc= is offline   Reply With Quote
Old 08-26-2007, 03:32 AM   #5 (permalink)

 
Iceberg's Avatar
 
Join Date: Dec 2004
Location: Illinois
Age: 25
Posts: 1,929
Re: C++ Gurus

Understood. Right now I haven't been able to run any code, my PC is currently dead.
__________________
It only takes 2 bites [insert parasite] to get to the center of a meatpop.
TGNS Admin | NS Forum Moderator Digz | NS Play Tester
Natural Selection | TGNS Primer | Communication Awareness!
Army Info:
Deployment or Bust - June 2009
Iceberg is offline   Reply With Quote
Old 08-27-2007, 12:16 AM   #6 (permalink)

 
Iceberg's Avatar
 
Join Date: Dec 2004
Location: Illinois
Age: 25
Posts: 1,929
Re: C++ Gurus

Ok, what about this:

Code:
void remove() {
	list *nNext = this->next;
	list *nPrev = this->prev;

	nNext ->prev = nPrev ;
	nPrev ->next = nNext ;

	delete this->data;
	delete this;
}
__________________
It only takes 2 bites [insert parasite] to get to the center of a meatpop.
TGNS Admin | NS Forum Moderator Digz | NS Play Tester
Natural Selection | TGNS Primer | Communication Awareness!
Army Info:
Deployment or Bust - June 2009
Iceberg is offline   Reply With Quote
Sponsored links
Old 08-27-2007, 01:34 AM   #7 (permalink)
 
=Sarc='s Avatar
 
Join Date: May 2003
Location: Ottawa, Canada
Posts: 4,600
Re: C++ Gurus

It looks ok. It also seems like you changed it so that remove() is an operation on a node object. Everything looks ok logically.
__________________
JO Guides & Tutorials
Team Element - It's who you game with.
=Sarc= is offline   Reply With Quote
Old 08-27-2007, 01:49 AM   #8 (permalink)

 
Iceberg's Avatar
 
Join Date: Dec 2004
Location: Illinois
Age: 25
Posts: 1,929
Re: C++ Gurus

Yeah that's what I did (seems more logical, plus I won't have to check if the node is of type list). Great, thanks for the guidance.
__________________
It only takes 2 bites [insert parasite] to get to the center of a meatpop.
TGNS Admin | NS Forum Moderator Digz | NS Play Tester
Natural Selection | TGNS Primer | Communication Awareness!
Army Info:
Deployment or Bust - June 2009
Iceberg is offline   Reply With Quote
Old 08-27-2007, 03:12 AM   #9 (permalink)
 
PanzerHans's Avatar
 
Join Date: Aug 2005
Location: By the PC
Age: 35
Posts: 2,259
Re: C++ Gurus

Perhaps off topic, but if you are going to do programming these days you might want to look at either Java or .NET. This way you can safely ignore the pesky *'s.

Pointer arithmetics will be a thing of your past.
__________________
--
VI VI VI - the number of the beast
PanzerHans is offline   Reply With Quote
Old 08-27-2007, 11:23 AM   #10 (permalink)
 
=Sarc='s Avatar
 
Join Date: May 2003
Location: Ottawa, Canada
Posts: 4,600
Re: C++ Gurus

Quote:
Originally Posted by PanzerHans View Post
Perhaps off topic, but if you are going to do programming these days you might want to look at either Java or .NET. This way you can safely ignore the pesky *'s.

Pointer arithmetics will be a thing of your past.
This is a good point. Managed code is much easier to program but... it's managed code. You lose that control you hated so much, which means your program might not run as fast or use the smallest memory footprint.

Managed code would be good for this unless it's a lesson in using pointers. I think it's still good to understand what pointers are and how to use them. Java uses pointers but not in the way you'd think because you never have to think about memory references. This can lead to problems when you accidentally pass a reference to a method instead of a copy of the object or vice versa. You also still need to understand the concept of pointers for linked lists!
__________________
JO Guides & Tutorials
Team Element - It's who you game with.
=Sarc= is offline   Reply With Quote
Sponsored links
Old 08-27-2007, 11:47 AM   #11 (permalink)

 
Iceberg's Avatar
 
Join Date: Dec 2004
Location: Illinois
Age: 25
Posts: 1,929
Re: C++ Gurus

This was entirely for a refresher with pointers and linked lists. I've done this all before, but I just needed to wrap my head around it again.

Plus I enjoy the control I get with pointers Now if I just remembered how to write in C instead of C++ I'd be in line to create some quick peices of code.

Now Assembly language is where the real speed is at , oh my little microcontrollers how I miss thee.
__________________
It only takes 2 bites [insert parasite] to get to the center of a meatpop.
TGNS Admin | NS Forum Moderator Digz | NS Play Tester
Natural Selection | TGNS Primer | Communication Awareness!
Army Info:
Deployment or Bust - June 2009
Iceberg is offline   Reply With Quote
Old 08-27-2007, 11:52 AM   #12 (permalink)



 
WhiskeySix's Avatar
 
Join Date: Oct 2005
Location: Gillette Stadium, Section 309, Row 12, Seat 24
Age: 33
Posts: 8,468
Re: C++ Gurus

Whenever I'm using linked lists, I use a 'list *pCurrent' pointer and use that as my main reference point. (the notion of a 'current' pointer gets used a lot in real-time code as well)
WhiskeySix is offline   Reply With Quote
Sponsored links
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


All times are GMT -4. The time now is 09:38 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
©2004-2008 - Tactical Gamer - All Rights Reserved