943,808 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 366
  • C++ RSS
Jan 30th, 2009
0

Question about the following code.

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. const char *getName()
  2. { return name; }

Looking at the following code which is a member function of a class, why would one pass the array of a person's name as a pointer and lock it so it cannot be modified when they can just pass a copy that won't affect the original? Is it because it takes less CPU?

Thanks for the help...
Similar Threads
Reputation Points: 48
Solved Threads: 0
Newbie Poster
rosenberg_a is offline Offline
17 posts
since Jan 2009
Jan 30th, 2009
0

Re: Question about the following code.

You might want to try something like this, but yes it uses more CPU and memory.

C++ Syntax (Toggle Plain Text)
  1. #define YOUR_ARRAY_SIZE 256
  2. #include <string.h>
  3.  
  4. char * getName(){
  5. char buf[YOUR_ARRAY_SIZE];
  6. sprintf(buf,name);
  7. return &buf[0];
  8. }
Reputation Points: 34
Solved Threads: 7
Junior Poster in Training
venomxxl is offline Offline
72 posts
since Jan 2009
Jan 30th, 2009
0

Re: Question about the following code.

Yes, it's more efficient to just pass the pointer.

It adds security if that pointer is const. Thus, user of the class cannot modify the array member - preventing array overruns.

If it's needed to be able to modify the name without using any class members, then the user can copy that name to an array or string they allocated, and be able to safely modify that.

Consider the string type. This code results in a compiler error
C++ Syntax (Toggle Plain Text)
  1. string s = "Hello, World";
  2. s.c_str( )[0] = 'h';
While this would work
C++ Syntax (Toggle Plain Text)
  1. string s = "Hello, World";
  2. char str[100];
  3. strcpy( str, s.c_str( ) );
  4. str[0] = 'h';
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Jan 30th, 2009
2

Re: Question about the following code.

Please be VERY CAREFUL with this code.

Consider
c++ Syntax (Toggle Plain Text)
  1. // THIS IS VERY VERY WRONG!!!!
  2. char * getName(){
  3. char buf[YOUR_ARRAY_SIZE];
  4. sprintf(buf,name);
  5. return &buf[0];
  6. }

The code above is a runtime error, the problem is the you have a local variable (buf) and that goes out of scope at the end of the function. BUT you are passing the pointer to that memory, which you expect to use!!! It is possible/likely that sometime later the program will use that space for something else. Then you will have corrupted the string you are looking at, or even worst you might actually change it!!!!

The orginal is correct, if you want a copy (to manipulate) then make a local copy out of the methods. If you want to change a buffer in the class use a class method.

Sorry for the rant but this kind of error, is normally (a) Very difficult to debug, (b) dormant for a long while so it ends up in customers code.
Last edited by StuXYZ; Jan 30th, 2009 at 5:14 pm.
Reputation Points: 732
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
659 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Celsius to Farenheit Converter for a newbie !
Next Thread in C++ Forum Timeline: Swap example using pointers and passing by reference





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC