473,395 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

protecting my char*

Hi,
I have the following:

class person{
public:
person(){name = new char[6]; strcpy(name,"peter");}
~person(){delete[] name;}
const char* getName(){return name;}
private:
char* name;
};

int main(int argc,char** argv){
person p;
char* s = (char*)p.getName();
s[0] = 'f';
std::cout<<p.getName()<<std::endl;
}

The above prints:

feter

What I want to do is allow my getName() function to return the name of
person but don't allow the caller to modify the name. Any idea?

I can do:

char* person::getName(){
char* s = new char[strlen(name) + 1];
strpcy(s,name);
return s;
}

But this approach has 2 drawback:

1. Everytime the getName() function is called, a copy of name has to
be made which is slow
2. The caller has to remember to free s

Is ther any other solution?

Thanks!
Jul 22 '05 #1
8 1255
pembed2003 wrote:
Hi,
I have the following:

class person{
public:
person(){name = new char[6]; strcpy(name,"peter");}
~person(){delete[] name;}
const char* getName(){return name;}
private:
char* name;
};

int main(int argc,char** argv){
person p;
char* s = (char*)p.getName();
s[0] = 'f';
std::cout<<p.getName()<<std::endl;
}

The above prints:

feter

What I want to do is allow my getName() function to return the name of
person but don't allow the caller to modify the name. Any idea?
You casted away the constness (using an old-style cast btw). The
compiler can protect you from doing errors, it won't protect you from
sabotaging your code.
I can do:

char* person::getName(){
char* s = new char[strlen(name) + 1];
strpcy(s,name);
return s;
}

But this approach has 2 drawback:

1. Everytime the getName() function is called, a copy of name has to
be made which is slow
2. The caller has to remember to free s

Is ther any other solution?


Yes. Don't cast unless you really know that (and why) you need it, and
if you need it, use the newer C++ style casts.

Jul 22 '05 #2
pembed2003 wrote:
class person{
public:
person(){name = new char[6]; strcpy(name,"peter");}
~person(){delete[] name;}
const char* getName(){return name;}
private:
char* name;
};

[snip]
#include <string>

class person
{
public:
person() { name = "peter"; }
std::string getName() const { return name; }

private:
std::string name;
};
To do it without copying return a const reference instead:
#include <string>

class person
{
public:
person() { name = "peter"; }
const std::string& getName() const { return name; }

private:
std::string name;
}
--Steve
Jul 22 '05 #3
Rolf Magnus <ra******@t-online.de> wrote in message news:<c6*************@news.t-online.com>...

Is ther any other solution?


Yes. Don't cast unless you really know that (and why) you need it, and
if you need it, use the newer C++ style casts.


So, I guess your answer is:

Tell whoever using the code not to cast away the const and hope they
will listen to me?

I think that's a very useful method of stop someone trying to break my
code! Thanks a lot!
Jul 22 '05 #4
Stephen Waits <st***@waits.net> wrote in message news:<40**************@waits.net>...

To do it without copying return a const reference instead:
#include <string>

class person
{
public:
person() { name = "peter"; }
const std::string& getName() const { return name; }

private:
std::string name;
}


Thanks Steve! I will give it a try.
Jul 22 '05 #5

"pembed2003" <pe********@yahoo.com> wrote in message
news:db**************************@posting.google.c om...
Rolf Magnus <ra******@t-online.de> wrote in message news:<c6*************@news.t-online.com>...

Is ther any other solution?


Yes. Don't cast unless you really know that (and why) you need it, and
if you need it, use the newer C++ style casts.


So, I guess your answer is:

Tell whoever using the code not to cast away the const and hope they
will listen to me?

I think that's a very useful method of stop someone trying to break my
code! Thanks a lot!


What else is possible? You cannot stop someone doing this (for instance)

memset(&person, 0, sizeof(person));

or this

char* rogue = (char*)&person;
*rogue = '\0';

or this

#define private public
#include "person.h"

person.name[0] = '\0';

or million other stupid things

C++ does not try to guard against deliberate attempts to break code. If you
want something like that try Java.

john
Jul 22 '05 #6

"pembed2003" <pe********@yahoo.com> wrote in message
news:db**************************@posting.google.c om...
Stephen Waits <st***@waits.net> wrote in message

news:<40**************@waits.net>...

To do it without copying return a const reference instead:
#include <string>

class person
{
public:
person() { name = "peter"; }
const std::string& getName() const { return name; }

private:
std::string name;
}


Thanks Steve! I will give it a try.


It doesn't work, for exactly the same reason as before

person p;
((std::string&)(p.get_name()))[0] = 'f';

At some point you just have to trust your users.

john
Jul 22 '05 #7
John Harrison wrote:
It doesn't work, for exactly the same reason as before


Oh right.. I didn't mean to imply that it would prevent something like this.

But, what prevents anything from writing anywhere.. [obviously some
OS-dependent functionality may be available to help with this].

--Steve
Jul 22 '05 #8

"Stephen Waits" <st***@waits.net> wrote in message
news:40**********************@news.newshosting.com ...
John Harrison wrote:
It doesn't work, for exactly the same reason as before
Oh right.. I didn't mean to imply that it would prevent something like

this.
But, what prevents anything from writing anywhere.. [obviously some
OS-dependent functionality may be available to help with this].


Exactly, the OP seems to be expecting too much from C++.

john
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Yang Li Ke | last post by:
Hi guys! Anyone know a way so that users purchasing my scripts would not be able to share them with other people ? Yang
6
by: nell | last post by:
Hi all, I've developed a testing application in python, and should supply it in a way that no one (lets say they are regular users) will understand it and edit it. The application source is all...
12
by: Roland Hall | last post by:
I read Aaron's article: http://www.aspfaq.com/show.asp?id=2276 re: protecting images from linked to by other sites. There is a link at the bottom of that page that references an interesting...
2
by: Jacky Luk | last post by:
I saw an old thread in this group which addressed the issue of reverse compilation. After some study, I draw a conclusion that "reverse compilation" is completely blocked off in win32 environment...
6
by: Roman Werpachowski | last post by:
In a recent thread http://tinyurl.com/8n7fe I asked about preventing the user from deleting the object pointed to by a pointer/reference. Now I would like to ask about a different aspect of this...
12
by: Dr. Edmund M. Hayes | last post by:
I wrote a access program that works well enough that a handful of people would like to buy it from me. My problem is that if I sell it to someone there is no mechanism that I know of to protect...
22
by: flit | last post by:
Hello All, I have a hard question, every time I look for this answer its get out from the technical domain and goes on in the moral/social domain. First, I live in third world with bad gov., bad...
0
by: xamman | last post by:
hi there! according to msdn (link at bottom) i should be able to protect a whole class declaratively as above. However i keep getting 'request for principal permissions failed' exceptions. in...
2
by: Jeff Williams | last post by:
I am developing an application which will allow users (students) to run applications on PC's with elevated rights. This is necessary for some applications which require Administrator rights on the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.