home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!kentrox!peter
- From: peter@kentrox.uucp (Peter Uchytil)
- Subject: can I 'delete' without a 'new'?
- Message-ID: <1992Sep4.171020.4762@kentrox.uucp>
- Organization: ADC Kentrox, Portland OR
- Date: Fri, 4 Sep 1992 17:10:20 GMT
- Lines: 54
-
- I can't seem to find any references which indicate whether I can do this
- or not, so I'm appealing to the net.wisdom. Here's what I've got:
-
- class foo {
- char *String;
- public:
- foo();
- foo( char * );
- ~foo();
- void set( char * );
- };
-
- foo:foo() { };
-
- foo:foo( char *inString ) {
- String = new char[strlen( inString )];
- strcpy( String, inString );
- }
-
- void foo:set( char *inString ) {
- String = new char[strlen( inString )];
- strcpy( String, inString );
- }
-
- foo:~foo() {
- delete [] String;
- }
-
- The question is what will happen if I create a foo with the empty constructor
- and never call 'set'? Since 'new' has never been called, what will 'delete'
- try to delete? Looks like a real good way to frazzle the system. Yes? No?
-
- The reason I want to do this is I want to declare an array of 'foo'. To do
- the assignment:
- LotsaFoos foo[100];
- I have to use an empty constructor.
-
- One idea I had was to have the empty constructor do something like:
- String = new char[1];
-
- But then does 'set' first have to do a 'delete' before doing another 'new'?
- I would assume it does.
-
- Summary of questions:
- 1) What happens if I call 'delete' without having called 'new'?
- 2) What happens if I call 'new' twice in a row? Sounds like I'm going
- to make some memory become unrecoverable.
-
- Thanks for any comments!
-
- Pete
- --
- Peter Uchytil | ADC Kentrox Industries | "They say that I'm a dreamer
- peter@kentrox.com | Portland, Oregon | And I tell them they are right"
-