Returning References and Pointers from functions
I''m not a complete newbie to C++, but I''ve been having a little troble understanding the content of Teach Yourself C++ in 21 Days by Jesse Liberty.
In chapter 11: References, he starts returning references and pointers from functions without explaining it in much depth.
Somehow what he has written in some chapters is contradictory to others.
First problem is that if your were passing an object into a function by pointer, why would you return that pointer? You are acting on the original copy. Same when he returns references that he passed into functions.
Why would you do that if you aren''t going to use the return type as a r-value?
Also, in one listing he passes an object into a function by manipulating a pointer and returning that same pointer, yet he says, he is passing it by reference???
Have I been mis-lead? If so, could someone explain?
Thanks
Ok some guy at Microsoft explained it as this:
Guy: you know the diff between reference and pointer?
Me: No.
Guy: a pointer is a pointer to the variable -- it is an actual value, an address. A reference is just another name for the variable -- it is the same variable. you can''t do pointer math to a name -- that wouldn''t make sense would it?
Ok, so that doesn''t really help -- it didn''t help me too much either until i thought about it. A pointer is number that is used to address memory. A pointer allows for nasty things such as pointer arithmetic, which allows us to write all over memory. A reference does not allow this, which is a good thing, protection.
Now, to answer your original question: Why return a pointer in the first place? Well assume we have the following function:
char *strcat (char *dest, char *source);
and we want to cat monkey onto it twice. We could do:
char dest[256];
char src[] = "monkey";
strcpy (dest, "bashful");
strcat (dest, src);
strcat (dest, src);
or we could do:
char dest[256];
char src[] = "monkey";
strcpy (dest, "bashful");
strcat (strcat (dest, src), src);
I hope you can see why this works. But you are prolly thinking, ok dmgoober that makes sense but its stupid. Ok i agree it is.
Take a more realistic example but you still probably will never do is:
CMonkey *GiveMeXMonkeys (int n)
{
return new CMonkey[n];
}
You probably never want to do this, as you now have no knowledge of where these new CMonkey''s are going to be stored. Take for example:
int foo ()
{
CMonkey *m = GiveMeMonkeys(3);
printf ("hi!");
}
whenever foo is called more monkeys are allocated, but never deleted (in general, you want to allocate new memory on the same level as it is going to be deleted or else you might forget to delete the memory and get memory leaks!)
In general, you would only want a function to return a pointer to data THAT HAS ALREADY BEEN ALLOCATED BY THE CALLER. A final example that actually makes sense is:
char *findPeriod (char *p)
{
while (*p != NULL)
{
if (*p == ''.'')
return p;
p++;
}
return NULL;
}
this returns a pointer to the first period (or null if the period is not in the string.) Notice findPeriod is returning a pointer to what it assumes is valid memory of the caller (it assumes it is getting a string, and that everything in that string up to the NULL is valid memory.) This keeps with the idea that a function should only return a pointer to memory allocated by the caller.
char sentence[] = "hello.mark"
char *m = findPeriod (sentence);
*m = ''!'';
replaces the period with an exclamation mark.
Hope that helps.
DmGoober
Guy: you know the diff between reference and pointer?
Me: No.
Guy: a pointer is a pointer to the variable -- it is an actual value, an address. A reference is just another name for the variable -- it is the same variable. you can''t do pointer math to a name -- that wouldn''t make sense would it?
Ok, so that doesn''t really help -- it didn''t help me too much either until i thought about it. A pointer is number that is used to address memory. A pointer allows for nasty things such as pointer arithmetic, which allows us to write all over memory. A reference does not allow this, which is a good thing, protection.
Now, to answer your original question: Why return a pointer in the first place? Well assume we have the following function:
char *strcat (char *dest, char *source);
and we want to cat monkey onto it twice. We could do:
char dest[256];
char src[] = "monkey";
strcpy (dest, "bashful");
strcat (dest, src);
strcat (dest, src);
or we could do:
char dest[256];
char src[] = "monkey";
strcpy (dest, "bashful");
strcat (strcat (dest, src), src);
I hope you can see why this works. But you are prolly thinking, ok dmgoober that makes sense but its stupid. Ok i agree it is.
Take a more realistic example but you still probably will never do is:
CMonkey *GiveMeXMonkeys (int n)
{
return new CMonkey[n];
}
You probably never want to do this, as you now have no knowledge of where these new CMonkey''s are going to be stored. Take for example:
int foo ()
{
CMonkey *m = GiveMeMonkeys(3);
printf ("hi!");
}
whenever foo is called more monkeys are allocated, but never deleted (in general, you want to allocate new memory on the same level as it is going to be deleted or else you might forget to delete the memory and get memory leaks!)
In general, you would only want a function to return a pointer to data THAT HAS ALREADY BEEN ALLOCATED BY THE CALLER. A final example that actually makes sense is:
char *findPeriod (char *p)
{
while (*p != NULL)
{
if (*p == ''.'')
return p;
p++;
}
return NULL;
}
this returns a pointer to the first period (or null if the period is not in the string.) Notice findPeriod is returning a pointer to what it assumes is valid memory of the caller (it assumes it is getting a string, and that everything in that string up to the NULL is valid memory.) This keeps with the idea that a function should only return a pointer to memory allocated by the caller.
char sentence[] = "hello.mark"
char *m = findPeriod (sentence);
*m = ''!'';
replaces the period with an exclamation mark.
Hope that helps.
DmGoober
Alexander "DmGoober" Jhinalexjh@online.microsoft.com[Warning! This email account is not attended. All comments are the opinions of an individual employee and are not representative of Microsoft Corporation.]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement