Get help on this challenge

Debugging14


Please read this explanation before attempting debugging challenges.


When I run this code in Windows I have no problem, but on my Linux box it breaks. What's wrong?

#include <iostream>
#include <cstring>
using namespace std;

class MyLittleStringBox
{
private:
	int size;
	char *stringBox;
public:
	MyLittleStringBox(unsigned int size)
	{
		stringBox=new char[size];
		this->size=size;
	}

	~MyLittleStringBox()
	{
		delete[] stringBox;
	}
	
	char *getString()
	{
		return stringBox;
	}
	
	int getSize()
	{
		return size;
	}
	
	void setString(char const *lilString, int size)
	{
		if(size<=this->size)
		{
			memset(stringBox, 0, this->size);
			strncpy(stringBox, lilString, size);
		}
		return;
	}
};

void printMyLittleString(MyLittleStringBox adorableString)
{
	int i;
	char *cuteString=adorableString.getString();
	for(i=0;i<adorableString.getSize();i++)
		cout<<cuteString[i];
	cout<<endl;
}

int main()
{
	MyLittleStringBox box(5);
	box.setString("abcd", 4);
	printMyLittleString(box);
	return 0;
}

Downloadable source code can be found here.


You must be logged in to play.



Challenge by COM.