new placement again...

1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main() {
	int pool[3];
	int* p1 = new(pool) int;
	int* p2 = new(pool) int;
	int* p3 = new(pool) int;
	int* p4 = new(pool) int; // OOPS. what is exactly going to happen here?
	                         // OUT OF BOUNDS and then what?
}
Last edited on
1
2
3
4
int* p1 = new(pool) int;
int* p2 = new(pool) int;
int* p3 = new(pool) int;
int* p4 = new(pool) int; 


All these are being created in the same memory location, over the top of pool[0].
If you did actually write
new (pool + 3) int;
Then your program's behavior would be undefined.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main() {
    
	int pool[3];
	int* p1 = new(pool) int;
	int* p2 = new(pool) int;
	int* p3 = new(pool) int;
	int* p4 = new(pool) int; 
	
	cout << "pool : " << pool << endl; 
	cout << "p1 : " << p1 << endl; 
	cout << "p2 : " << p2 << endl; 
	cout << "p3 : " << p3 << endl; 
	cout << "p4 : " << p4 << endl; 
	
	return 0;
}



$g++ -std=c++11 -o main *.cpp
$main
pool : 0x7fff4e9b7f24
p1 : 0x7fff4e9b7f24
p2 : 0x7fff4e9b7f24
p3 : 0x7fff4e9b7f24
p4 : 0x7fff4e9b7f24
Topic archived. No new replies allowed.