pointers

is it making a new copy of memory adress (or variable) when passing a pointer on function? if yes , so it is wasting more memory than a passing normal variable?
so whats the point of pointers?
if no, can you explain it to me?
is it making a new copy of memory adress (or variable) when passing a pointer on function?

A pointer is simply a type of variable that contains a memory address.

When you pass a pointer to a function, the value of the pointer is pushed onto the stack. Is this 'wasting memory"? That depends on whether the pointer was needed or not. If the pointer was needed, then it was not "wasted". A pointer takes however many bytes the address size is on your platform (4 bytes on a 32 bit platform). This is typically the same size as an int. If you're passing around large objects, it is more efficient to pass a pointer than to pass the object by value which requires copying the object.

Pointers are used arguments to many C functions. Pointers are less important in C++ where objects can be passed by reference. An exception to this with C++ is dynamic memory. Dynamic memory is always allocated and released via pointers.
When you pass an object by reference in C++, the compiler is actually generating a pointer to the object.
If you're passing around large objects, it is more efficient to pass a pointer than to pass the object by value which requires copying the object

isnt it passing pointer which has the adress of an object is also the same as copying the object?


When you pass an object by reference in C++, the compiler is actually generating a pointer to the object.


i see sometimes it would be more good to pass by reference than using a pointers
Last edited on
isnt it passing pointer which has the adress of an object is also the same as copying the object?
No.

i see sometimes it would be more good to pass by reference than using a pointers
There is a difference. A pointer can point to (can be bound to) an object or nothing, whereas a reference is always bound to an object.
can you give me tips on when to use pointers?
Last edited on
Use only when you have to.

Prefer smart pointers to plain pointers.


Have you encountered "class inheritance and run-time polymorphism"?

Prefer smart pointers to plain pointers.

whats that?


Have you encountered "class inheritance and run-time polymorphism"?

i havent yet
Smart pointers: search with that term or look at the "managed pointers" section in http://www.cplusplus.com/reference/memory/
Topic archived. No new replies allowed.