Question about the following code.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 17
Reputation: rosenberg_a is an unknown quantity at this point 
Solved Threads: 0
rosenberg_a rosenberg_a is offline Offline
Newbie Poster

Question about the following code.

 
0
  #1
Jan 30th, 2009
  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...
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 16
Reputation: venomxxl is an unknown quantity at this point 
Solved Threads: 2
venomxxl's Avatar
venomxxl venomxxl is offline Offline
Newbie Poster

Re: Question about the following code.

 
0
  #2
Jan 30th, 2009
You might want to try something like this, but yes it uses more CPU and memory.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,678
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Question about the following code.

 
0
  #3
Jan 30th, 2009
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
  1. string s = "Hello, World";
  2. s.c_str( )[0] = 'h';
While this would work
  1. string s = "Hello, World";
  2. char str[100];
  3. strcpy( str, s.c_str( ) );
  4. str[0] = 'h';
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Question about the following code.

 
2
  #4
Jan 30th, 2009
Please be VERY CAREFUL with this code.

Consider
  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.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC