protecting my char*

P

pembed2003

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!
 
R

Rolf Magnus

pembed2003 said:
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.
 
S

Stephen Waits

pembed2003 said:
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
 
P

pembed2003

Rolf Magnus said:
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!
 
P

pembed2003

Stephen Waits said:
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.
 
J

John Harrison

pembed2003 said:
Rolf Magnus <[email protected]> wrote in message

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
 
J

John Harrison

pembed2003 said:
Stephen Waits <[email protected]> wrote in message

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
 
S

Stephen Waits

John said:
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
 
J

John Harrison

Stephen Waits said:
John said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top