What are c++ pointers useful for?

stoymigo

Senior Member
Joined
Dec 11, 2008
Messages
975
Reaction score
26
I'm studying c++, in what type of apps are the c++ pointers used?
Thanks
 
They're typically used in applications that prefer to do memory management themselves (e.g., 3D game engines).

The reason being is that you can get better performance by implementing a custom memory manager (with garbage collection, pre-allocation, pooling, etc...techniques) because you tune the memory management to deal specifically with whatever data you're storing.

In the case of game engines, allocation speed is critical because you have the potential to allocate memory to many dynamic objects and systems, many times a second. These objects are usually small (for entities, particles, world state, etc...) but extremely large in number.

Although, a good game engine will try and prevent this by having free space for new objects as much as possible in both memory management and system specific scope.
 
Last edited:
I'm studying c++, in what type of apps are the c++ pointers used?
Thanks

All apps. Instead of passing around large objects and mucking around with the heap too much, you pass around a pointer, which is vastly more efficient.
 
Its sort of like this, instead of bringing the funfair to you, someone just points you in its direction. So much quicker, no setup times etc.

how can this be useful-
Im not sure about C++, but some of the other languages used to create a copy of a variable when you passed it into a function, so if you changed the value, it didn't alter the original. You had to pass in pointers instead.
 
Last edited:
Here's a list of some pointer usages:

Linked lists
Binary trees
It supports polymorphism

Protip: Never use pointers unless it's absolutely necessary.
 
Protip: Do not be fearful of pointers.

Lol, I remember years ago working with pointer and if you declared a array but didn't fill it, it would have some random junk in it. What was funny was it used to use the borland c++ compiler manual to fill it so if you addressed wrong you'd get some random string of text that sort of made sense. Would freak people out :D
 
A pointer is just the address in the memory where data is stored. If you want to work with the data by calling a function its much more efficient to give the address of the data (a few bytes in size) to the function instead of transferring the actual data (could be a few megabytes).
 
Are pointers available in c# also?
Because c# also uses linked lists.

Would pointers useful in data transfer operations (like form large files/big databses) or are data transfer operations limited to the hardware used?

Thanks, again.
 
Last edited:
In .NET (C#/VB.NET) you could get a pointer if you mark your code as unsafe. Otherwise heap memory is accessed with references IIRC. Similar to pointers.

References are available in C++

eg.
Code:
int& someInt;

Not exactly sure how .NET works, they have structure types (struct) which is allocated on the stack IIRC. Whereas their class objects are allocated on the heap.

A pointer or reference is an address to a memory location, the pointer/reference itself is a "variable" on the stack. The pointer/reference is a stack variable that points to a memory location, generally on the heap.

So with C# a class would be accessed with a reference. Whereas a struct would be accessed directly. They thought this was a very clever idea to give the programmer the choice, heap or stack.

Java has taken it a step further. You have new operator which gives you a reference. However Hotspots will decide if it wants to put that variable on the heap or stack. In fact it can decide to forget about it all together and just work with it class variables directly on the CPU registers. Something not feasible in C++ which is why some benchmarks are faster on Java than even C.

.NET and Java handle the allocation and deallocation of memory for you. The use block allocation and deallocation. Here my knowledge is lacking but it is orders of magnitude faster to allocate large amounts of objects in Java and theoretically .NET than in C/C++ because of the way memory is allocated.

Something to keep in mind while thinking of how "wonderful" pointers are ;)
 
Last edited:
In VB.NET you can use "pointers" in function arguments by using the "ByRef" option. See http://stackoverflow.com/questions/4383167/byref-vs-byval-clarification

C# is a (memory) managed programming language, so pointers are used behind the scenes, like when you send an object (and not just a primitive) as an argument to a function.
You can use (unmanaged) C code in C# - like with DLL calls, but it is pretty nasty, because you can then easily get memory leaks. There are a bunch of classes that allow you to allocate memory to unmanaged data structures and convert data between unmanaged and managed data structures.

When you program something in C with data structures, then you have a very high chance that you'll need to use pointers somewhere, but not necessarily double pointers (or pointers to pointers).

Managing the memory in C can quickly become pretty difficult, especially if it is a multi-threaded application. I eventually had to write like wrappers in C to manage my data structures, which made them look like C++/Java classes - so I probably could've just used C++ from the start.
 
C++ pointers are useful for passing your BSc in Computer Science ;)
 
I stand to be corrected.

But a good example would be this.

Open a 100 Mb text file with normal windows Notepad.exe you will notice it hangs for a while untill it has loaded all of the data into the notpad text box or crashes as the text box cant handle all of the data at once (I think in VB terms notepad will be able to handle the maximum amount of data a Vb6 string can handle which is again i stand to be corrected an integer amount of characters.

scroll up and down about and you will see that it hangs as its trying to get to the point where you scrolled to.

Then open the same file with Notepad++ you will see that it can scroll smoothly and that's because it reads from Hdd as you scroll. and has a good memory manager and is only pointing to sections in the memory which is usefull to you at the time
 
Last edited:
I stand to be corrected.

But a good example would be this.

Open a 100 Mb text file with normal windows Notepad.exe you will notice it hangs for a while untill it has loaded all of the data into the notpad text box or crashes as the text box cant handle all of the data at once (I think in VB terms notepad will be able to handle the maximum amount of data a Vb6 string can handle which is again i stand to be corrected an integer amount of characters.

scroll up and down about and you will see that it hangs as its trying to get to the point where you scrolled to.

Then open the same file with Notepad++ you will see that it can scroll smoothly and that's because it reads from Hdd as you scroll. and has a good memory manager and is only pointing to sections in the memory which is usefull to you at the time
 
I stand to be corrected.

But a good example would be this.

Open a 100 Mb text file with normal windows Notepad.exe you will notice it hangs for a while untill it has loaded all of the data into the notpad text box or crashes as the text box cant handle all of the data at once (I think in VB terms notepad will be able to handle the maximum amount of data a Vb6 string can handle which is again i stand to be corrected an integer amount of characters.

scroll up and down about and you will see that it hangs as its trying to get to the point where you scrolled to.

Then open the same file with Notepad++ you will see that it can scroll smoothly and that's because it reads from Hdd as you scroll. and has a good memory manager and is only pointing to sections in the memory which is usefull to you at the time

That's relevant to what I'm asking about, but does that involve pointers or just reading part of the file using the file library functions? Thanks
 
C++ pointers are useful for passing your BSc in Computer Science ;)

That's a good answer :p

That's relevant to what I'm asking about, but does that involve pointers or just reading part of the file using the file library functions? Thanks

It will be reading the file directly via pointers. A manageable section of the file will be loaded each time.
 
Disk reads and pointers don't really have too much to do with each other.
 
Top
Sign up to the MyBroadband newsletter
X