473,387 Members | 1,493 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.

Can you delete a pointer without newing it up? (modified version)

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;
}
regards,
Vynce

May 31 '06 #1
4 2209

"asimorio" <th******@gmail.com> wrote in message

| All,
| Well, now i modify a bit my code:
| Will it make sense?

It will only be legal if the memory pointed to by a has been obtained by a
call to new operator. Also if the pointer is NULL, then delete has no
effects.

Sharad

| void A::Foo(void* a)
| {
| char* buffer = (char*) a;
| delete buffer;
| return;
| }
May 31 '06 #2

"asimorio" <th******@gmail.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
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;
}


It depends if a was allocated with new or not. If a was allocated with new,
it is legal. If it was not allocated with new, it is undefined and will
most likely cause a memory fault.
May 31 '06 #3
asimorio posted:
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;
}

There are two sole conditions under which that would work:

int main()
{
A::Foo( new char );

A::Foo( 0 );
}
-Tomás
May 31 '06 #4
asimorio wrote:
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;
}


What are actually trying to do? What problem are you solving? What you
propose is rarely a good idea.


Brian
May 31 '06 #5

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

Similar topics

2
by: foo | last post by:
I'm creating a debug class called debug_mem_allocation for the purpose of finding memory leaks. I used macro's to replace the new and delete operators. My problem is with trying to replace the...
11
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...
30
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 =...
28
by: Wonder | last post by:
Hello, I'm confused by the pointer definition such as int *(p); It seems if the parenthesis close p, it defines only 3 integers. The star is just useless. It can be showed by my program: ...
7
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();
3
by: Ramki | last post by:
How does free() , delete() works? If we allocate memory using the following statement int *p = malloc(100*sizeof(int)); Inorder to release the memory we will call '"free(p) " How does...
11
by: asimorio | last post by:
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; }
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
17
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 () {
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.