#### Archived

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

# Need help with pointers

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

## Recommended Posts

Im writing a lightning tutorial for NeHe and im having trouble implementing a singly linked list and a bit of pointer manipulation. Here''s the code:

//------------------------------------------------------------------//

//- class LIGHTNING ------------------------------------------------//

//------------------------------------------------------------------//

//- Simulates lightning --------------------------------------------//

//------------------------------------------------------------------//

//This class is used to generate the lightning

class SECTION
{
public:
SECTION(){pNext = NULL;}
~SECTION(){}
VECTOR3D Position;
float Energy;
SECTION * pNext;
};

class LIGHTNING
{
public:
LIGHTNING(){
//Some default values

active = false;
treshold = 0.5;
alpha = 1.41421356237; //sqrt2 usually looks good

NumSelections = 8;
line = false;
}
~LIGHTNING(){}

//Variables

//Orientation

VECTOR3D StartPos, EndPos;

//Behaviour

float Energy, alpha, dist, treshold;
bool active;
bool line;
int NumSelections;

//Functions

//Returns true if there was a strike

bool Update();
void SetPos(VECTOR3D Start, VECTOR3D End);

};

//------------------------------------------------------------------//

//- LIGHTNING ENIGNE IMPLEMNENTATION -------------------------------//

//------------------------------------------------------------------//

//Used to delete SECTION linked lists

void deletelist(SECTION * pFirst)
{
//Some working variables

SECTION * pCurrent, * pNext;
if(pFirst)
{
pCurrent = pFirst;
while(pCurrent != NULL)
{
pNext = pCurrent->pNext;
delete pCurrent;
pCurrent = pNext;
}
}
}

void LIGHTNING::SetPos(VECTOR3D Start, VECTOR3D End)
{
StartPos.SetProperties(Start.v[0],Start.v[1],Start.v[2]);
EndPos.SetProperties(End.v[0],End.v[1],End.v[2]);
//Calculate distance squared

dist = (StartPos.v[0]-EndPos.v[0]) * (StartPos.v[0]-EndPos.v[0]) +
(StartPos.v[1]-EndPos.v[1]) * (StartPos.v[1]-EndPos.v[1]) +
(StartPos.v[2]-EndPos.v[2]) * (StartPos.v[0]-EndPos.v[2]);
}

bool LIGHTNING::Update()
{
if (active) // Draw the lightning

{
//Set up OGL

glPushMatrix();
//Some Variables

SECTION * pNewList = NULL, * pCurrent = NULL,
* pNew = NULL, * pPossible = NULL, * CurSections = NULL;

//Some working variables

int x;
float y,b;

//Selection variables

int a = 1;
float z = 1;

//Start at the top - the first section

CurSections = new SECTION;
CurSections->Position.SetProperties(StartPos.v[0],StartPos.v[1],StartPos.v[2]);
CurSections->Energy = Energy * dist;

//Keep going until there are no more sections to be processed

while(CurSections)
{
//Make a list for the new selections

deletelist(pNewList);
pNewList = new SECTION;
pNew = pNewList;

//Go through every point on the current list

pCurrent = CurSections;
while (pCurrent)
{
if(pPossible)
delete[] pPossible;
pPossible = new SECTION [a];
//For every position create the next points

//Find our next points

b = 0;
for(x = 0; x < a; x++)
{
//Find the next points

//Assign random values

//Find break down number

//E = kQ/r^2

y = (((pPossible[x].Position.v[0] - EndPos.v[0])*(pPossible[x].Position.v[0] - EndPos.v[0])) +
((pPossible[x].Position.v[1] - EndPos.v[1])*(pPossible[x].Position.v[1] - EndPos.v[1])) +
((pPossible[x].Position.v[2] - EndPos.v[2])*(pPossible[x].Position.v[2] - EndPos.v[2])));
y = 9000000000/((rand()/RAND_MAX)*y*y*y);
pPossible[x].Energy = pow(y,alpha)*(rand()/RAND_MAX);
b += pPossible[x].Energy;
}
for(x = 0; x < a; x++)
{
//Distribute energy

//It is a percentage of the original

pPossible[x].Energy = pPossible[x].Energy/b * pCurrent->Energy;
//If energy less than threshold discard

if(pPossible[x].Energy > treshold)
{
//Assign the new point

pPossible[x].Position.v[0] = pPossible[x].Position.v[0] * pPossible[x].Energy;
pPossible[x].Position.v[1] = pPossible[x].Position.v[1] * pPossible[x].Energy;
pPossible[x].Position.v[2] = pPossible[x].Position.v[2] * pPossible[x].Energy;
//Draw from the previous to the current

glBegin(GL_LINES);
pCurrent->Position.SendToOGL();
pPossible[x].Position.SendToOGL();
glEnd();
//Add to new sections new sections

pNew->Position.SetProperties(pPossible[x].Position.v[0],
pPossible[x].Position.v[1],
pPossible[x].Position.v[2]);
if(pCurrent->pNext != NULL)//If you arn''t at the end

{
pNew->pNext = new SECTION;
pNew = pNew->pNext;
}
}
}
//Go to the next point

pCurrent = pCurrent->pNext;
}
//Remove the old selections and make the CurSelectiosn the new selection

deletelist(CurSections);
CurSections = pNewList;

//Update the amount of selections the next pass

z = z + (NumSelections * NumSelections)/100 + (2*(rand()/RAND_MAX) -1);
a = Round(z);
}
}
//Check to see if the engine has enough energy to hit the target

//This compares the distance between the two points

if( (Energy * dist) < (dist) || (active == false)) //if there isn''t enough return false or there was no spark

{
return false;
}
else
{
return true;
}
//Its wierd, im writing this durring a thunder storm...hope it doesn''t fry

//the computer befroe i do

}

