Deleting a pointer?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Deleting a pointer?

 
0
  #1
Apr 28th, 2005
Hello ladies and gents,

Ive got this example of a program that I tried out wich shows some special possibilities to use new.

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int a[100]= {0};
  8.  
  9. for (int i = 0; i < 100; ++i)
  10. a[i] = 100 * i;
  11.  
  12. int *p = new(a) int [5];
  13.  
  14. for (int j = 0; j < 5; j++)
  15. cout<< p[j] << " "; // 0 10 20 30 40
  16.  
  17. cout<<endl;
  18.  
  19. double *pd = new (a + 5) double;
  20.  
  21. *pd = 12.34;
  22.  
  23. cout<< *pd <<endl; // 12.34
  24.  
  25. float *pf = new (a + 50) float (5.6F);
  26. cout<< *pf <<endl; // 5.6
  27.  
  28. return 0;
  29. }

Now, as a good hobbyist I'm trying to be, I tried to delete the pointer with
  1. delete p;
But, euh..., that didn't work and gave my computer almost a hart attack

So I figured, it's got to do with the fact that the array a is connected to the pointer.

If I'm correct, then problem is, how do I delete it, do I use a loop in wich I delete everything in the array. Because I tought you only had to delete the pointer pointing to the first place?

Ive tried to use this:
  1. delete [] p;
but got the same result, get message:
Debug Assertion Failed!
file: dbgheap.c
Line:1011

ANy help would be greatly appreciated
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Deleting a pointer?

 
0
  #2
Apr 28th, 2005
A shot into new (pun!) territory for me.

Here (way down at the bottom) it states,
Since placement new doesn't allocate any memory, it's an error to delete the object created by it.
For the time being I've been going with this method.
ADVICE: Don't use this "placement new" syntax unless you have to. Use it only when you really care that an object is placed at a particular location in memory. For example, when your hardware has a memory-mapped I/O timer device, and you want to place a Clock object at that memory location.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Deleting a pointer?

 
0
  #3
Apr 28th, 2005
Thanks for the links Dave, I'll read them first and when Ive got questions, I'll be back
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: Deleting a pointer?

 
0
  #4
Apr 28th, 2005
Originally Posted by JoBe
[code]
#include <iostream>

using namespace std;

int main()
{
int a[100]= {0};

for (int i = 0; i < 100; ++i)
a[i] = 100 * i;

int *p = new(a) int [5];

for (int j = 0; j < 5; j++)
cout<< p[j] << " "; // 0 10 20 30 40

cout<<endl;

hey, what's up its me again, i know you're asking for help in this thread but i think you can help me with the simple stuff :o

I'm trying to understand a few things about the arrays in your example

int a[100]= {0};

^^^does that mean the array size is 100 and the first element is equal to 0?

for (int i = 0; i < 100; ++i)
a[i] = 100 * i;

^^^Does that mean each of the 100 elements in the array (eg. a[0,1,2,..., 99]) will be multipled by 100, such as 0*100, 1*100, 2*00. If so, does that mean you array will look like this a[0, 100, 200,..., 99,000]?

int *p = new(a) int [5];

^^^Does this mean p points to array a and the array's size equals 5? If so, according to my previous question, why isn't the output 0 100 200 300 400 instead of 0 10 20 30 40?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 707
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Deleting a pointer?

 
0
  #5
Apr 29th, 2005
>^^^does that mean the array size is 100 and the first element is equal to 0?
An array of size 100 and all elements are initialized to 0. If you privide an initializer list and there aren't enough values, all remaining elements are initialized to the default value T() where T is the type of the array.

>If so, does that mean you array will look like this a[0, 100, 200,..., 99,000]?
Try it and see.

>^^^Does this mean p points to array a and the array's size equals 5?
Yes.

>why isn't the output 0 100 200 300 400 instead of 0 10 20 30 40?
It is. The comment and code don't match.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: Deleting a pointer?

 
0
  #6
Apr 29th, 2005
>An array of size 100 and all elements are initialized to 0. If you privide an initializer list and there aren't enough values, all remaining elements are initialized to the default value T() where T is the type of the array.

Hmmm...you lost me. Where did this default value T() where T is the type of the array stuff come from?

>Try it and see.

I can't, not right now. I don't own a computer remember? Dude I'm not getting a Dell

Thanks for the help Na
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 707
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Deleting a pointer?

 
0
  #7
Apr 29th, 2005
>Hmmm...you lost me.
Joy.

>Where did this default value T() where T is the type of the array stuff come from?
It's a generic way of saying that all of the ints are set to 0. However, you can do this:
  1. int x = int();
And set x to the default value of an int, or 0. To make the description generic, one would say T() instead of int() where T refers to an arbitrary, but unknown type.

>I don't own a computer remember?
That makes teaching you C++ a reall pain in the butt. There are just some things you have to experiment with to understand.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: Deleting a pointer?

 
0
  #8
Apr 29th, 2005
>Joy.


Hahahaha...you're crazy for that


>It's a generic way of saying that all of the ints are set to 0. However, you can do this:
  1. int x = int();

And set x to the default value of an int, or 0. To make the description generic, one would say T() instead of int() where T refers to an arbitrary, but unknown type.

got you!


>That makes teaching you C++ a reall pain in the butt. There are just some things you have to experiment with to understand.

Yeah I know and I love you for that!

Thanks for hanging in there and helping me. I'm doing the best I can right now without a computer of my own to ACTUALLY code. I'll have one soon!

again, much appreciated (we'll have to do lunch on Peach Tree some day or something)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC