473,387 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

delete pointers: why is this working!!??

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()
{
string * j = new string("this prog should dump core!");
printf("\n%s\n",j->c_str());
delete j;
delete j;
}

Jul 23 '05 #1
11 2254
* mufasa:
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()
{
string * j = new string("this prog should dump core!");
printf("\n%s\n",j->c_str());
delete j;
delete j;
}


It's Undefined Behavior, which means that any result whatsoever is
acceptable as far as the standard is concerned.

General advice as you're learning the language:

1) Don't use raw pointers.
2) Don't use raw pointers.
3) Don't ... You get the idea.

Also, consider using C++ iostreams, which are more type-safe than C printf.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
mufasa wrote:
i believe deleting an already delete pointer should give me a
segmentation fault!
No, it should give you undefined behavior.
Why, then, is the following working?
Because that is one instance of "undefined behavior".
I am using gcc version 3.0.2.

#include<stdio.h>
#include <string>
using namespace std;

int main()
{
string * j = new string("this prog should dump core!");
printf("\n%s\n",j->c_str());
delete j;
delete j;
}


Jul 23 '05 #3
the std::string class might've used a reference counting allocation for the
actually string content. The reference counting rule can be "delete the
content if reference count drops to zero; if the reference count is
negative, don't do any deletion".

you should refer to the actually implementation to see what is going on
there.

don't do double deletion, that's all.

"mufasa" <so*****@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
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()
{
string * j = new string("this prog should dump core!");
printf("\n%s\n",j->c_str());
delete j;
delete j;
}

Jul 23 '05 #4
[top posting fixed]

Kehuei Huang wrote:

"mufasa" <so*****@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
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()
{
string * j = new string("this prog should dump core!");
printf("\n%s\n",j->c_str());
delete j;
delete j;
}

the std::string class might've used a reference counting allocation

for the actually string content. The reference counting rule can be "delete the
content if reference count drops to zero; if the reference count is
negative, don't do any deletion".
I don't see how your /proposed/ reference counting avoid undefined
behavior, when you are deleting the pointer j twice.

you should refer to the actually implementation to see what is going on
there.

don't do double deletion, that's all.


Krishanu
Jul 23 '05 #5

Krishanu Debnath wrote:
[top posting fixed]

Kehuei Huang wrote:

"mufasa" <so*****@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
i believe deleting an already delete pointer should give me a
segmentation fault! Why, then, is the following working? I am using gccversion 3.0.2.

#include<stdio.h>
#include <string>
using namespace std;

int main()
{
string * j = new string("this prog should dump core!");
printf("\n%s\n",j->c_str());
delete j;
delete j;
}

> the std::string class might've used a reference counting allocation for the
> actually string content. The reference counting rule can be

"delete the > content if reference count drops to zero; if the reference count is > negative, don't do any deletion".


I don't see how your /proposed/ reference counting avoid undefined
behavior, when you are deleting the pointer j twice.
>
> you should refer to the actually implementation to see what is going on > there.
>
> don't do double deletion, that's all.


Krishanu


Jul 23 '05 #6
Hi

Sorry about the previous "empty" message.

I'm using glibc-2.3.4 with gcc-3.3.5.
When I do delete twice my program aborts:
"*** glibc detected *** double free or corruption (fasttop): 0x08049db0
***
Aborted"

I didn't expect such a descriptive error message.

Anyway, new and delete are merely mechanisms of telling the system,
which sections of memory you're using.
After calling the delete operator on a pointer, the memory pointed to
becomes available to the system again. Until the system reasigns that
memory you'll be able to "access" it.
ie:
int& a = new int;
a = 5;
cout << a; // "5"
delete &a;
cout << a; // "5"
.... // some time later
cout << a; // whatever's in memory at that time.
It is posible that having the two delete statements next to each other,
would allow your program to run without any problems.

Remember new and delete are class (static) operators not instance
operators.
It is posible to call them with out having any valid instances.

Also if you have to use raw pointer, set them to null after calling the
delete, then you'll be able to tell if it's safe to use.
IE:
SomeClass* sc = new SomeClass();
.... // some time later
if (sc) // true
{
sc->someFunction();
}
.... // some time later
if (sc) // true
{
delete sc;
sc = 0;
}
.... // some time later
if (sc) // false
{
sc->someFunction();
}

Joe

