|
-
Feb 3rd, 2003, 10:32 AM
#1
Thread Starter
Addicted Member
passing pointers
i have a string pointer declared in a class like this:
class Track {
public:
void Init();
private:
int TrackNum;
string *LlamaNames;
};
void Track::Init() {
LoadInfo(LlamaNames,TrackNum);
}
void LoadInfo(string *Names, int Num) {
switch (Num) {
case 1:
Names = new string[3];
Names[0] = "blah"......
break;
case 2:
Names = new string[4];
Names[0] = "blah1"......
break;
}
}
Basically i want to call Init() on a track after changing its track number. Based on that tracknumber, it will load different information into the class. And thus, i want to dimension a spot in memory outside of the class. As my understanding, Names (local) is pointing to the same spot as the member pointer LlamaNames, so shouldn't dimensioning Names dimension the same memory spot that LlamaNames points to? When ever i run it it crashes.
-
Feb 3rd, 2003, 10:57 PM
#2
Hyperactive Member
You need to use pointer-to-pointer or reference-to-pointer
Try the below
Code:
//Pointer-to-pointer example
class Track {
public:
void Init();
private:
int TrackNum;
string *LlamaNames;
};
void Track::Init() {
LoadInfo(&LlamaNames,TrackNum);
}
void LoadInfo(string **Names, int Num) {
switch (Num) {
case 1:
*Names = new string[3];
*Names[0] = "blah"......
break;
case 2:
*Names = new string[4];
*Names[0] = "blah1"......
break;
}
It should work now. This computer I'm using now, doesn't have a compiler for me to test it out.
-
Feb 3rd, 2003, 11:27 PM
#3
Hyperactive Member
Code:
void LoadInfo(string **Names, int Num) {
switch (Num) {
case 1:
*Names = new string[3];
*Names[0] = "blah"//dereferencing twice
break;
case 2:
*Names = new string[4];
*Names[0] = "blah1"//dereferencing twice
break;
}
If it still can't work, change the code in red to
(*Names)[0] = "blah1"//dereferencing twice
I will explain the ptr-to-ptr syntax if you want. But right now I'm busy.
-
Feb 4th, 2003, 09:55 AM
#4
Thread Starter
Addicted Member
Thanks a lot! My code works great.
jmiller
-
Feb 6th, 2003, 10:05 PM
#5
Thread Starter
Addicted Member
another question:
what happens if i have this:
void Pass(User *UD, int index) {
cout << UD[index].Name;
}
int main() {
User *UserDatabase = new User[10];
Pass(UserDatabase,5);
}
By doing this, am i creating an entirely new copy of the array in Pass with UD as the pointer to it? In other words, is *UD a pointer to an indentical copy of UserDatabase that's local to Pass, or it is a pointer to the same memory spot that UserDatabase is pointing to?
-
Feb 7th, 2003, 04:26 AM
#6
Hyperactive Member
Originally posted by jmiller
By doing this, am i creating an entirely new copy of the array in Pass with UD as the pointer to it?
No. You are just only passing the address.
Originally posted by jmiller
In other words, is *UD a pointer to an indentical copy of UserDatabase that's local to Pass,
No.
Originally posted by jmiller
or it is a pointer to the same memory spot that UserDatabase is pointing to?
it is another pointer to the same memory spot that UserDatabase is pointing to. That's why it is useless changing that pointer, as the original pointer UserDatabase still remains the same.
Below link contains everything you want to know, it talks about the need to use ptr-to-ptr and ref-to-ptr and their reasons. If you are lazy to read everything, just read CBasicNet's posts
Pointer to reference or Reference to Pointer?
Here's a more direct link
Why they are needed?
-
Feb 7th, 2003, 05:05 AM
#7
Hyperactive Member
Actually you need only to add 1 & to your code in the first post, and it would work as you wanted. But it is 'unsafe' for unsuspecting users like you as the same piece of code does much more now.
Anyway if you understand ptr-to-ptr, you will grasp ref-to-ptr easier.
Most people prefer ptr-to-ptr over ref-to-ptr as it is clearer to programmers themselves of what they are doing.
Code:
class Track {
public:
void Init();
private:
int TrackNum;
string *LlamaNames;
};
void Track::Init() {
LoadInfo(LlamaNames,TrackNum);
}
void LoadInfo(string *&Names, int Num) {
switch (Num) {
case 1:
Names = new string[3];
Names[0] = "blah"......
break;
case 2:
Names = new string[4];
Names[0] = "blah1"......
break;
}
}
-
Feb 7th, 2003, 01:01 PM
#8
Thread Starter
Addicted Member
When you pass a pointer, like:
int *pInt;
blah(pInt);
do you pass the value at the address that pInt holds, or do you pass the address?
On my second example,
void Pass(User *UD, int index) {
cout << UD[index].Name;
}
int main() {
User *UserDatabase = new User[10];
Pass(UserDatabase,5);
}
if i want to reference back the memory that UserDatabase points to from Pass, don't I need to use ptr-ptr?
like:
void Pass(User **UD, int index)
cout << (*Ud)[index].Name;
}
and then call pass like this: Pass(&UserDatabase)? Wouldn't this actually reference back to the original memory? If so, what does having this:
void Pass(User *UD,int index); Pass(UserDatabase)
do?
thanks for all your patience, i really appreciate you guys
jmiller
-
Feb 7th, 2003, 08:45 PM
#9
Hyperactive Member
Original posted bi jmiller
When you pass a pointer, like:
int *pInt;
blah(pInt);
do you pass the value at the address that pInt holds, or do you pass the address?
It passes the address, which pInt holds.
Original posted bi jmiller
On my second example,
void Pass(User *UD, int index) {
cout << UD[index].Name;
}
int main() {
User *UserDatabase = new User[10];
Pass(UserDatabase,5);
}
if i want to reference back the memory that UserDatabase points to from Pass, don't I need to use ptr-ptr?
like:
No, unless you are changing the address. Why don't you read the links I gave you?
Original posted bi jmiller
void Pass(User **UD, int index)
cout << (*Ud)[index].Name;
}
and then call pass like this: Pass(&UserDatabase)? Wouldn't this actually reference back to the original memory? If so, what does having this:
void Pass(User *UD,int index); Pass(UserDatabase)
do?
This I will have to explain as it is not explained in the links.
void Pass(User **UD, int index);//Function prototype
......
Pass(&UserDatabase);
UD : UD==&UserDatabase : UD contains the address of the pointer, UserDatabase. You would never change this, if you do, UD is no longer pointing to UserDatabase (which makes the whole ptr-to-ptr thing pointless.)
*UD : *UD==UserDatabase : Anything, you give to *UD, goes into UserDatabase pointer;You are effectively changing UserDatabase itself.
**UD : **UD==*UserDatabase : Self-explanatory
-------------------------------------
void Pass(User *UD, int index);//Function prototype
......
Pass(UserDatabase);
UD : UD is another pointer which points to the same thing, UserDatabase is pointing. Changing UD doesn't change UserDatabase itself as it is another variable.
*UD : Self-explanatory
------------------------------------
Original posted bi jmiller
thanks for all your patience, i really appreciate you guys
jmiller
It took me quite a while to understand it too. Anyway it is good to know them as some Win32 APIs and DirectX APIs make use of ptr-to-ptr.
-
Feb 8th, 2003, 07:54 PM
#10
Thread Starter
Addicted Member
Thanks alot trancendental. Before I was using pointers without really understanding them, but I think i've got a good grasp of the concept now.
thanks again,
jmiller
-
Feb 10th, 2003, 02:06 AM
#11
Hyperactive Member
You are welcome.
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
|