new(size_t, void* p)

Have a look at this;
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main() {
	int x = 0;

	int* p1 = new(&x) int[2]; // 2 integers allocated at &x 
                                  // the first one will overwrite x's value?
                                  // what about the second int
	*p1 = 5;
	*(p1 + 1) = 6;
	
	system("PAUSE");
}
Last edited on
Compile and run this:

1
2
3
4
5
6
#include <iostream>
int main() {
    int x = 0;
    auto a = new(&x) int[2];
    a[0] = 5; a[1] = 5;
}

And this:

1
2
3
4
5
6
7
#include <iostream>
int main() {
    int x = 0;
    auto a = new(&x) int[2];
    a[0] = 5; a[1] = 5;
    std::cout << a[0] << ',' << a[1] << '\n';
}

And this:

1
2
3
4
5
6
#include <iostream>
int main() {
    int x = 0;
    auto a = new(&x) int[2];
    a[0] = a[1] = 5;
}

Last edited on
you forgot to include <new> in the first and last samples, however; the 4 source codes are the same :D, I just want to know why the compiler says that there is a corruption around x.
Last edited on
you forgot to include <new> in the first and last samples

You're right. I actually had <iostream> in all of them and removed when I posted those. I edited it to put iostream back in all of them.

What compiler are you using?

Did you compile and run all of them? (Don't just say they are the same without compiling and running them. I get different results from them.)

What exactly does your "compiler" say about a "corruption"?
Last edited on
I compiled 3 of them using visual studio 2017 and I always get the same Autos:

&x..........0x006ff8c8 {5}..........int *
a............0x006ff8c8 {5}..........int *
a[0]........5..............................int
a[1]........5..............................int
x............5..............................int


+ Exception thrown : Run-Time Check Failure #2 - Stack around the variable 'x' was corrupted. occurred

-- This happens when going out of scope --
1
2
3
4
5
6
7
8
#include <iostream>
int main() {
    int x = 0;
    auto a = new(&x) int[2];
    a[0] = a[1] = 5;
    std::cout << a[0] << ' ' << a[1] << '\n';
    system("PAUSE"); // It will crash after you press any key
}
Last edited on
Instead of allocating memory you use the memory of x. So the second field of the array is out of bounds, i.e. undefined behavior which might lead to a crash,
Maybe; but the program crashes after going out of scope (i.e implicit destruction of x)
I'm not sure what you mean by "Autos" there (or why you would think I would know what that means), but I assume it's some kind of debugging thing in VS.

But the reason for the exception is obvious. I thought that was the point of your question. You aren't providing enough space for the two-int array. How could it possibly fit into the space of one int? What did you expect to happen?
I thought that the second int would be allocated at &x + 1
this code doesn't crash
1
2
3
4
5
6
7
8
#include <iostream>
int main() {
	int x = 0;
	int* a = new(&x) int[2];
	a[0] = 0;
	//std::cout << a[0] << ' ' << a[1] << '\n';
	system("PAUSE");
}
Last edited on
The only reason the other code "crashes" is because there is apparently a special check that is done at the end of your code that (attempts to) detect stack corruption. You are probably running it in some kind of debugging mode. If you run it in a "release" mode, it may not actually crash. The fact is that there is almost certainly space for the extra int on the stack, so it's the kind of error that can go undetected without a special check.

If you only modify the first int of the two then the special (presumably debug-only) check won't be able to detect a problem.

You could try leaving it in the debugging mode and not writing to either of the locations and just doing the cout statement to see what value it prints. I wish I had a windows system to test it on myself. I'm thinking of getting one. :-)
Last edited on
You should definitely get a windows machine :)
Now I understand what is happening there after executing this code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main() {
	int* p = new int[10];
	for (int i = 0; i < 10; ++i)
		p[i] = i + 1;
	std::cout << p << '\n';
	for (int i = 0; i < 10; ++i)
		std::cout << &p[i] << '\n';
	delete[] p;
	int* q = new int(100);
	std::cout << q << '\n';
        delete q;

	system("PAUSE");
}
Last edited on
You should definitely get a windows machine :)

No Holy Wars pl0x.
No Holy Wars pl0x.

I want to get one. It'll be dual boot, of course! But I need a new laptop.
Topic archived. No new replies allowed.