• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
McGrane

Linked List Problems

8 posts in this topic

Hey, i am having problems with converting a list into an array in c++,
Any ideas whats gone wrong? Its causing segment faults.

[code]

#ifndef _CARRAY_H
#define _CARRAY_H
#include <fstream>

using namespace std;

template <class ALLTYPE> class cDataType {
public:
ALLTYPE Data;
cDataType *p_Next;
cDataType(): p_Next( NULL ) {
}
};
template <class ALLTYPE> class cArray {
int m_Size;
cDataType<ALLTYPE> *p_Head;
cDataType<ALLTYPE> *p_Tail;
public:
cArray() : m_Size( 0 ), p_Head( NULL ), p_Tail( NULL ) {
}
void Drop( ) {

cDataType<ALLTYPE> *ptr = p_Head;
while( ptr->p_Next != p_Tail ) {
ptr = ptr->p_Next;
}
cDataType<ALLTYPE> *deleteNode = ptr->p_Next;
p_Tail = ptr;
m_Size--;
delete deleteNode;
}
void Add( ALLTYPE l_firstPoint ) {
if( p_Head == NULL ) {
cDataType<ALLTYPE> *newNode = new cDataType<ALLTYPE>;
newNode->Data = l_firstPoint;
newNode->p_Next = NULL;
p_Head = newNode;
p_Tail = newNode;
m_Size++;
}
else {
cDataType<ALLTYPE> *newNode = new cDataType<ALLTYPE>;
newNode->Data = l_firstPoint;
newNode->p_Next = NULL;
p_Tail->p_Next = newNode;
p_Tail = newNode;
m_Size++;
}
}
ALLTYPE *getArray() {
int index = 0;
ALLTYPE *array = new ALLTYPE[m_Size];
cDataType<ALLTYPE> *ptr = NULL;
ptr = p_Head;
while( ptr->p_Next != NULL ) { // !#### Causes error here, or if set to ptr == NULL
array[index] = ptr->Data; // !#### Causes error here
ptr = ptr->p_Next;
index++;
}
return array;
}
int getSize() { return m_Size; }
};
#endif
[/code]
It is being called here
[code]

glDrawElements( GL_TRIANGLE_STRIP, (indexArray->getSize()) -256, GL_UNSIGNED_INT, indexArray->getArray() );
[/code]
and initialized here
[code]

indexArray = new cArray<unsigned int>();


indexArray->Add( 0 );

int ySize = TERRAIN_DEPTH - 1; // Height of array
int xSize = TERRAIN_DEPTH * 2; // Width of array
unsigned int index = TERRAIN_DEPTH; // First, Third, Fifth ... etc row
unsigned index2 = (TERRAIN_DEPTH*3)-1; // Secound, Fourth Sixth ... etc row
// Fill the array
for( int y = 0; y < ySize; y+=2 ) {
for( int x = 0; x < xSize; x+=2 ) {
indexArray->Add( index++ );
indexArray->Add( index-TERRAIN_DEPTH );
}
indexArray->Drop();
for( int x = 0; x < xSize; x+=2 ) {
indexArray->Add( index2-- );
indexArray->Add( index2-TERRAIN_DEPTH );
}
indexArray->Drop();

index = index + TERRAIN_DEPTH;
index2 = index2 + (TERRAIN_DEPTH*3);
}
[/code]

Thanks for any help ! Edited by mcgrane66
0

Share this post


