| | |
Deleting a pointer?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Hello ladies and gents,
Ive got this example of a program that I tried out wich shows some special possibilities to use new.
Now, as a good hobbyist I'm trying to be, I tried to delete the pointer with
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: but got the same result, get message:
Debug Assertion Failed!
file: dbgheap.c
Line:1011
ANy help would be greatly appreciated
Ive got this example of a program that I tried out wich shows some special possibilities to use new.
C++ Syntax (Toggle Plain Text)
#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; double *pd = new (a + 5) double; *pd = 12.34; cout<< *pd <<endl; // 12.34 float *pf = new (a + 50) float (5.6F); cout<< *pf <<endl; // 5.6 return 0; }
Now, as a good hobbyist I'm trying to be, I tried to delete the pointer with
C++ Syntax (Toggle Plain Text)
delete p;
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:
C++ Syntax (Toggle Plain Text)
delete [] p;
Debug Assertion Failed!
file: dbgheap.c
Line:1011
ANy help would be greatly appreciated
A shot into new (pun!) territory for me.
Here (way down at the bottom) it states, For the time being I've been going with this method.
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.
•
•
•
•
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
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
•
•
•
•
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?
>^^^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.
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.
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 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.
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
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
>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:
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.
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:
C++ Syntax (Toggle Plain Text)
int x = int();
>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.
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
>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:
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)
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:
C++ Syntax (Toggle Plain Text)
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)
![]() |
Similar Threads
- Deleting this pointer (C++)
- How to be Crash Free (C++)
- storing references to classes (C++)
Other Threads in the C++ Forum
- Previous Thread: STL set vs. map
- Next Thread: Multiple files C project.
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






