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

magal

Copy Constructor/Memory Allocation

5 posts in this topic

well, when making things as flexible as you are making your matrix class, you are BOUND to cause more overhead.

you pretty much HAVE to allocate a new space into which to copy over the cells of the matrix, otherwise when the original matrix object is destroyed, the new one will crash, since the memory for the matrix cells would be freed.

0

Share this post


Link to post
Share on other sites
also, what is the return type from operator + and operator *?

it SHOULD be CMatrix (or whatever your matrix class is called) rather than CMatrix& or CMatrix*.

then you return the temp matrix on the stack (which is more overhead, but cant be helped)

however, if for some reason this doesnt work, you may have to just overload the += and *= operators.

0

Share this post


Link to post
Share on other sites
Thanks for the replies so far.
Now I started to test the class and, when I only compile the class (source and header, with implementation and declaration respectively), it is fine, no errors are found.

But when I add a void main() at the end of the file and put the line Matrix m(2) to declare a square matrix of integers with 2 rows and 2 columns, it says that the allocation line '_pMatrix = new T[_pNRows][_pNCols];' is using a non-constant boundary for the array, and won't compile. Now, how can that be??? (_pMatrix is declared as a protected member 'T * _pMatrix;' and T is the template class.
Please guys, help me out again...

0

Share this post


Link to post
Share on other sites
As I recall a new statement of an array of arrays like you have requires that one of the sizes be constant... To get around this I believe you have to use single subscript arrays

blah = new T[_pNRows*_pnCols]

And then access it like this

blah[_pnCols*row + col]

0

Share this post


Link to post
Share on other sites
I'm having a problem with the Matrix class that I'm creating. The class is a matrix of any numeric type (so I used 'template' before the declaration of the class and it contains a protected member T * _pMatrix. In the constructor, I allocate the memory for this pointer using the 'new' operator.

But now I'm starting to overload the operators like +, * to implement all the ops with matrices. When I use a statement like A = B + C (all these variables are matrices), the values from B and C are summed and put on a new temp matrix which is returned from the overloaded operator function. And it happens that the default copy constructor copies the POINTER _pMatrix, making two different and independent pointers point to the same resource. Now, after the temp matrix with the summed values is returned, it runs out of scope and its destructor (which deallocates the resource) is called. And then the A matrix contains a _pMatrix member pointing to a deleted resource!!!

All I could think of was to implement a new copy constructor that would copy all non-pointer members of the source class to the destination class, allocate memory for the destination class' _pMatrix member and copy the entire contents from the source _pMatrix. Then, I have two different pointers pointing to two different resources, but both resources are identical.
If I'm using a 4x4 matrix of double, imagine the memory it was going to use!!!! It would take a lot of resources from the heap and cycles from the processor to do all this!!!!

Isn't there any other viable solution to copy the source object and its allocated resource to the destination???

0

Share this post


Link to post
Share on other sites
or, you could make a CMatrixArray class, which would take care of newing / deleting the whole slew of them, and could also do the array indexing stuff:

CMatrix *CMatrixArray::get(int x, int y)
{
return(&m_array[(y*m_dimx)+x])
}

or something. Not sure if it'd make sense in your situation, just thought I'd mention it as a wild thought.

Mason McCuskey
Spin Studios
http://www.spin-studios.com

0

Share this post


Link to post
Share on other sites