|
-
Aug 30th, 2002, 01:37 PM
#1
Thread Starter
Frenzied Member
Pointer usage?
I was reading, and I never did understand pointers...
But I think I get the purpose now...
Say we have a object that takes 2kb of ram, and to pass
that object to a routine would mean copying that 2kb to
execute that routitine!
But with a point its 2bytes of memory to pass... Just the
address of the object... Right?
So the purpose of a pointer is to save time by just passing the
address?
Is that all correct?
-
Aug 30th, 2002, 02:35 PM
#2
Frenzied Member
Pointers (in the sense you mentioned) is just like passing ByRef in VB.
C++ has two ways to pass parameters - by value (the default)
or by reference (using a pointer).
BTW an array reference is a pointer already.
Pointers are used a lot for processing data as well. They are just not in VB, so their use will seem weird to someone who only knows VB, I guess.
For example, find the character furthest along in the collating (ASCII) sequence in a string:
Code:
unsigned char maxchar(char *s) // an array of char passed as a pointer
{
unsigned char *buf; // working pointer
char retval='\0'; // char to return default is null
buf=(unsigned char*) s; // work against s using buf
while (*buf!='\0'){ // loop til end of string
if( (*buf > retval )
retval=buf; // if bigger, replace value of retval
buf++; // next character in the array
}
return retval; // return biggest one found
}
-
Aug 30th, 2002, 03:43 PM
#3
Thread Starter
Frenzied Member
But is the point of using them to speed of Routine calls or not?
-
Aug 30th, 2002, 04:59 PM
#4
Frenzied Member
Only in a small part - using them like in the code above, or using them to generalize (you can create function pointers) programming is what they are all about.
-
Aug 30th, 2002, 05:11 PM
#5
Thread Starter
Frenzied Member
Originally posted by jim mcnamara
Only in a small part - using them like in the code above, or using them to generalize (you can create function pointers) programming is what they are all about.
I dont understand..
What asspect of your program will it improve in the way your specified?
-
Aug 31st, 2002, 05:25 AM
#6
Frenzied Member
The example uses a pointer like Mid(a$,x,1) in Visual Basic.
YOu use it for lists, arrays, queues, so on. You cannot do that in VB. You also use pointers to create data storage.
You can use a pointer like:
Code:
'vb
for x = 1 to 100
b=a(x)
next
' C++
int *a=&b[0];
for(i=0;i<100;i++) b=*a++;
What you need to do is mess around with about 4-5 different programs.
-
Aug 31st, 2002, 08:09 AM
#7
Hyperactive Member
linked lists are excellent with pointers. They will, like you said, use less memory on your computer. I'm not sure how much extra SPEED you'll get, but the memory can really be saved like that.
If I agree with you today, don't get used to it.
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
|