Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C++ pointers, references and Dynamic Memory... oh my :(

pointers references dynamic memory

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 297 posts

Posted 31 July 2018 - 02:08 PM

Hello,

 

coming back from a long long break due to career and personal changes in my life, my goal to learn C++ is all more on top of my list to do this year, especially learn c++ to use unreal game engine, why... to make games for fun.

 

so, i get the syntax, and all the cool things c++ has to offer than java/C#, however one of the biggest things (and there are other tops/features to c++ that i will soon encounter), is challenging and hard to grasp is, pointers, references and dynamic memory.

 

i get it, like writing the syntax, using it in console command etc., but my big question is WHY? why ever use pointers, references and dynamic memory? why ever use it, when you can easily write a c++ program with out them right? can anyone explain (dumb it down if you can), why we should use pointers, references and/or dynamic memory, would you use it still in a actual job? actual application, or is it nice to have, or a requirement to have?

 

examples will help me as an idiot, big thanks in advance :)


Its only funny till someone gets hurt.... THEN ITS HILARIOUS :)

#2 RoboticForest

RoboticForest

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 130 posts

Posted 01 August 2018 - 05:00 PM

OK. So, short answer is performance. I actually use references all the time because the data being worked on isn't moved around in the computer's memory or copied. I don't actually use pointers that often, but when I do I debug the ** out of them.

 

Dynamic memory is essential when building your own tools for certain tasks. I've made memory pools for objects, my own linked lists, and my own string classes before. All of which needed both dynamic memory and pointers.

 

I'm not sure if I can answer more specifically without know what you want to do, but I hope that gives you a general idea.


No trees were harmed in the sending of this message, but millions of electrons were severely inconvenienced.


#3 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 297 posts

Posted 03 August 2018 - 06:52 PM

Hello CC Addict, big thanks for taking the time to explain, so the biggest thing is performance for using pointers, let me know if this makes sense (i have been watching videos and reading forums about pointers and smart pointers etc. but still trying to get smart pointers so bare with me :( )

 

so i would use a pointer to probably store a large object/variable? then i would want to have it hold a lot of memory for it for performance purposes, especially if its going to be multiple same objects/variables, so i would declare a pointer like:

 

double * buffer = new double[8] which means 8 bytes for a double * by 8 = 64 bytes (correct?)

 

so i can store multiple double variables within the buffer object/variable? (would my example buffer be a variable or would it be a object?)

 

so let me know if that makes sense to you in a real world scenario, if not, let me know what is a good example in a real world scenario, again big thanks for taking the time to read and write to my post :)


Its only funny till someone gets hurt.... THEN ITS HILARIOUS :)

#4 RoboticForest

RoboticForest

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 130 posts

Posted 07 August 2018 - 05:07 PM

I wish I had some paper and a pencil. I find pointers easiest to explain in pictures.  :) 

 

Your code...

double * buffer = new double[8];

... does a couple things at once. And, yes it's a total of 64 bytes in size if you have 8 byte doubles.

 

First double * buffer makes a simple variable named "buffer" that holds an integer number. That integer is actually a spot in the computers memory, an address, and the thing at that address is a decimal number (the double). I think you already understand that, but I'm just going full tilt for ease and completeness (no offense intended  :biggrin:).

 

Imagine a set of mailboxes. Each box has a number and if I told you I'm at number 15 you'd know where to go to give me a letter.

double * buffer

is like

letter * boxNum

The next part, = new double[8] makes eight decimal numbers out in the heap, all in an unbroken line of memory. Their addresses (whatever they are) are contiguous, just like real-world mailbox usually are.

 

So, to answer your questions more directly:

so i would use a pointer to probably store a large object/variable?

 

Yes. Pointers are most often used to point at very large/complex objects that you don't want to be copying around, OR, for pointing at individual objects within a list, like the 8 element array in your example.

 

so i can store multiple double variables within the buffer object/variable? (would my example buffer be a variable or would it be a object?)

 

You can, in a way of thinking, store multiple objects, but technically no you are only "storing" one thing. The pointer itself is a variable. It only stores the location of a single double. What makes the pointer act like a container is that you know you have 8 doubles in a row and are looking at one of them. You can then make the pointer move around through your array by adding to or subtracting from the address that it holds. The pointer itself knows basically nothing about what it's pointing at. buffer points at some 8 byte thing and it's up to you to know what that is and how to use it. Add 1 to the buffer and it jumps ahead another 8 bytes. It's more than happy to point at literally any 8 bytes in the computer's memory.

 

so let me know if that makes sense to you in a real world scenario, if not, let me know what is a good example in a real world scenario.

 

One common use for pointers would be drawing images to the screen. For example: 1600 pixels by 900 pixels times 3 bytes per pixel (one byte for red, green, and blue) is a whopping 4,320,000 bytes! If you drew a nice picture to some drawing area in memory then wanted to transfer that to your monitor it will most likely go so slow that you would see it happening (even within a split second). So we might make something like:

drawingSurface * buffer = new drawingSurface; // Where we're going to draw.
drawingSurface * screen = new drawingSurface; // What the physical monitor will show.
setMonitorDisplay(screen); // Tell the computer which pointer points to displayable imagery.

/* Drawing code goes here. */

// Swap the pointers so that we "move" our drawing to the screen instantaneously.
drawingSurface * temp = nullptr;
temp = buffer;
buffer = screen;
screen = temp;

I hope that all made sense. Please let me know if you have any other questions and I'd be happy to clarify.

 

Talk to you later Siten. :thumbup1:


No trees were harmed in the sending of this message, but millions of electrons were severely inconvenienced.





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download