Link to post
Share on other sites
Thanks for the reply JTippetts! I feel abit thick about the memory leak, this is something i had fixed previously, then seem to have back tracked at some stage that i don't remember [img]http://public.gamedev.net//public/style_emoticons/default/dry.png[/img], i had pointers and all set up for it. [quote name='JTippetts' timestamp='1336699378' post='4939179']Second... ew.[/quote] I thought it was funny that my code disgusted you ha, What i am using this for is for loading heightmaps, i have all the heights stored in an array, and was just making a function that would calculate the indices of all the triangles in a triangle strip. I was only planning to do this once at Initialize time - are using linked lists for this a bad idea for this?? And no this isint a case of [quote name='JTippetts' timestamp='1336699378' post='4939179']
[left][background=rgb(250, 251, 252)]roll-your-own syndrome[/background][/left]
[/quote], its more a case of 'know no better syndrome'. I have never used vectors before, and may look into them by the sounds of it [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
For a learning curve anyway, i thought that maybe the excess memory was causing the Access Violation, but it is still happening in the while loop.. Why would this be, am i doing this incorrectly? Edited by mcgrane66
0

Share this post


Link to post
Share on other sites
Thanks again, i know there are problems with the drop methods, as at the minute it was only used after the for loops had filled in the indexArray, to just delete one excess entry
[code]
for( ... ) {
Add( ... )
Add( ... )
}
Drop(); // Last excess item
[/code]
But thats just laziness, on my behalf. As for the while loop, i originally was using while( ptr != NULL ), but then i was getting a segmant fault on the last line, ptr = ptr->next; Presumably if i just used an if statment so as to not asign it to null, this may stop it, but would that not cause an infinite loop, considering its waiting for ptr to be NULL ?

Thanks for the help anyway, i will just look into the methods you said and use them, it was just to learn from my mistakes that i wanted to know.
0

Share this post


Link to post
Share on other sites
The mistakes here seem to be sloppy programming and laziness. Your ptr doesn't have to be null in order to raise a segfault; if it's an invalid pointer such as would be the case, as JTippetts pointed out, in a number of circumstances then it'll still segfault, even though the pointer is not null.

Take the time to do things right, check your conditions, stop being lazy. It's lazy programming that leads to mistakes. How much time did you save yourself by being lazy, considering the debugging time you've wasted, the forum posting time you've wasted, etc?
0

Share this post


Link to post
Share on other sites
I dont feel that i wasted anytime, the debug literally took 10 seconds, and by posting to the boards i learned something that would have took me alot longer to figure out myself if ever. All i consider here for lazy programming is not checking for empty list when dropping a node, but as i just said i only left them out because at the minute this will never be the case, it is only used to remove one unused element added by a for loop. I was making this more as a proof of concept for myself, and something to learn from, your comment however does not add to this topic, clear up anything, nor in anyway is help-full, it to me just seems you've wasted your time writting a message to say stop being lazy. This is an obvious suggestion.
Thanks anyway
0

Share this post


Link to post
Share on other sites
I didn't mean that as an attack, just more along the lines of advice from someone who has made plenty of mistakes by being lazy. I still make mistakes. But here's the thing: if you come on here posting code that you know is sloppy (you admitted it) then you a) waste someone elses time, who points out mistakes you already know are mistakes and b) hide the bug you are trying to find behind a whole slew of other bugs. The problem you ask about needs to be pared down to the actual problem, not the problem disguised by a raft of other problems. You asked about why you would be getting a segfault in your while loop, when it is immediately obvious that there are so many places in your code where invalid pointers could slip through the cracks that narrowing in on the specific time and place that it happens is kind of difficult, made more difficult by the fact that you admittedly posted lazy code, rather than posting code that you made the effort to write as correctly as possible.
-2

Share this post


Link to post
Share on other sites
[quote name='FLeBlanc' timestamp='1336752017' post='4939329']
I didn't mean that as an attack, just more along the lines of advice from someone who has made plenty of mistakes by being lazy. I still make mistakes. But here's the thing: if you come on here posting code that you know is sloppy (you admitted it) then you a) waste someone elses time, who points out mistakes you already know are mistakes and b) hide the bug you are trying to find behind a whole slew of other bugs. The problem you ask about needs to be pared down to the actual problem, not the problem disguised by a raft of other problems. You asked about why you would be getting a segfault in your while loop, when it is immediately obvious that there are so many places in your code where invalid pointers could slip through the cracks that narrowing in on the specific time and place that it happens is kind of difficult, made more difficult by the fact that you admittedly posted lazy code, rather than posting code that you made the effort to write as correctly as possible.
[/quote]


Chill out.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0