• Advertisement

Archived

This topic is now archived and is closed to further replies.

Pointers loosing their way?

This topic is 6349 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is driving me crazy. My pointers seems to be loosing their memory references. I whipped up a small demo to explain whats going wrong: #include class TestClass { public: void SetName() { char *name = new char[10]; name="hello"; } const char *GetName() { return itsname; } private: char *itsname; }; void TestFunction(TestClass *Data) { Data->SetName(); } void main(void) { TestClass *data = new TestClass; TestFunction(data); cout << data->GetName(); } Any help will be appreciated... Chris

Share this post


Link to post
Share on other sites
Advertisement
itsname is not being set anywhere in the class functions, instead you are creating name and storing hello in it, but when you call GetName, it returns itsname, not name as the string.

instead:

class TestClass
{
private:
char *itsname;

public:
TestClass()
{
itsname = new char[128];
}

~TestClass()
{
if(itsname != NULL)
delete itsname;
}

void SetName(const char *name)
{
if(itsname != NULL)
strcpy(itsname, name);
}

const char *GetName()
{
return itsname;
}
};

void main(void)
{
TestClass *data = new TestClass;
data->SetName("Hello");
cout << data->GetName();
}



Jim Adams

Share this post


Link to post
Share on other sites
Jim is correct on this, I just want to clarify something.

void SetName() {
char *name = new char[10];
name="hello";
}

This does not do *at all* what you think it does. Name is a pointer, thus wherever you say "name" without derefrencing it (*name, name[2]) you get the address returned by new above. "hello" is a string literal, in your code it looks just like an address that just happens to point to static data containing "hello". name="hello" results in name now pointing to that static data. This is sometimes called a "shallow copy". name[6]='?';name[7]='\0'; could do very unplesant things, even though it looks like it would be perfectly fine because you have allocated 10 characters. Note also, you are leaking memory but thats the least of your problems. Personally I think that you should be reqired to cast to assign "literal" to a non-const char *

Jim's version also allows buffer overflows, but it's faster and is just an example.
    
class quickstr {
char *str;
char static_data[128];
size_t size;
public:
quickstr() : str(static_data), size(sizeof(static_data)) {
str[0] = '\0';
}
quickstr(size_t ssize) : str(new char[ssize]), size(ssize) {
str[0] = '\0';
}
~quickstr() {
if(str!=static_data) delete [] str;
}
void set_trunc(const char *s) {
strncpy(str,s,size);
}
void set_grow(const char *s) {
size_t newlen = strlen(s);
if(newlen > size) {
size = newlen+1;
if(str != static_data) delete [] str;
str = new char[size];
}
set_trunc(s);
}
const char * c_str() {return str;}
};

This hides a lot of nastyness, does not allocate memory in the general case and gives you the choice of speed and limited strings, or flexability. With default construction it even beats stl::string
Each object also takes up 136 bytes, but tanstaafl.


Edited by - Grib on September 3, 2000 12:25:37 PM

Share this post


Link to post
Share on other sites

  • Advertisement