• 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
Martin Radev

C++ avoiding pointers

16 posts in this topic

Hello,

 

I am working on a game using SDL. I was pointers to some 2d primitives, however probably to bad management there were some memory leaks. I switched to use references and not allocate dynamic memory. I believe the problems were due to not following the rule of 3 (not declaring explicitly the assignment operator).

 

So, my questions is: should I try to avoid dynamic memory allocation. For example, I know that some object is needed in the scope of a function or that it should last for a very very brief period where a few functions have to work on it. Should I in this case pass by reference or by pointer to the object?

 

What do you think?

 

To me, it seems that dynamic memory allocation is helpful only when one has to create an array where the size of the array is not known.

 

I would be grateful if you give some hints and point out when they would be definitely necessary to be used.

0

Share this post


Link to post
Share on other sites

Don't avoid pointers out of fear; if you're purposely avoiding them then maybe C++ isn't for you, you might be better off using another language. You can make some pretty intense gfx games with C# (if you're writing for the Windows eco-system.) As already stated there are a number of rules-of-thumb, some tried and true patterns, and some really great new support in C++0x that make pointer management pretty easy.

 

Another thing you can do; when I was starting out with C I sat own with my compiler (and GCC or CLang is really nice for this, you can just install cywin and the gcc toolchain if you're not willing to stray from windows) and worked through some example pointer patterns until I got a fairly deep understanding of how they work. It didn't take long either.

 

If you want to be a proficient programmer with graphics you should invest the time it takes to be comfortable with pointers. Either that or just write tic-tac-toe games with other languages. Smart phone developers seem to do pretty well.

 

1

Share this post


Link to post
Share on other sites

Smart pointers, smart pointers, smart pointers.

 

The whole idea of a smart pointer is that they effectively allow you to hoist dynamic, non-deterministic memory usage into the realm of static, deterministic memory usage where you can effectively treat them as value-types.

 

Today, you basically don't have to think about the Rule of 5 or rule of 3 -- provided that use an appropriate smart pointer whenever a heap object is owned, and you only use raw pointers when no ownership is implied. Which kind of pointer you use is easy once you understand what each kind of pointer does -- your entire decision-making process can be expressed through a flow-chart that fits on a postcard. The only part that's a bit tricky is learning to recognize where weak_ptr fits in, but the entire landscape is a whole lot smaller than what you need to know to implement correct memory management patterns using new/delete directly.

2

Share this post


Link to post
Share on other sites

You should be avoiding pointers if at all possible.  You'll know when the time comes to use them, because in those circumstances it won't be possible to do what needs to be done.  In practice, most objects are temporaries, and unless the temporary in question is ungodly large, you'll be allocating it on the stack, which means no pointers.

 

Dynamic memory allocation is useful (and required) in the following instances:

  1. Your object is too large for the stack.
  2. Your object needs to persist outside the lifetime of the allocating scope (i.e. past the '}')
  3. You want to share your object across multiple threads (usually a bad idea).
  4. You have some kind of stupid control flow requiring it, like throwing gotos around everywhere (really just a specialization of #2).

 

#2 is pretty much unavoidable in any reasonably complex program, but what's important is to recognize when it's necessary.

0

Share this post


Link to post
Share on other sites

Don't avoid pointers out of fear; if you're purposely avoiding them then maybe C++ isn't for you, you might be better off using another language. You can make some pretty intense gfx games with C# (if you're writing for the Windows eco-system.) As already stated there are a number of rules-of-thumb, some tried and true patterns, and some really great new support in C++0x that make pointer management pretty easy.

 

Another thing you can do; when I was starting out with C I sat own with my compiler (and GCC or CLang is really nice for this, you can just install cywin and the gcc toolchain if you're not willing to stray from windows) and worked through some example pointer patterns until I got a fairly deep understanding of how they work. It didn't take long either.

 

If you want to be a proficient programmer with graphics you should invest the time it takes to be comfortable with pointers. Either that or just write tic-tac-toe games with other languages. Smart phone developers seem to do pretty well.

 

Since programming in C and later moving to C++ professionally for 15 years (university and learning by myself before that), and understanding all the nuances of pointers, even I on rare occasions forget to delete/free() some pointer.  And that always causes some problem, often hard to track down ones.

 

Using smart pointers can remove that small bug that all humans are prone to create.So, it isn't about being "afraid" of pointers, it's just common sense.

 

Heck, I'm a really good driver, why would I need an air bag in my car?  I don't ever make a mistake while driving....

1

Share this post


Link to post
Share on other sites
So, it isn't about being "afraid" of pointers...

Isn't that something you should ask the OP about? Sounds to that's exactly what he was talking about.

 

Dynamic memory allocation is useful (and required) in the following instances:
  1. Your object is too large for the stack.
  2. Your object needs to persist outside the lifetime of the allocating scope (i.e. past the '}')
  3. You want to share your object across multiple threads (usually a bad idea).
  4. You have some kind of stupid control flow requiring it, like throwing gotos around everywhere (really just a specialization of #2).

 

#2 is pretty much unavoidable in any reasonably complex program, but what's important is to recognize when it's necessary.

 

Um, 3 is often unavoidable in any useful program, and very common. that's why locks and mutexes are for. You don't share access so much as control access serially to one or more threads.

1

Share this post


Link to post
Share on other sites


The real proper use of these are all based on _lifetimes_. Who is responsible for creating the object? Who is responsible for destroying? Are users of the object just borrowing it for a while or do they need to take over responsibility of the lifetime? If they're just borrowing the object, how long do they need to borrow it for?
Quoted for emphasis.

I honestly find given proper design thinking about ownerships and lifetimes beats smart pointers at large.

1

Share this post


Link to post
Share on other sites

 

There is nothing wrong with using dynamic memory. In fact, for any suitably non-trivial application you will find it impossible to NOT have dynamic memory allocation.
 
If you're doing this in C++ then you should be using modern C++ methodologies, such as smart pointers.
 
Additionally, when implementing your own classes that contain raw pointers you should always be sure to implement the rule of three at the minimum, rule of five at best.


While understanding and following the Rule of Three/Five is important, I would suggest striving for the Rule of Zero. (Which is basically a variation on the single responsibility principle)

 

EXACTLYYYYY finally one man on earth understands programming. damn. why is it so hard to make people forget about those damned rule of 3/5 ?

0

Share this post


Link to post
Share on other sites

There is nothing wrong with using dynamic memory. In fact, for any suitably non-trivial application you will find it impossible to NOT have dynamic memory allocation.
 
If you're doing this in C++ then you should be using modern C++ methodologies, such as smart pointers.
 
Additionally, when implementing your own classes that contain raw pointers you should always be sure to implement the rule of three at the minimum, rule of five at best.


While understanding and following the Rule of Three/Five is important, I would suggest striving for the Rule of Zero. (Which is basically a variation on the single responsibility principle)

EXACTLYYYYY finally one man on earth understands programming. damn. why is it so hard to make people forget about those damned rule of 3/5 ?


Because it's still important to know, especially if (when) you write your own resource management classes.
0

Share this post


Link to post
Share on other sites

EXACTLYYYYY finally one man on earth understands programming. damn. why is it so hard to make people forget about those damned rule of 3/5 ?


Not knowing the rule of 3/5 is like not knowing for loops. You have a giant gaping hole in your knowledge just waiting to come along and bite you on the ass.

There are a great many situations where the "rule of zero" will not work conveniently without you implementing the appropriate containers or proxies (which need to obey the rule of 3/5, by the way) in order to have your "rule of zero." Two examples where you need non-trivial handling of objects where the standard library is lacking convenient containers: handle duplication and COM with proper reference handling, unlike most DirectX code. Edited by Washu
1

Share this post


Link to post
Share on other sites

 

EXACTLYYYYY finally one man on earth understands programming. damn. why is it so hard to make people forget about those damned rule of 3/5 ?


Not knowing the rule of 3/5 is like not knowing for loops. You have a giant gaping hole in your knowledge just waiting to come along and bite you on the ass.

There are a great many situations where the "rule of zero" will not work conveniently without you implementing the appropriate containers or proxies (which need to obey the rule of 3/5, by the way) in order to have your "rule of zero." Two examples where you need non-trivial handling of objects where the standard library is lacking convenient containers: handle duplication and COM with proper reference handling, unlike most DirectX code.

 

This, plus the rule of zero depends on your compiler being able to automatically generate appropriate default equivalents for move ctor/assignment operator.  That isn't available everywhere just yet (although the situation seems to be improving).  Many people will be using older compilers, so you can't blindly forget about the rule of 3/5.

2

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