• Advertisement

Archived

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

std::list question

This topic is 5488 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
If you want a C++ "array" that you can resize, use std::vector. Documentation links can be found in my signature.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

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
Guest Anonymous Poster
If you are going to use newSprite beyond
the scope of the function you are in , you
need to allocate heap space.

//example
cSprite newSprite = new cSprite;
if(NULL == newSprite);...//Handle error here
newSprite->Define(<snip>;
newSprite->SetBitmap(<snip>;
...
ShipBullets.AddNode(newSprite);
//end example

Another tip is to check the return value of all
of your functions. Certainly all memory
allocates need to be checked.

Finally, make sure all of your variables are initialized
properly in the cSpriteList constructor.

BTW: Why did you change void cSpriteList::AddNode(cSprite* Sprite) to void cSpriteList::AddNode(cSprite Sprite)? The new
routine will have Head pointing to garbage as soon the function
returns since cSprite Sprite is created in local stack space.

Share this post


Link to post
Share on other sites
i would not need to allocate memory for the sprite because i allocate memory for the node in AddNode() (and the sprite is within the node). i modified the class to work with integers, and it works fine, but for some reason it doesn''t work with my csprite class, any suggestions?

Share this post


Link to post
Share on other sites
When you call AddNode(cSprite Sprite), the Sprite is copy constructed upon entering the function and assignment copied when assigning to newNode->mySprite. You DID define both the copy constructor and assignment operator for class cSprite right? If you do so, your program shouldn''t crash anymore.

Regardless though, passing the cSprite in by value is a bad idea. The time it takes for the copy construction and assigment copy is unnecessary and its just bad form for the list to have its own, personal copy of the sprite. What will you do when you want to get it out of the list? Copy it again? Have the nodes store pointers to the cSprite like you originally did. Then the copy and assignment operators will never be called and you will have only 1 instance of the sprite loaded in memory, like you should.

Storing an integer in node is "basically equivalent" to storing a pointer to the cSprite.

Share this post


Link to post
Share on other sites

  • Advertisement