I think i've got pointers

closed account (iAk3T05o)
After reading up to/more than 10 different articles and tutorials more than once, i think i finally understand why pointers should be used.
1
2
3
4
int *pointer = new int;
pointer = 4
std::cout << *pointer;
delete pointer;

this saves 4 bytes of memory once deleted unlike
1
2
int pointer = 4;
std::cout << pointer;

which never releases the 4 bytes used.
Is this correct?
Thanks.
Nope not correct. Read a decent book about C++ first then add onto that basic knowledge using tutorials
closed account (iAk3T05o)
I've read 4 books over and over again for 4 days.
That my example was dynamic memory allocation.
Could you explain why i should use a pointer?
1
2
3
4
5
6
int x = 5;
int *p_x = &x;
*p_x = 10;
std::cout << p_x; //print address pointed to by p_x
std::cout << *p_x; //print value stored at address pointed to by p_x, 10
std::cout << x; //prints 10 because of *p_x = 10  

which never releases the 4 bytes used.

Pointers are variables too.
They are like long unsigned int's which hold a number (the number being a memory address).
Depending on the computer type, pointers can use 4 or 8 bytes (32-bit and 64-bit should ring a bell).

So yes, delete'ing the pointer would release 4 bytes of memory for the int.
But the 4 or 8 bytes required for the pointer itself would still be used.

Local variables live in a part of memory called the stack.
Whenever a function is called, it allocates space for its local variables on the stack.
When the function returns, it deallocates the space. All of this happens automatically.

(This is a simplified explanation, more technically a stack pointer (I think) is being constantly changed.)

http://en.wikipedia.org/wiki/Call_stack

Now going back to the pointers... they can be used for dynamic memory allocation but in modern C++ you shouldn't.

If you need a dynamically allocated array, C++ gives you std::vector.
If you need a pointer to a function, C++11 gives you std::function.
If you need a pointer + dynamic allocation for polymorphism, C++11 gives you std::unique_ptr.

std::unique_ptr is a smart pointer.

Smart pointers behave like regular pointers, except they automatically delete the memory when they go out of scope. So you no longer need to manually delete anything.

1
2
3
4
5
6
7
8
#include <memory>

// ...

std::unique_ptr<int> smart_pointer(new int);

*smart_pointer = 4;
std::cout << *smart_pointer << '\n';


So for a lot of things you'd use pointers for, C++ offers better alternatives.

You'll end up using pointers when you write your own data structures, for example trees, where parent nodes link to their children nodes via pointers and there's no other way.
Last edited on
closed account (iAk3T05o)
Yeah i know char *stringy[] is equivalent to std::string stringy and dynamic arrays are easier with vectors.
I hope my previous post did not confuse you. Just to simplify and maybe clear things up:

Local variables are allocated on the memory when the function is called, and deallocated when the function returns, automatically. Both the pointer and the int are local variables.

Yeah i know char *stringy[] is equivalent to std::string stringy and dynamic arrays are easier with vectors.

Strictly speaking it is not. It's an array of pointers to char.
http://www.unixwiz.net/techtips/reading-cdecl.html

It also does not resize automatically as std::string does, etc.

I do understand what you're trying to say, and you have the correct mindset.
In C++ you should use std::string (called C++ strings) instead of char arrays (called C strings).
For some reasons why you might use pointers, have a look at my post in this thread:

http://www.cplusplus.com/forum/general/105593/
closed account (iAk3T05o)
I only understand 1 in your list. What does the rest mean?
If you haven't come across these concepts in your learning process, you will in good time.

If you're really interested in any of those topics, then Google is your friend.
closed account (iAk3T05o)
Thanks. 1994 posts, (20 more to 2014) ;).
Topic archived. No new replies allowed.