delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/01/17/04:15:21

Date: Mon, 17 Jan 1994 09:23:23 +0200 (MESZ)
From: Christoph Kuhmuench <fb33 AT rummelplatz DOT uni-mannheim DOT de>
Subject: Re: 'new' runtime error (seg fault)
To: Matthew Moss <mmoss AT panix DOT com>
Cc: DJGPP Mailing List <djgpp AT sun DOT soe DOT clarkson DOT edu>

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019