Ad
Placement New Operator
#include<iostream>
using namespace std;
int main() {
int buf[2];
int *p=new (buf) int(2);
int *q=new (buf+1) int(6);
for(int i=0;i<2;i++)
cout<<buf[i]<<" ";
return 0;
}
I was trying placement new operator with the following example. For the above code I get the output as:
trial.cpp:7:20: warning: placement new constructing an object of type 'int' and size '4' in a region of type 'int [2]' and size '0' [-Wplacement-new=]
int *q=new (buf+1) int(6);
2 6
Why am I getting a warning for *q ? From my understanding p and q are 2 pointers pointing to 2 different blocks in buf array. Please correct me if I'm wrong.
Ad
Answer
This is a GCC bug affecting versions 8 to 10 and fixed in 11, see bug report.
The code is fine, the warning a false-positive.
Ad
source: stackoverflow.com
Related Questions
- → Comparing two large files are taking over four hours
- → Setting JSON node name to variable value
- → Compiling GLUT using Emscripten
- → Evaluate check box from a scanned image in node.js
- → Find an easy web server framework for mobile game
- → my https C++ code doesn't work on some sites (binance)
- → Error while opening pivx wallet on ubuntu
- → Why sending a POST by AJAX is interpreted by the HTTP Server as OPTIONS and sending by CURL is effectively a PUT?
- → Python reading in one line multiple types for a calculator
- → How do I properly pass an argument to a function
- → Accessing Websql database with Qt
- → Using Mysql C API for c++ codes
- → How do I set constants at run-time in a c++ header file, imported through Cython?
Ad