Skip to main content
gamedev.net
🔒 Locked

String Problem

Started by Database 6 replies 450+ views
Original Post
Database
Database
Hi all, I have a problem. The code: MyString str3 = str2; causes an error when the application closes, namely when str2 is destructed and I try to free the memory. I've tracked this to the fact that the string memory that I've allocated with my MyString implementation has already been released when str3 goes out of scope. The above operation does not call the operator= or either of the constructors specified. The only way I've managed to get this to work is by changed the test to be: CODE MyString str3; str3 = str2; Any comments/suggestions gratefully received! Thanks in advance
Anon Mike
Anon Mike
You need to show the code for MyString.

I'm guessing you didn't implement a copy constructor (i.e. MyString(const MyString &) {...}) and so in the first case you are getting a binary copy.
-Mike
pulpfist
pulpfist
It sounds like you have a MyString constructor that allocate some default memory for the string, while the operator = does not. Is that right?
You should probably post your code on this one...
DeadXorAlive
DeadXorAlive
A copy constructor is used to initialize like this. If you haven't defined a copy constructor, in the code you posted str3 will be initialized with a memberwise (all members are copied by value) copy of str2, which is probably not what you want.
Database
Database
Thanks for the help so far. Here is the code:

###################################################################
t3q4.h

#ifndef _t3q4_h_
#define _t3q4_h_

//
// include files
//
#include <iostream>
#include <cstring>
#include <cstdio>

//
//
class MyString
{
public:
MyString();
virtual ~MyString();
};

#endif


########################################################

t3q4.cpp

#include "t3q4.h"

//
// using directive
//
using namespace std;

//
// Main program entry point
//
int main(int argc,char **argv)
{

//
// should word
//

string str;
str.print(); //this one should print a blank string

string str2("Test String");
str2.print();

string str3 = str2;
str3.print();

string str4("Second String");
str4.print();
string str5(" This Shall Be Added");
str5.print();


strcat() function

str4.append(str5);
str4.print();


//return from main
return 0;
}


I hope ya'all can help me.
Thanks in advance
RDragon1
RDragon1
Huh? What's "string" ? It doesn't look like MyString does much of anything, nor have you tried to use it anywhere.

And why are you trying to implement a string anyway? What do you need that can't be done with std::string?
rip-off
rip-off
Assuming c++, the problem your are having is generally related with not using std::string.

However, I must note that its admirable that you have your own string class, as opposed to using raw char arrays.

Throw this out the window if you are building a string class as a learning experience...

EDIT: I didn't get to read your second post...

Since you have not posted your full string code (otherwise how could your project compile at all ), I cannot say why it is failing...

EDIT EDIT: t3q4.h/ t3q4.cpp ? What kind of names are those? Maybe I'm a little suspicious seeing as how this could potentially be expanded to t(est?)3q(uestion?)4
Database
Database
Quote:
Original post by rip-off

EDIT EDIT: t3q4.h/ t3q4.cpp ? What kind of names are those? Maybe I'm a little suspicious seeing as how this could potentially be expanded to t(est?)3q(uestion?)4



;-)

No, since I am German, I give my progs german abbreviations.

t = Test
q = Quelltext (or in English: Sourcecode :) )



But still, thanks a lot

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.