Hello ladies and gents,

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

#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

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:

delete [] p;

but got the same result, get message:
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,

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.

Thanks for the links Dave, I'll read them first and when Ive got questions, I'll be back ;)

    #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 0100, 1100, 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.

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:

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.

>Joy.


Hahahaha...you're crazy for that :D


>It's a generic way of saying that all of the ints are set to 0. However, you can do this:

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)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.