Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Neen10do

std::list question

This topic is 5732 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey guys: im trying to create a 2d shooter. and obviously i need a dynamic memory structure to hold bullets, enemies etc. and in visual basic i used an array (because you can resize) but in c++ im trying to use my own custom LinkList class, but that isnt working too well. my question is this, can the std::list structure suit my purposes? and if so, where can i find tutorials on how to use it? thanks. i code therefore i am.

Share this post


Link to post
Share on other sites
Advertisement
google. std lists.
You might find it to be a good programming practice to write your own linked list class. No only will you know how to use it =P but you''ll learn much about memory management.

BUT

as far as this and its equivalence to vb. I would say that this is a good approch. you could also look into the vector class that comes bundled with the stdlib.
andy

Share this post


Link to post
Share on other sites
ive created my own linked list classes in AP c++, but the data item to the nodes werent pointers to classes, like this (cSprite).

apparently, something is wrong when i try to add a node to the list, because the program crashes instantly

void cSpriteList::AddNode(cSprite* Sprite)
{
//Does if the list is empty
if(mySize == 0)
{
///////////CRASHES WHEN RUNNING THESE LINES/////////////////////////
Head->mySprite = Sprite;
Head->link = NULL;
//////////////////////////////////////
}
//If it isn''t empty
else
{
NodePtr newNode = new node;

//Creates the new node
newNode->mySprite = Sprite;
newNode->link = NULL;

//Adds to the end of the list
node* cursor;
cursor = Head;
while(cursor->link != NULL)
{
cursor = cursor->link;
}

delete newNode;
}
mySize++;
}

i code therefore i am.

Share this post


Link to post
Share on other sites
well i really should write my own linklist class, but it keeps crashing, can you guys give me any pointers?


this is my .h file for my "linklist"

  
//CSPRITELIST.H

#include "cSprite.h"

#pragma once

//NODE STRUCT

struct node
{
cSprite* mySprite;
node* link;
};

typedef node* NodePtr;

class cSpriteList
{
public:
NodePtr Head;
NodePtr Tail;
int mySize;

cSpriteList();
~cSpriteList();

void AddNode(cSprite* Sprite);
void DisplayAll(LPDIRECTDRAWSURFACE7 buffer);
void MoveAll();

private:
void RemoveNode(NodePtr previous);
};


and ive traced my problem to my addnode function

  
void cSpriteList::AddNode(cSprite* Sprite)
{
//Does if the list is empty

if(mySize == 0)
{
//Head->mySprite = Sprite;

//Head->link = NULL;

}
//If it isn''t empty

else
{
NodePtr newNode = new node;

//Creates the new node

newNode->mySprite = Sprite;
newNode->link = NULL;

//Adds to the end of the list

node* cursor;
cursor = Head; //would this change head perminently?


//traverses the list

while(cursor->link != NULL)
{
cursor = cursor->link;
}
cursor->link = newNode;
delete newNode;
}
mySize++;
}


the two commented lines when the list is empty is where it crashes, it could crash other places in the function as well. if you guys could give me a little help and direction that would be great.

i code therefore i am.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Head initially points to garbage. Thus when you first
try to access its members..CRASH.

Second, you are deleting
the node you just created when you add to a non-empty
list. Why?


PS> No, head does not change unless you
assign a value to it.

Share this post


Link to post
Share on other sites

My gut instinct is that this is an uninitialized pointer.

if(mySize == 0)
{
//Head->mySprite = Sprite;
//Head->link = NULL;
}

What is head pointing at when this code runs?
Do you ever initialize Head anywhere? You''re not allocating
any new nodes here for your head to point at, unless you do it someplace else like the constructor and didn''t show us that.

Share this post


Link to post
Share on other sites

cursor = Head; //would this change headperminently?
//traverses the list
while(cursor->link != NULL)
{
cursor = cursor->link;
}
cursor->link = newNode;
delete newNode;

AP has a good point. You don''t want to delete your new node there, this is going to make the links in your list invalid, and cause more crashes.

Share this post


Link to post
Share on other sites
i modified my code a little bit... and it should work, but it seems that the code is crashing when i called the AddNode function... and it cant be that anything is invalid in the AddNode, because i can completely comment the whole function out and it still crashes. here are some snippets.

the input function where upon space is pressed, and a new bullet is added. this is where it crashes.

  
//SPACE

if (KEYDOWN(state, DIK_SPACE))
{
//Declaration

cSprite newSprite;

//Defines all custom parameters of csprite, no problems here

newSprite.Define(0,ship.myX + ship.myWidth,ship.myY +( ship.myHeight / 2),39,18,1,3,5,1,20);

//loads the bitmap to the sprite, no problem here

newSprite.SetBitmap(dd.lpDD,"regbullets.bmp",Main_Window);

//adds the new sprite to the cSpriteList, crashes when called

ShipBullets.AddNode(newSprite);
}


this next snippet is the addnode function, although it crashes when the code is completely commented out anyway.

  
void cSpriteList::AddNode(cSprite Sprite)
{
//Creates Node

NodePtr newNode = new node;
newNode->mySprite = Sprite;
newNode->link = NULL;

//Does if the list is empty

if(mySize == 0)
{
Head = newNode;
}
//If it isn''t empty

else
{

//Adds to the end of the list

node* cursor;
cursor = Head;
while(cursor->link != NULL)
{
cursor = cursor->link;
}
cursor->link = newNode;

}
mySize++;
}



there seems to be something that is happening to the function call that is making it crash.... i dont know what it is, any ideas?

i code therefore i am.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!