• 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.

Archived

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

Elwren

Delete Question...

7 posts in this topic

Hey, I''m reading this C++ book and one of the chapters is discussing templates. So as I''m reading it and coding the examples, I''m getting an "Debug Assertion Failed!" error with the expression "_BLOCK_TYPE_IS_VALID(pHead->nBlockUse". So I copied and paste the code from the book, and get the same exact error. I''m not good with the Debugging mode in VC++, but I believe the problem is the very last part the program, where I am deleting theZoo. The author uses a for loop and deletes it in a weird way, one that I''ve never seen before, so I''m not really sure what the problem is. I tried to take out the weird for/delete loop, and just put my normal way of deleting an array, delete [] theZoo;, but I get some conversion error. Anyways, could someone take a look at this code and explain what the problem is and why the author deleting theZoo in the way that he did. Thanks
  
   #include <iostream.h>
   const int DefaultSize = 10;
   // declare a simple Animal class so that we can

   // create an array of animals

   class Animal
   {
   public:
      Animal(int);
      Animal();
      ~Animal() {}
      int GetWeight() const { return itsWeight; }
       void Display() const { cout << itsWeight; }
   private:
      int itsWeight;
   };
   Animal::Animal(int weight):
   itsWeight(weight)
   {}
   Animal::Animal():
   itsWeight(0)
   {}
   template <class T>  // declare the template and the parameter

   class Array            // the class being parameterized

    {
   public:
       // constructors

       Array(int itsSize = DefaultSize);
       Array(const Array &rhs);
      ~Array() { delete [] pType; }

      // operators

       Array& operator=(const Array&);
      T& operator[](int offSet) { return pType[offSet]; }
      const T& operator[](int offSet) const 
          { return pType[offSet]; }
       // accessors

      int GetSize() const { return itsSize; }
    private:
      T *pType;
       int  itsSize;
   };
   // implementations follow...

   // implement the Constructor

   template <class T>
   Array<T>::Array(int size = DefaultSize):
   itsSize(size)
   {
      pType = new T[size];
      for (int i = 0; i<size; i++)
         pType[i] = 0;
   }
   // copy constructor

   template <class T>
   Array<T>::Array(const Array &rhs)
   {
      itsSize = rhs.GetSize();
      pType = new T[itsSize];
      for (int i = 0; i<itsSize; i++)
         pType[i] = rhs[i];
   }
   // operator=

   template <class T>
   Array<T>& Array<T>::operator=(const Array &rhs)
   {
      if (this == &rhs)
         return *this;
      delete [] pType;
      itsSize = rhs.GetSize();
      pType = new T[itsSize];
      for (int i = 0; i<itsSize; i++)
         pType[i] = rhs[i];
      return *this;
   }
   // driver program

   int main()
   {
      Array<int> theArray;      // an array of integers

      Array<Animal> theZoo;     // an array of Animals

      Animal *pAnimal;
      // fill the arrays

      for (int i = 0; i < theArray.GetSize(); i++)
      {
         theArray[i] = i*2;
         pAnimal = new Animal(i*3);
         theZoo[i] = *pAnimal;
           delete pAnimal;
     }
     // print the contents of the arrays

     for (int j = 0; j < theArray.GetSize(); j++)
     {
        cout << "theArray[" << j << "]:\t";
        cout << theArray[j] << "\t\t";
        cout << "theZoo[" << j << "]:\t";
        theZoo[j].Display();
        cout << endl;
     }
      for (int k = 0; k < theArray.GetSize(); k++)
        delete &theZoo[j];
    return 0;
}
  
0

Share this post


Link to post
Share on other sites
After a quick look:

theZoo is an array of Animals, not pointers to animal.



What the hells!
0

Share this post


Link to post
Share on other sites
It looks as though theZoo is an array and to delete it the author is referencing theZoo to get its address and through the for loop is incrementing it to delete all values of the array. My best guess for the reason a delete [] theZoo would not work is because theZoo points to the first part of the array and with that syntax u are telling it to delete an array when it is actually a single pointer, i think, thats why he uses the for loop. Don''t quote me on that I am still kind of new at C++ but thats what I got from it.

Chris
0

Share this post


Link to post
Share on other sites
In the book, the author says something about "returning the allocated memory before the arrays are destroyed". What does this mean?

Also, why does he delete the address of the last instance of the array of objects (he put the already incremented variable ''j'' in theZoo[j] instead of ''k'')
0

Share this post


Link to post
Share on other sites
Well returning the allocated memory before the arrays are destroyed means to go through each element of your array and free the memory that was allocated for each element before you free the array itself. Otherwise the memory for each element will be hogged forever(or atleast until reboot) if it manages to slip by Windows on exit

Did you try replacing that j with a k?

I think it should be a k. Trying to delete something that was already deleted will generally cause problems

Seeya
Krippy
0

Share this post


Link to post
Share on other sites
Ya I tried changing it to a ''k'' and it still got the same error. Is it safe to run this program and encountring the error, because I don''t want to hog up a portion of my memory "forever". =( If anyone could tell me how to delete the memory in a different way than the author is doing it, than that would be fine too. Thanks
0

Share this post


Link to post
Share on other sites
After looking at that code closer, I have come to the conclusion that you should find a different book to read. lol. This author is nuts.

That last loop encompasses all kinds of issues.

First, even though we are pretty sure theArray and theZoo contain the same number of elements, using the size of theArray rather than the size of theZoo to iterate through the elements of theZoo is madness and breaks numerous rules of programming etiquette.

Second, the j in the array bounds makes absolutely no sense at all.

Third, the reason he puts the & before theZoo[j] is because theZoo[j] returns a reference, not a pointer. Therefore, he is trying to delete a reference which is not going to happen. If you don''t put the & first you will get an error about converting '''' to something, so putting the & in front allows the program to compile but not function properly. He probably never tested that it worked, just hacked it together so that it would compile. That would also explain his not noticing the misplaced j.
That line is pretty much eqivalent to this:
Animal an = theZoo[j];
delete &an;

which is a big no-no.


Fourth, the loop is completely unnecessary. In the destructor for Array he has this:

delete [] pType;

Which does exactly what he is attempting to do in that loop. It iterates through each instance of Animal in the array and deletes them. That is the purpose of the delete [] syntax instead of simply delete.

Therefore, if the program somehow was able to delete those instances in his loop, it would certainly fail in the destructor where it will attempt to delete things which were already deleted unless you were to set them as NULL after you delete them (which he doesn''t attempt to do).

So you can safely remove that loop from your program and be done with it

Seeya
Krippy
0

Share this post


Link to post
Share on other sites
Woo hoo, I took out the loop at the end like you said and it worked fine. Thanks so much, I''ve been stuck on the same chapter for like 4 days.

Oh and the book I''m reading is "Teach Yourself C++ in 21 Days" and I''m currently on day 19; up to this point, the code has been pretty good so all and all, its a pretty decent book for beginners.

Thanks again for the help, peace
0

Share this post


Link to post
Share on other sites