Help me wrap my head around pointers and iterators.

Pages: 123
Now that I have a better understanding of pointers (I'd still say it's below average) I might be able to understand polymorphism a little better as well. What would be the best way to get better at understanding them. I have a narrow mindset about how I'm doing things now, work, but I want to expand and be able to use pointers when I need to, or when it would be better to use them, than just avoiding them altogether (I know that is a long run on sentence, but hopefully you understand).

I think I've seen a function return a pointer before (actually, I'm pretty sure it can), but I'm not sure how you can "point" to a function. Or are you just pointing at the return value then? I would assume the returned object would have to be something stored on the heap (correct term?) then so it can be accessible once the function's scope ends...It's kind of like slowly completing a puzzle in my mind as to when pointers can be used. =D
 I want to expand and be able to use pointers when I need to, or when it would be better to use them, than just avoiding them altogether
Implementing linked lists and binary trees did wonders for me.

 I think I've seen a function return a pointer before (actually, I'm pretty sure it can), but I'm not sure how you can "point" to a function.
I've never used function pointers myself, and I'm still not comfortable with the syntax. But a function, like any variable has an address which when a function is called the address is placed on the stack. Hopefully someone can clarify function pointers for both of us.

Edit: After a quick google, they may not be too complicated. http://www.newty.de/fpt/intro.html#what
On that page, `float (*pt2Func)(float, float)` is a pointer to a function that takes 2 floats and returns a float.
Last edited on
 but something I'm curios about is if the [] operator is so slow, and there is no fast way to traverse through a linked list, how can you insert an object somewhere in the middle? Does it iterate through the list until it finds where to put it?

You give insert an iterator, and it inserts the element before that iterator.

The list page on this site gives a decent example:
http://cplusplus.com/reference/stl/list/insert/

 I think I've seen a function return a pointer before (actually, I'm pretty sure it can), but I'm not sure how you can "point" to a function.

IMO C++11 functions are a million times simpler (and infinitely more flexible) than traditional function pointers:

 ``123456789101112131415`` ``````#include int add( int a, int b ) { return a + b; } int sub( int a, int b ) { return a - b; } int main() { std::function op; // note the function signature inside the <> op = add; // simple assignment cout << op(5,3); // prints 8 op = sub; cout << op(5,3); // prints 2 }``````

And that's just scratching the surface. The things you can do with placeholders and std::bind are immensely powerful.
I've used functional before, didn't realize they were really pointers, but I was referring to a function returning a pointer. Maybe it doesn't exist, but I swore I've seen functions that looked like:
 ``123`` ``````int *myFunction() { return new int(5); }``````

Does that not exist? What happens to the new int? Where does it get stored in relation to the program? On the previous function's stack block? In the heap?
 Does that not exist?

It does exist.

 What happens to the new int?

Do you mean "what happens to the memory allocated by new"? Nothing happens to it. At some point in the future it will be deallocated by a delete.

 Where does it get stored in relation to the program? On the previous function's stack block? In the heap?

In the heap. If it's made with new, it goes on the heap.

Functions can return any data type there is. A pointer is a data type. Functions can return pointers.

 I think I've seen a function return a pointer before (actually, I'm pretty sure it can), but I'm not sure how you can "point" to a function.

A function exists as numbers in memory; the numbers are interpreted as instructions for the processor. These numbers begin somewhere. That somewhere is a memory location. A pointer holds a memory location.
Last edited on
 In which situation is it better to "new" something instead of creating the object each time?

Like it's already been stated it depends on the situation. Maybe this will help. You know about the stack and the heap. Allocating memory on the stack is faster than allocating on the heap. This is how I think of it, if I don't need the object to live outside of the scope that I'm currently in and I need to allocate, I use the stack, in your example that is the non pointer. If I need the object to live outside the current scope, then I will use the heap '`new`' (an example is the returning a pointer from a function that allocated a new object on the heap, you are responsible for deleting said memory).

