|
-
Nov 5th, 2001, 04:24 AM
#1
Thread Starter
Addicted Member
newbie to pointers
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
ASP, SQL, VB6, Java Script and dubious guitar playing skills.
-
Nov 5th, 2001, 06:38 AM
#2
Member
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):
Code:
SomeClass *x;
x = new SomeClass();
x->SomeMethod();
delete x;
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.
-
Nov 5th, 2001, 07:47 AM
#3
Thread Starter
Addicted Member
Why would you want to use a pointer?
Thanks,
Alex
ASP, SQL, VB6, Java Script and dubious guitar playing skills.
-
Nov 5th, 2001, 08:38 AM
#4
Lively Member
Assume
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.
-
Nov 5th, 2001, 09:05 AM
#5
transcendental analytic
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Nov 5th, 2001, 10:03 AM
#6
Thread Starter
Addicted Member
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
ASP, SQL, VB6, Java Script and dubious guitar playing skills.
-
Nov 5th, 2001, 10:10 AM
#7
Lively Member
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
-
Nov 5th, 2001, 11:49 AM
#8
Black Cat
I don't think it is possible in C++ to pass a string to a function (I may be wrong about this).
Because it's not really a string, but an array of characters, so you pass a pointer to the first char in the array.
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.
Josh
Get these: Mozilla Opera OpenBSD
I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.
-
Nov 10th, 2001, 02:50 AM
#9
Frenzied Member
Pointers make passing arrays very simple:
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;
}
You ALWAYS need to delete pointers after you use them,
otherwise you have allocated memory that never gets
deallocated causing a memory leak. 
/
-
Nov 11th, 2001, 01:13 PM
#10
Monday Morning Lunatic
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:
Code:
int main() {
int *x = new int[5000];
return 0;
}
...is fine, and the memory will be freed when the RTL winds up the call stack.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|