473,804 Members | 4,223 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can you delete a pointer without newing it up?

Hi all,
If I don't new up a pointer, can I delete it?
see code below:

void A::foo()
{
char* buffer;
delete buffer;
return;
}
regards,
Vynce

May 31 '06 #1
11 2370
asimorio wrote:
Hi all,
If I don't new up a pointer, can I delete it?
Well, you can, but the behavior will be undefined unless the pointer was a
null pointer. In that case, nothing is done.
see code below:

void A::foo()
{
char* buffer;
delete buffer;
return;
}


May 31 '06 #2
asimorio wrote:
Hi all,
If I don't new up a pointer, can I delete it?
see code below:

void A::foo()
{
char* buffer;
delete buffer;


You can try, but your toilet might explode.

--
Ian Collins.
May 31 '06 #3
asimorio wrote:
Hi all,
If I don't new up a pointer, can I delete it?
see code below:

void A::foo()
{
char* buffer;
delete buffer;
return;
}


No you can't, the behaviour of your

delete buffer;

is undefined.

What exactly is the purpose of that code?
May 31 '06 #4
Now lets think about it. When you instantiate a pointer variable, you
obviously want to point it to some defined memory location. Since you
did not new-up or NULL-up a memory location, the pointer basically has
an undefined property. When you delete this pointer variable, which has
an undefined property, you are basically telling the compiler to return
the pointed to location ??? to the heap stack. Does this even make
sense? No!

asimorio wrote:
Hi all,
If I don't new up a pointer, can I delete it?
see code below:

void A::foo()
{
char* buffer;
delete buffer;
return;
}
regards,
Vynce


May 31 '06 #5
Well, now i modify a bit my code:
Will it make sense?

void A::Foo(void* a)
{
char* buffer = (char*) a;
delete buffer;
return;
}
regards,
Vynce

May 31 '06 #6
ur***********@h otmail.com wrote:
Now lets think about it. When you instantiate a pointer variable, you
obviously want to point it to some defined memory location. Since you
did not new-up or NULL-up a memory location, the pointer basically has
an undefined property. When you delete this pointer variable, which has
an undefined property, you are basically telling the compiler to return
the pointed to location ??? to the heap stack. Does this even make
sense? No!


That reminds me of the Chewbacca defense :-)

May 31 '06 #7
asimorio wrote:
Well, now i modify a bit my code:
Will it make sense?

void A::Foo(void* a)
{
char* buffer = (char*) a;
delete buffer;
return;
}


Yes, as long as A::Foo is only called with a pointer that points to a single
char allocated with new.

May 31 '06 #8
asimorio wrote:
Well, now i modify a bit my code:
Will it make sense?

void A::Foo(void* a)
{
char* buffer = (char*) a;
delete buffer;
return;
}

Even if your toilet doesn't explode, you program will surely crash. You
are telling delete to free memory that doesn't belong to the heap.

--
Ian Collins.
May 31 '06 #9
Ian Collins wrote:
asimorio wrote:
Well, now i modify a bit my code:
Will it make sense?

void A::Foo(void* a)
{
char* buffer = (char*) a;
delete buffer;
return;
}


Even if your toilet doesn't explode, you program will surely crash. You
are telling delete to free memory that doesn't belong to the heap.

Oops, didn't see the pointer passed in as a parameter.

--
Ian Collins.
May 31 '06 #10

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

Similar topics

19
2695
by: Sandeep Grover | last post by:
Hi, If I do another delete on an object which has been deleted earlier, then how is the system expected to behave ? Is it an unpredictable behavior ?? Thanks Sandeep
1
2614
by: Pelle | last post by:
Hello all, I have to admit, that the idea that occurred to me recently is weird. It was somehow inspired by the huge response on the "delete operator" thread, but goes into a somewhat different direction. From my understanding there are 2 "ways" of handling memory: -- one (from C) is the malloc/realloc/free set of API functions -- the other one (from C++) is the new/new/delete/delete set of API functions.
11
23059
by: Squid Seven | last post by:
I create a pointer to an item: CardSession *cardSession; Then, later, I use new to create an instance of the item and assign it to that pointer: cardSession = new CardSession(); In another function, I want to test if an object is assigned to that
30
3759
by: jimjim | last post by:
Hello, This is a simple question for you all, I guess . int main(){ double *g= new double; *g = 9; delete g; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl; *g = 111; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl;
7
16549
by: morz | last post by:
i just search for a while in c++ groups but cannot find good answer. i have code like this: int **ptr; ptr = new char *; ptr = new int(5); ptr = new int(16);
7
1703
by: Ninan | last post by:
I am skipping a couple of steps in betweens, like inserting into a list of ints and extracting from it. But this is the gist of what I am trying to do class A { }; A *a = new A();
4
2233
by: asimorio | last post by:
All, Well, now i modify a bit my code: Will it make sense? void A::Foo(void* a) { char* buffer = (char*) a; delete buffer; return; }
17
50543
by: Nick Keighley | last post by:
Hi, I have a map containing pointers. When I destroy the map I want to delete all the pointers. typedef std::map<std::string, const T*Table; void destroy_map () {
19
10765
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is there an existing adapter? Thanks, Daniel.
0
9706
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
10332
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10077
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
9150
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...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5521
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...
1
4299
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 we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
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.