Also, I prefer to use const reference over pointer whenever possible and only use pointers for legacy API's that require it, or where the object I am referencing will need to be changed (since a reference can only be initialized and not changed).

I would also suggest grabbing some used books from Scott Meyers, they helped me, I still go back and reference some of the golden nuggets in there. You can grab them used off Amazon for as little as 3.99 + ship.

More effective C++
Effective C++
Effective STL

come to mind.
Last edited on
I have one last question. When returning a pointer, is it possible to use a basic data type but just use the reference operator to accept it? For example:
 ``12`` ``````myClass myObject; &myObject = FunctionReturningPointer('a');``````

I haven't had time to test it, but it just occurred to me that a pointer variable should behave the same way as a basic variable using the reference operator.
No that does not work.

&myObject gives you the address of where 'myObject' resides in memory. Reassigning that to something else implies that you are moving the object to somewhere else in memory, which isn't allowed (at least not that way).

You can, however, assign pointer variables to a function returning a pointer:

 ``1234`` ``````myClass* ptr = &myObject; // OK, ptr points to myObject ptr = FunctionReturningPointer('a'); // Also OK // however, note now that ptr no longer points to myObject, but points to a different object. ``````
So the reference operator isn't allowed to be used for lvalues? Only rvalues?
It's the 'address-of' operator in this case. But yes, that's correct.
I know this thread is getting long, but I wanted to clear up a few simple questions I had by searching wikipedia. I came across this snippet:
Wikipedia wrote:
While most operators on arrays and pointers are equivalent, it is important to note that the sizeof operator will differ. In this example, sizeof(array) will evaluate to 5*sizeof(int) (the size of the array), while sizeof(ptr) will evaluate to sizeof(int*), the size of the pointer itself.

Why is `sizeof(int)` not the same size as `sizeof(int *)`?
 Why is sizeof(int) not the same size as sizeof(int *)?

Often they are, but it depends on your system.

An int is a kind of data type.

An int-pointer is a different kind of data type.

They are different kinds of object, so they can be different sizes.

Would you ask why an int is not the same size as a double? It's the exact same thing here.

it is, isn't it? it is on my implementation.

the size of an int is the size of an int, a pointer is a memory address with a size that's big enough to hold that memory address.

mine are 4 in both cases, it may be possible your int is 8
I guess I'm asking more of why wouldn't they always be the same? An int pointer should always point to an int, so they should always be the same size correct?
 An int pointer should always point to an int, so they should always be the same size correct?

Incorrect.

What is an int for? Storing an integer value.

What is a pointer for? Storing a memory address.

They are for completely different things. They are a size suitable for their purpose. Often, they will be the same size, because four (or 8) bytes is a convenient size for an int AND a convenient size for a pointer. This may be confusing you.

Imagine a class object that is ten megabytes in size (stores lots of internal data). How big will a pointer to that object be? Will it be ten megabytes as well?
Last edited on
not really, no, an int doesn't hold the same data type as a pointer, a pointer holds a memory address, that isn't an int, that memory address is just used for getting the int, the size of a memory address and the size of an int do not have to be the same.
Here's my thoughts:

`sizeof(int *)` is the size of a pointer, this is bigger than than the size of an int because it holds a memory address.

There is another situation where sizeof returns a different value to what might be expected, it happens because of memory alignment requirements:

 ``123456`` ``````struct MyStruct { int MyInt =0; double MyDouble =0.0; }``````

The sizeof this struct will probably be 2*sizeof a double, and not, sizeof int + sizeof double.

HTH
Does that mean that all pointers will be the same size then (with the obvious exceptions to a container, array, etc.)?
Bingo. All pointers do the exact same thing, and have to store exactly the same information. They will all be the same size.
on the same implementation, yeah, even with containers and arrays, on other OS or compilers the size may be different.

TheIdeasMan, have you even tried it? a pointer doesn't have to be bigger than an int simply because it holds a memory address.

and you're right, it is 2*sizeof double
Last edited on
Pages: 123