[英]Why am I getting warning 'C6386' Buffer overrun while writing to 'ptr'?
`Variable 'size' is 10 in this example.在此示例中,变量“大小”为 10。 If I were to hardcode 10 in place of 'int(size)' the overrun warning goes away.
如果我用硬编码 10 代替“int(size)”,超限警告就会消失。 Any suggestions/reasoning why this is occurring?
为什么会发生这种情况的任何建议/推理? I want to allocate 80 bytes for my pointer, each of the allocated values being a timestep from the given timespan.
我想为我的指针分配 80 个字节,每个分配的值都是给定时间跨度的一个时间步长。
Thank you!谢谢!
int main() {
const double h = 0.1;
const double tspan[2] = { 0.0, 1.0 };
const double size =round(tspan[1] / h);
double *ptr = (double*)malloc(int(size) * sizeof(double));
if (!ptr) {
cout << "Memory Allocation Failed";
exit(1);
}
double j = 0;
for (int i = 0; i < size; i++) {
ptr[i] = j;
//cout << j << '\n';
j++;
}
cout << '\n';
for (int i = 0; i < size; i++) {
cout << *(ptr + i) << endl;
//cout << i << '\n';
}
free(ptr);
return 0;
}
I have tried dereferencing the pointer and making sure it isn't NULL.我试过取消引用指针并确保它不是 NULL。 I have also printed out the result.
我也打印了结果。 The result being a pointer that counts 0-9.
结果是一个计数 0-9 的指针。 `
`
double size
can be 10.1
, the condition i < 10.1
does not terminate the loop if i
is 10, the allocated buffer size is int(10.1)
, that is 10, ptr[10]
causes the buffer overrun. double size
可以是10.1
,条件i < 10.1
不终止循环如果i
是 10,分配的缓冲区大小是int(10.1)
,即 10, ptr[10]
导致缓冲区溢出。
As an addedum to what 273K has answered, you csn solve this problem by having size
as a std::size_t
variable.作为 273K 回答的补充,您 csn 通过将
size
作为std::size_t
变量来解决这个问题。
Let's floor the result of that division and cast to int
.让我们对该除法的结果进行 floor 并转换为
int
。
int main() {
const double h = 0.1;
const double tspan[2] = { 0.0, 1.0 };
const int size = static_cast<int>(std::floor(tspan[1] / h));
double *ptr = (double*)malloc(size * sizeof(double));
if (!ptr) {
cout << "Memory Allocation Failed";
exit(1);
}
double j = 0;
for (int i = 0; i < size; i++) {
ptr[i] = j;
//cout << j << '\n';
j++;
}
cout << '\n';
for (int i = 0; i < size; i++) {
cout << *(ptr + i) << endl;
//cout << i << '\n';
}
free(ptr);
return 0;
}
Somewhat better, let's use new and delete instead of malloc and free.更好的是,让我们使用 new 和 delete 而不是 malloc 和 free。
int main() {
const double h = 0.1;
const double tspan[2] = { 0.0, 1.0 };
const int size = static_cast<int>(std::floor(tspan[1] / h));
double *ptr = new double[size];
if (!ptr) {
std::cout << "Memory Allocation Failed";
exit(1);
}
double j = 0;
for (int i = 0; i < size; i++) {
ptr[i] = j;
//std::cout << j << '\n';
j++;
}
std::cout << '\n';
for (int i = 0; i < size; i++) {
std::cout << *(ptr + i) << std::endl;
//cout << i << '\n';
}
delete[] ptr;
return 0;
}
Better still, use std::vector
.更好的是,使用
std::vector
。
int main() {
const double h = 0.1;
const double tspan[2] = { 0.0, 1.0 };
const int size = static_cast<int>(std::floor(tspan[1] / h));
std::vector<double> vec(size);
double j = 0;
for (auto &x : vec) {
x = j;
//std::cout << j << '\n';
j++;
}
std::cout << '\n';
for (auto x : vec) {
std::cout << x << std::endl;
//std::cout << i << '\n';
}
return 0;
}
round()
rounds to the nearest integer number. round()
四舍五入到最接近的整数。 If anything, you should use ceil()
to round up .如果有的话,你应该使用
ceil()
来四舍五入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.