Jul 23 '05 #7
How about this code? Is the delete statement here trying to break some
ownership rules (I think that's the case .. still verifying)?
Because this segment is dumping core as well!

#include<stdio.h>
#include <string>
using namespace std;

int main()
{
string someString("abc");
char * c= new char[someString.length()];

c = const_cast<char*>(someString.c_str());

printf("\n%s\n",c);
delete [] c;
}

Jul 23 '05 #8
jo*****@dariel.co.za wrote:
Hi

Sorry about the previous "empty" message.
Post with proper context/quote is much appreciated.

I'm using glibc-2.3.4 with gcc-3.3.5.
[all compiler/implementation specific details snipped]
After calling the delete operator on a pointer, the memory pointed to
becomes available to the system again. Until the system reasigns that
memory you'll be able to "access" it.
ie:
int& a = new int;
[invalid c++ code fragments snipped]


It is posible that having the two delete statements next to each other,
would allow your program to run without any problems.


After invoking UB, it can run without any problems, it may also order
a 'peppy paneer pizza' from Pizza Hut for you. Best of luck !

Krishanu
Jul 23 '05 #9
mufasa wrote:
How about this code? Is the delete statement here trying to break some
ownership rules (I think that's the case .. still verifying)?
Because this segment is dumping core as well!

#include<stdio.h>
#include <string>
using namespace std;

int main()
{
string someString("abc");
char * c= new char[someString.length()];
Here, you allocate an array of 3 char and assign its starting address to c.
c = const_cast<char*>(someString.c_str());
Here, you overwrite c, losing the only pointer you had to your dynamically
allocated array. This is a memory leak. Also, the const_cast is a *very*
bad idea, as are almost all const_casts.
printf("\n%s\n",c);
delete [] c;
Here, you try to delete the array that was returned by c_str(), so the
answer to your question is yes, you break an ownership rule. You don't even
know whether the pointer returned by c_str() actually points to dynamically
allocated memory.
}


Jul 23 '05 #10
Am I not trying to break any ownership rules herein (see below) :

#include <string>
#include<iostream.h>

using namespace std;

int main()
{
char * a = new char('a');
cout << *a <<endl; //prints 'a'
char * c = new char();
c = a;
delete c;
cout << *a <<endl; //prints '0'. which means that 'delete c'
//did delete the contents
owned by a! right?
delete a;
delete a; //why does this double delete
work!
}

Jul 23 '05 #11
mufasa wrote:
Am I not trying to break any ownership rules herein (see below) :
There is no real ownership semantic here. You're allocating memory yourself,
and you're deallocating it yourself.
#include <string>
#include<iostream.h>

using namespace std;

int main()
{
char * a = new char('a');
cout << *a <<endl; //prints 'a'
char * c = new char();
c = a;
Like in the previous example, you loose the allocated memory here. Note that
c is only a pointer, just as a. "c = a" means that you let c point to the
very same memory location that a was (and still is) pointing to (the one
you allocated previously with "new char('a')"). You didn't do anything to
the memory that you allocated with "new char()", you only let the pointer c
point somewhere else. That memory is still there, but you don't have any
pointer pointing to it. So that memory is lost, unaccessable, undeletable.
delete c;
This frees the memory allocated with "new char('a')" that both a and c are
pointing to.
cout << *a <<endl; //prints '0'. which means that 'delete c'
//did delete the contents
This invokes undefined behavior (anything can happen, from printing '0' to a
system crash). You're not allowed to dereference a pointer that points to
memory that was already freed.
owned by a! right?
delete a;
delete a; //why does this double delete
work!
}


Again, undefined behavior. It might "work" (i.e. not crash), but it could
also crash or do anything else. The C++ standard doesn't give you any
guarantee about what happens. It might be system or compiler specific or
even depend on the moon phase.

Jul 23 '05 #12

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

Similar topics

5
by: Andrew DeFaria | last post by:
I created the following .sql file to demonstrate a problem I'm having. According to the manual: If |ON DELETE CASCADE| is specified, and a row in the parent table is deleted, then InnoDB...
1
by: Sunshine | last post by:
Pretty new to javascript so please help. Why isn't this working in my validateform()? if (document.This.strOld.value.compareTo(Session("strPassword")) != 0) { alert("Current password...
0
by: Nedu N | last post by:
Hi Experts, All of sudden the delete confirmation is not working for me on my datagrid (it was working 2 days ago..couldn't figure out the cause). I am trying to add an 'onsubmit' attribute to...
4
by: Pipo | last post by:
The problem: I create a very simple custom control: public class cLabel : System.Web.UI.WebControls.Label { } I place the cLabel in a user control. When I place the user control on a page...
9
by: pavan734 | last post by:
Hello, I have a array of double pointers as shown below dummy_class **ptr ; //assume dummy class to be some c++ class I have allocated it like this: for(int i = 0 ; i<10 ; i++) { ptr = new...
7
by: Anil Gupte | last post by:
Private Sub mnu2Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnu2Exit.Click Dim fDir As String = Path.GetDirectoryName(L3Global.VideoFileName) ...
8
by: TBass | last post by:
Hi, Switching some code from C to C++, and I have a destructor question: In the C code, when the program wants to close and free all memory, I have them call a Close function. The linked-lists...
4
by: wangers16 | last post by:
I have the following code in my website and it is supposed to delete a user account along with all associated records, however it doesn't delete any of the records, the function is working, beacuse I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.