The problem occurs in the update function. I think it''s happening in the first while loop. When i run the program in debug mode i get this error: Debug Assertion Failed! ... File:dbgdel.cpp Line:47 Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse) ... and in release mode i get an illegal operation. Im basically trying to make lightning bolt out of a fractal tree, and the tree is continously built until the energy in the bolt runs out resulting in no more current sections. Does anyone know what is causing this and if so any possible ways to fix this problem?

##### Share on other sites
Why aren''t you using std::list?

##### Share on other sites
*gasp* maybe he''s trying to learn how link lists actually work before letting STL do all the work for him? Since when is it bad to learn?

llvallatrix, "in the first while loop" doesn''t tell us much, since that one encompasses most of the function. Be more specific please.

##### Share on other sites
quote:
Original post by foofightr
*gasp* maybe he''s trying to learn how link lists actually work before letting STL do all the work for him? Since when is it bad to learn?

It was a simple question which I''m sure llvllatrix is capable of answering for himself. Rather than grasping an opportunity for self-righteousness, maybe you should consider the possibility llvllatrix does not know of std::list. In that light, you might even be able to stretch your imagination to see how my question could be helpful.

##### Share on other sites
another question: if you have trouble with a simple linked list, why are you writing a tutorial? I''m not saying that you can''t do lighting, or that your tutorial couldn''t teach someone how to do it, just that most people would prefer to learn from someone with a broader base of skills.

##### Share on other sites
Question: Why would you want to use std::list?

Seems like a lazy mans way of doing things. Not to mention slow because std::list contains a hellava lot more crap than is required for something so trivial.

It''s called knowing when to use the right tools. You don''t use a hammer when what you need is a chisel.

##### Share on other sites
quote:
Original post by Anonymous Poster
Question: Why would you want to use std::list?

Seems like a lazy mans way of doing things. Not to mention slow because std::list contains a hellava lot more crap than is required for something so trivial.

It's called knowing when to use the right tools. You don't use a hammer when what you need is a chisel.

MUST.....CONTROL...TEMPER! I'm not at all surprised that this was posted anonymously.

I had a lot more written but I decided to trim this down to these four key points:

1) We're getting off topic and into the inevitable my-way-is-better-than-your-way-so-shut-the-hell-up flame war.

2) The overhead added by a GOOD stl implementation on a good compiler is trivial. You can almost always improve performance on ANY library, especially when you know more info about the data than the library does. But when it comes to a generic library, STL is hard to beat. This doesn't mean you HAVE to use it.

3) Since when is abstraction and code-reuse 'lazy?' If it is, then you're lazy for using C++ in the first place. LAZY LAZY LAZY HIGH LEVEL CODER! Go back to assembly you lazy anonymous programmer!

4) There would be a lot less memory-leaking programs if more people used time-tested libraries rather than rolling your own. I'm not saying don't LEARN how to write your own, but if you are writing production code that has to be of a certain quality, then what is wrong with going with a proven library like STL or Boost? Heaven forbid we ever use code that somebody else wrote!

And I have got the original code in question and I am currently looking for the problem in hopes to answer the original question. Without a C++ compiler currently at my disposal this may take some time, but I'll get back to this post later when I get a chance. I would hope that someone else can answer the original question before then, but if not I'll try and help.

--TheMuuj

[edited by - themuuj on June 28, 2002 1:04:24 PM]

##### Share on other sites
This might fix it(probably not). In deletelist and in the for your while loop you use (pCurrent != NULL) to check that the pointer is not NULL, but in the if statement in deletelist, you check for a null pointer by using simply if(pFirst), why not try if(pFirst != NULL). Sorry, but I didnt have much time to look, but that is what I saw at first.

##### Share on other sites
I think I''ve got it
not sure exactly how it causes the error, but it seems after the first loop pNewList,pCurrent, pNew, CurSections all point to the same piece of memory.

##### Share on other sites
Thanks for the help guys, its much appreciated.

BTW i have no idea what an std::list is, im still in high school and learned all my programming is pascal.

The reason i can code in c is because i read c++ in 21 days over last summer. Im wicked with linked list in pascal but i haven''t had much luck with them in c++. Dont know why.

As for std::list, thanks for the suggustion, it gives me more options. I was originally considering something like collections, ie a premade linked list, but i wasn''t sure if i would be able to do what i did in the original code. I was also fed up with researching as i spent the majority of yesterday going over university level electrostatics.

Give me 5 minuites, ill change those while arguments to != NULL...and...didn''t work, but ill leave it in just the same. Makes the code easier to understand

1 last thing...heres how i figured it was in the first while loop. When i first got these errors, i started commenting...I didn''t get the error when i commented out the while(CurSections) loop. But i got it when i commented out from the comment that reads
quote:

//Go through every point on the current list

in the while(CurSections) loop to the end of the while(pCurrent) loop. (this is all in the Update function btw).

Thanks for any help.

Anyway, time to get debugging...

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
18
5. 5

• 9
• 12
• 9
• 33
• 13
• ### Forum Statistics

• Total Topics
632589
• Total Posts
3007243

×

## Important Information

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!