Mail Archives: djgpp/1994/01/17/04:15:21
Hi Mathew,
On Sat, 15 Jan 1994, Matthew Moss wrote:
> I have the following class declarations (among others) for a set of execution
> commands:
>
> //////////////////////////////////////////////////
> class Command
> {
> classes _cl;
> char* _name;
0------>^^^^^^^^^^^^^^^^^^^^^^
>
> protected:
> Command(classes cl,char* name)
> {
> _cl = cl;
> strcpy(_name,name);
1-------------->^^^^^^^^^^^^^^^^^^^
> }
> public:
> virtual int Perform() = 0;
> virtual char* isA() { return "Command"; }
> };
>
> class C_create:public Command
> {
> public:
> C_create(classes cl,char* name) : Command(cl,name) {}
> virtual char* isA() { return "C_create"; }
> virtual int Perform() {}
> };
> ///////////////////////////////////////////////////
>
> Now, later on I have the following code
>
> printf("About to create C_create:\n");
> temp = new C_create(cl,arg[1]);
2------>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> printf("Created C_create successfully!\n");
>
Your mistake is, that you use strcpy without allocating memory for _name.
The function strcpy doesn't allocate or check if there is enough memory.
It simply copies.
Try to insert:
_name= new char[strlen(name)];
in front of the strcpy line (marked with "1")
Greetings
Chris
- Raw text -