Hi I am relativly new to C++ and wonder if someone can explain the concept of pointers (which I sort of understand) and why they are so useful.
Sorry if this seems like a dumb question,
Thanks,
Alex
Printable View
Hi I am relativly new to C++ and wonder if someone can explain the concept of pointers (which I sort of understand) and why they are so useful.
Sorry if this seems like a dumb question,
Thanks,
Alex
A pointer is a variable that doesn't store objects but rather a location in memory. For example (and this might be wrong, I haven't done C++ pointers for a year):
That declares a pointer, allocates space for it to hold SomeClass, calls the pointer's value's SomeMethod method, and frees up memory by deallocating memory for the pointer.Code:SomeClass *x;
x = new SomeClass();
x->SomeMethod();
delete x;
Why would you want to use a pointer?
Thanks,
Alex
Assume that you have a string "Hello World". You want to pass this to a function UCase().
Instead of passing the whole string you can pass a pointer to the string. When you pass a pointer to a string, the pointer contains the address on the first char (H in this case). The function will read each char until it reaches the Null terminator (Used in C/C++ to determine the end of a string in memory)
As a further note. VB always passes a string as a pointer even when you use ByVal.
Hope you understand the above, if not I will clarify for you.
Stuff you have to read:
http://newdata.box.sk/bx/c/htm/ch08.htm
Thanks for the replies.
That link Kedaman is good - this is a detailed and difficult subject for me, I think it will take a couple of read throughs!
FantastichenEin can you not just pass the string to the function rather than passing the pointer to it? Is it more efficent passing the pointer?
I think the article kedaman sent said that pointers are sometimes used to allow you to access variables outside a functions scope but surely there are other ways around this like static?
In VB to change a value that is passed to the function you use the by ref, are pointers c++ way of doing this? Surely you could just assign the value returned from the function?
Sorry dont know if I am making much sense!
Thanks for help,
Alex
I don't think it is possible in C++ to pass a string to a function (I may be wrong about this).
It is more efficient to pass a pointer as a pointer is only a Long whereas a string is Number of chars.
When using byRef Vb just passes a pointer to the string.
In VB to change a value that is passed to the function you use the by ref, are pointers c++ way of doing this? Yes
When using ByVal in vb, Vb first copies the string to a new memory location, and then passes to address of the new location. This means that the origional string cannot be modified by the function
Because it's not really a string, but an array of characters, so you pass a pointer to the first char in the array.Quote:
I don't think it is possible in C++ to pass a string to a function (I may be wrong about this).
Another use a pointers and strings is this:
Suppose you have an array of characters that represent the a file name and its full path. ("C:\folder\folder2\file.xml"). Suppose you want just the short file name ("file.xml"). You could use a pointer to the 'f' char in the file name.
Pointers make passing arrays very simple:
You ALWAYS need to delete pointers after you use them,Code:void test(int arr[], int size)
{
for(int i = 0; i < size; i++)
{
arr[i] = i;
}
}
// now you have no way of passing the array back to the calling
// function. You can't do "return arr"
// if you call test() from main and pass it an array,
// then the array will be unchanged after the function
// returns.
// you also can't declare: void test(int &arr[], int size);
// but with pointers:
void main()
{
int * array = new int[5];
test(array, 5);
// now array = {0, 1, 2, 3, 4}
delete[] array;
}
otherwise you have allocated memory that never gets
deallocated causing a memory leak. ;)
/
Although, any good OS should clean itself up after running every program...this basically means that memory leaks are only critical if your program will be run for a long period of time.
For example, under Windows:...is fine, and the memory will be freed when the RTL winds up the call stack.Code:int main() {
int *x = new int[5000];
return 0;
}