-3

I am trying to learn classes by making a simple board game in C++. I have a class named "board" that has two functions, drawBoard and fillBoard. When I'm creating the object in my main function, I seem to have two options.

1) board gameBoard;

2) board* gameboard = new board();

What is the difference between these two? Do I always have to create a pointer when using the 'new' operator? I'm mostly trying to understand what situations I would use one over the other in.

Also, when I define it as a pointer, do I always have to use the "->" instead of the dot when calling functions?

board.drawBoard(); versus board->drawBoard();

What is different between these two?

  • I would highly suggest google.com and cplusplus.com – Chantola Sep 29 '14 at 22:16
  • Google for c++ and dynamic memory – jpw Sep 29 '14 at 22:18
  • 1
    Possible duplicate: [RAII and smart pointers in C++](http://stackoverflow.com/questions/395123/raii-and-smart-pointers-in-c/395158#395158) – πάντα ῥεῖ Sep 29 '14 at 22:18
  • 1
    you might start reading [proper-stack-and-heap-usage-in-c](http://stackoverflow.com/questions/599308/proper-stack-and-heap-usage-in-c/599327#599327) – wonko realtime Sep 29 '14 at 22:18
  • See http://stackoverflow.com/questions/3673998/what-is-difference-between-instantiating-an-object-using-new-vs-without – Jesse W at Z - Given up on SE Sep 29 '14 at 22:19
  • 1
    rereading your question, i'd also recommend [what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome](http://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome) for the second part – wonko realtime Sep 29 '14 at 22:22

1 Answers1

0

1) board gameBoard;

This one is created on stack

2) board* gameboard = new board();

This one is created on heap.

Do I always have to create a pointer when using the 'new'

Yes.

do I always have to use the "->" instead of the dot when calling functions

-> operator is just a shortcut to (*board).drawBoard(). pointer are variables holding address of other variables if you want to access the variable that the pointer points to you use the star. The parenthesis are because of operator precedance

atlanteh
  • 5,615
  • 2
  • 33
  • 54
  • And you're expecting OP to know what the stack and the heap are? – Hatted Rooster Sep 29 '14 at 22:30
  • 1
    Thank you for the reply. You pointed me in the right direction to understand what is actually going on in the background. I read about the stack and heap and now have an unrelated question: since you're constantly freeing up memory in the heap does it get very fragmented? Would fragmentation even slow RAM down the way it does with a hard drive? If it does, is there something that periodically defragments the heap? – user1125316 Sep 29 '14 at 22:55
  • read this: http://www.design-reuse.com/articles/25090/dynamic-memory-allocation-fragmentation-c.html http://stackoverflow.com/questions/1592804/how-do-i-workaround-heap-fragmentation-in-a-c-server-program – atlanteh Sep 29 '14 at 23:00
  • Not correct. The `board gameBoard;` depends on where it is defined. When outside of a function, it will be in automatic or global storage, which is usually not on the stack. – Thomas Matthews Sep 30 '14 at 00:06
  • @ThomasMatthews *automatic* is only when it is inside a function. Outside a function is *static* storage. – M.M Sep 30 '14 at 00:32