| | |
Question about the following code.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2009
Posts: 17
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
const char *getName() { 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...
You might want to try something like this, but yes it uses more CPU and memory.
C++ Syntax (Toggle Plain Text)
#define YOUR_ARRAY_SIZE 256 #include <string.h> char * getName(){ char buf[YOUR_ARRAY_SIZE]; sprintf(buf,name); return &buf[0]; }
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
While this would work
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)
string s = "Hello, World"; s.c_str( )[0] = 'h';
C++ Syntax (Toggle Plain Text)
string s = "Hello, World"; char str[100]; strcpy( str, s.c_str( ) ); 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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Nov 2008
Posts: 392
Reputation:
Solved Threads: 72
Please be VERY CAREFUL with this code.
Consider
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.
Consider
c++ Syntax (Toggle Plain Text)
// THIS IS VERY VERY WRONG!!!! char * getName(){ char buf[YOUR_ARRAY_SIZE]; sprintf(buf,name); return &buf[0]; }
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
![]() |
Similar Threads
- Rich Text Box Code (HTML and CSS)
- What is wrong with this code? (C++)
- Question on my code in tower of hanoi (Assembly)
- mock test question, can anyone help (C)
- String literial question (PHP)
- Simple Counter Code (ASP)
- I just found this code for PAC MAN...what does this mean??? (Java)
- generate random question (C)
- find out the error in my code (Java)
Other Threads in the C++ Forum
- Previous Thread: Celsius to Farenheit Converter for a newbie !
- Next Thread: Swap example using pointers and passing by reference
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






