PDA

View Full Version : C++ pointers



Shuttleu
05-31-2009, 10:38 PM
i have been learning C++ recently and i have just read about pointers
this is what i have learnt and i want to know if i have got anything wrong or missed anything out

pointers only hold a memory address
put a & infront of a var name to return a memory address
to access the memory the pointer points to you put * infront of the pointer name
to create a pointer you put the var type it will hold followed by a * then the pointer name
when you create a pointer always assign it a to a var or set it to 0
to create a memory location in the free store use the keyword new followed by the object type or var type that it can hold which will then return a memory location that a pointer can hold
to free the memory in the free store call delete on the pointer
once you have deleted a pointer set it to 0 or NULL or if you call delete on it again it can cause the program to crash
if you assign a location in the free store make sure to free it before assigning it to another location or you will create a memory leak
if you create a pointer that is local and points to the free store, make sure to call delete before the function it was created in ends
to access a function or var inside a a class by using a pointer use one of the following: (*objectpointername).objectfinction or objectpointername->objectunction
to create a pointer which cant be changes use vartype const pointername
to create a pointer which points to a var that cant be changed use const vartype pointername
to create a pointer that cant be changed that points to a var that cant be changed use
const vartype const pointername

~shut

JAD
05-31-2009, 11:52 PM
A few things to remember,

When creating pointer variables, it is important to note that this:

int* x,y;

will make both x and y pointers. But this:

int *x, y;

Will make just x a pointer, but not y.


When you delete a pointer, you should immediately after it do pointer = NULL;


I think you've got the gist of it though. Pointers can be fun and very helpful if you know how to use them. Just make sure to use your reference (&) and dereference (*) operators correctly to avoid headaches.

You seem to be zipping along with learning C++. Good job :) Learning SCAR really helped me pick up the basics of other languages almost instantly.

Wizzup?
06-01-2009, 12:15 AM
A few things to remember,

When creating pointer variables, it is important to note that this:

int* x,y;

will make both x and y pointers. But this:

int *x, y;

Will make just x a pointer, but not y.


EDIT: JAD is right.

Shuttleu
06-01-2009, 12:30 AM
When you delete a pointer, you should immediately after it do pointer = NULL;

You seem to be zipping along with learning C++. Good job :) Learning SCAR really helped me pick up the basics of other languages almost instantly.
i did say that

once you have deleted a pointer set it to 0 or if you call delete on it again it can cause the program to crash
and 0 == NULL

and yeah i think that learning scar has really helped me learn C++


EDIT: Jad is right.

so in all those cases x and y are both pointers?

~shut

Wizzup?
06-01-2009, 12:31 AM
so in all those cases x and y are both pointers?
~shut

EDIT: Nevermind. JAD was right. I must have been really sleepy. ;)

JAD
06-01-2009, 01:54 AM
and 0 == NULL

You should set your pointers to NULL after deleting instead of 0. There are slight differences between the two; they are not exactly the same. NULL is a macro defining a pointer, which points to nowhere, and therefore cannot be dereferenced. 0 is a number, and can be used in all integer expressions.

You should just make the habit of setting deleted pointers to NULL instead of 0 ;)

Shuttleu
06-01-2009, 03:03 AM
You should set your pointers to NULL after deleting instead of 0. There are slight differences between the two; they are not exactly the same. NULL is a macro defining a pointer, which points to nowhere, and therefore cannot be dereferenced. 0 is a number, and can be used in all integer expressions.

You should just make the habit of setting deleted pointers to NULL instead of 0 ;)

okie doke
thanks

~shut

boberman
06-01-2009, 03:13 AM
You should set your pointers to NULL after deleting instead of 0. There are slight differences between the two; they are not exactly the same. NULL is a macro defining a pointer, which points to nowhere, and therefore cannot be dereferenced. 0 is a number, and can be used in all integer expressions.

You should just make the habit of setting deleted pointers to NULL instead of 0 ;)

And NULL is generally set to the value 0... Yeah, not much difference really (though I agree, using the NULL macro is just generally considered good practice.)

As for the NULL delete thing. I think it really depends on what context that point is defined in. If the pointer is local then I really don't see any problem with deleting it and letting it fall off to noman's land.

However, if the pointer is going to be used outside of whatever scope it was deleted in, then I completely agree that it should be set to NULL.