473,732 Members | 1,991 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(){del ete 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 5990
puzzlecracker wrote:
interesting case:

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

public:
~MyString(){del ete 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
"puzzlecrac ker" <ir*********@gm ail.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.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
"puzzlecrac ker" <ir*********@gm ail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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
"puzzlecrac ker" <ir*********@gm ail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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
5246
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 discussions of new and delete is a pretty damn involved process but I'll try to stick to the main information I'm looking for currently. I've searched around for about the last too weeks and have read up on new and overloading it to some extent but...
20
4150
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
2280
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 namespace std; int main() {
4
4878
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); delete p2; ------------------------------------------ Alex Vinokur
7
7025
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
3372
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 the beginning of the array. But I couldn't find this information by looking at the memory. (Via VS2005 - C++) My second questions is, if there is a mechanism to know how much memory is allocated for the array, why don't we use it for things like...
5
1763
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 = inText.substr(0, outLength); }
15
5023
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 - but for the sake of this post ... I'm searching for an answer that assumes said decision. If I allocate memory in the class declaration: char buffer;
23
1947
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 second two statements
0
8944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8773
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9445
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9234
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9180
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6030
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4548
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.