473,508 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

delete char * - why does it work

interesting case:

class MyString{
char * strRep; // initialized to char array

public:
~MyString(){delete strRep;} //why would this work
// just like 'delete [] strRep;'
};
isn't delete first calls the destructor for the object and then
deallocates the memory (by operator delete)?

Jul 23 '05 #1
8 5972
puzzlecracker wrote:
interesting case:

class MyString{
char * strRep; // initialized to char array

public:
~MyString(){delete strRep;} //why would this work
// just like 'delete [] strRep;'


It doesn't if strRep was allocated with new[]. It does if strRep was
allocated with new.

char *c = new char[10];
delete c; // error

char *c = new char;
delete c; // ok

That's undefined behavior. On my system, mismatched new and delete
crashes almost everytime.
Jonatha

Jul 23 '05 #2
let's say it is allocated as char *c ="dummy";

IT WILL WORK!

Jul 23 '05 #3
puzzlecracker wrote:
let's say it is allocated as char *c ="dummy";

IT WILL WORK!


Please quote what you are answering to next time.

Depends on what you mean by 'it will work'. If you intend to crash your
system, it may or may not work. If you intend to run into undefined
behavior, it will work. If you intend to have a well-behaving program,
it won't. Deleting something you haven't allocated yourself is illegal.
Jonathan

Jul 23 '05 #4
"puzzlecracker" <ir*********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
let's say it is allocated as char *c ="dummy";

IT WILL WORK!

It is undefined behaviour, looking as if it worked.
--
jb

(reply address in rot13, unscramble first)
Jul 23 '05 #5
the behavior is undefined - but why does it work on all compilers?????

Jul 23 '05 #6
Hi

puzzlecracker wrote:
the behavior is undefined - but why does it work on all compilers?????


Come on... don't answer your own questions.
Markus

Jul 23 '05 #7
"puzzlecracker" <ir*********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com
the behavior is undefined - but why does it work on all compilers?????


I doubt that you have tested all compilers.

You really need to get over this nonsense. Undefined means that anything can
happen. Included in the "anything" is that it might work as you expect.
There is no explanation in standard C++ as to why something with undefined
behaviour might "work". That all depends on the details of compiler
implementations etc. If you want to know, then go ask the authors of the
compiler.

If you write correct code, you are entitled to expect it to work. If you
write code that invokes undefined behaviour, you have no right to expect
anything. The fact that code with undefined behaviour sometimes works in the
way expected by the programmers who write it is neither new nor interesting
information. Programmers who rely on such "good fortune" are fools.

--
John Carson

Jul 23 '05 #8
"puzzlecracker" <ir*********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
the behavior is undefined - but why does it work on all compilers?????

It does not. Proof? Try VS 7.1 on Win XP SP2 with:

class MyString
{
char* strRep;

public:
MyString (char* t) : strRep (t)
{
}

~MyString ()
{
delete strRep;
}
};

MyString s = "test";
--
jb

(reply address in rot13, unscramble first)
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5213
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
20
4116
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
11
2259
by: mufasa | last post by:
i believe deleting an already delete pointer should give me a segmentation fault! Why, then, is the following working? I am using gcc version 3.0.2. #include<stdio.h> #include <string> using...
4
4839
by: Alex Vinokur | last post by:
Should 'delete below (after casting to void*)' work fine? ------------------------------------------ char* p1 = new (nothrow) char; if (p1 == 0) return; void* p2 = reinterpret_cast<void*> (p1);...
7
7006
by: AB | last post by:
Hi all, A thought crossed my mind.... if I allocate memory for an array at runtime using.... int* arr = new int ; what happens when I then de-allocate memory using
12
3350
by: yufufi | last post by:
Hello, How does delete know how much memory to deallocate from the given pointer? AFAIK this informations is put there by new. new puts the size of the allocated memory before the just before...
5
1753
by: michael | last post by:
Hi All, I have the following: const int LENGTH = 5; void limitNameLength(string inText, char *&outText, int outLength){ if(static_cast<int>(inText.length()) outLength){ inText =...
15
4991
by: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision -...
23
1914
by: Martin T. | last post by:
Hi all! char* p = new char; // POD! .... delete p; // std compliant delete p; // will work on VC8 free(p); // will also work on VC8 I am interested on which platforms/compilers the...
0
7124
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7498
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3195
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1558
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.